报错 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学习资源,一起成长,一起进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值