Spring Cloud Edgware.SR4 在测试时报Error creating bean with name 'eurekaAutoServiceRegistration'

在GitHub上找到了解决办法,链接如下:https://github.com/spring-cloud/spring-cloud-netflix/issues/1952

里面的crmky大神讲的很清楚,简单翻译下,

问题原因:

      当关闭ApplicationContext时,它将销毁所有单例bean,首先销毁eurekaAutoServiceRegistration,然后销毁feignContext。当销毁feignContext时,它将关闭与每个FeignClient相关联的ApplicationContext。因为eurekaAutoServiceRegistration侦听ContextClosedEvent,所以这些事件将被发送到该bean。不幸的是,因为它已经被销毁了,所以我们得到了上面的异常(尝试在销毁中创建bean)。

解决办法:

     随便在哪个包下新建下面的类就可

@Component
public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (containsBeanDefinition(beanFactory, "feignContext", "eurekaAutoServiceRegistration")) {
            BeanDefinition bd = beanFactory.getBeanDefinition("feignContext");
            bd.setDependsOn("eurekaAutoServiceRegistration");
        }
    }

    private boolean containsBeanDefinition(ConfigurableListableBeanFactory beanFactory, String... beans) {
        return Arrays.stream(beans).allMatch(b -> beanFactory.containsBeanDefinition(b));
    }
}

 

你可能感兴趣的:(SpringCloud)