报错 BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration'

如题
BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration'
这里直接给出解决方案 ,直接添加下面这类就可以了


package com.newcoin.broker.web.manager.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Description
 * @Author apdoer
 * @Date 2019/7/14 20:53
 * @Version 1.0
 */
@Component
public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        if (containsBeanDefinition(configurableListableBeanFactory,"feignContext","eurekaAutoServiceRegistration")){
            BeanDefinition bd = configurableListableBeanFactory.getBeanDefinition("feignContext");
            bd.setDependsOn("eurekaAutoServiceRegistration");
        }
    }

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

}

具体想知道什么原因的, 可以看springcloud官方的 issue

https://github.com/spring-cloud/spring-cloud-netflix/issues/1952crmky的回答

The root cause is when closing ApplicationContext, it will destroy all singleton bean, eurekaAutoServiceRegistration is destroyed first, then feignContext. When destroy feignContext, it will close the ApplicationContext associated with each FeignClient. Since eurekaAutoServiceRegistration listen on ContextClosedEvent, those events will be sent to that bean. Unfortunately because it has been destroyed, so we got the above exception (try to create bean in destruction).

这里简单说一下就是

1.当关闭ApplicationContext容器的时候 , 会销毁所有的单例bean , eurekaAutoServiceRegistration会最先被销毁,后面才是是feignContext ,

2.但是当关闭feignContext的时候,会销毁所有和 FeignClient关联的ApplicationContext

3.同时eurekaAutoServiceRegistration监听ContextClosedEventContextClosedEvent的所有事件将会被发送到那个bean,不幸的是,它因为它已经被关闭了,所以会出现上面异常

4.解决方案是括号里面的 , 再销毁的时候再创建bean

欢迎大家加入qq群859759121,大量免费vip学习资源,一起成长,一起进步

你可能感兴趣的:(异常解决)