エラーが表示されました.BeanCreationNotAllowedException:Error creating bean with name'eurekaAutoServiceRegistration'

6933 ワード

問題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公式のissuehttps://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.同時にeurekaAutoServiceRegistrationContextClosedEventContextClosedEventを傍受するすべてのイベントがそのbeanに送信されます.残念なことに、それはすでに閉鎖されているので、上の異常が発生します.
4.解決策は括弧の中にあり、再破棄時にbeanを作成する
qq群859759121に参加することを歓迎して、大量の無料vip学習資源、いっしょに成長して、いっしょに進歩します