spring cloud 测试的时候报 BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration' 但能正确跑完测试方法

因为都能正确的跑测试方法,所以我也不太注意它,但是有时候闲得蛋疼就会找一下原因。

具体原因我也说不清,直接丢个连接

https://github.com/spring-cloud/spring-cloud-netflix/issues/1952

里面的一位叫crmky的大神解释的很清楚了,

由于是英文,我理解是可以的,但是翻成中文就不知道什么鬼了

所以我直接丢解决方法

@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));
    }
}

当然里面的spring人员也说了

The workaround works when running the application, but not when running tests disappointed

翻译就是最新的版本测试的时候依然有这问题,但是正常跑的时候没有

大哥,我们虽然没说正常跑的时候没这个问题,但是我们提的问题是测试的时候有这个问题啊!!!

猛然间,中了springcloud的人员的一个程序员冷笑话...

原文地址:https://www.cnblogs.com/ydymz/p/8953388.html