tomcat版本升级-启动报错
1、报错信息:
Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
2、报错原因:
根本原因是关闭 ApplicationContext 时, 它将销毁所有单例 bean, eurekaAutoServiceRegistration 首先销毁, 然后销毁feignContext. 销毁 feignContext 时, 它将关闭与每个 FeignClient 关联的 ApplicationContext. 由于eurekaAutoServiceRegistration 监听 ContextClosedEvent, 因此这些事件将发送到该bean. 不幸的是, 由于它已被销毁, 因此我们得到了上述异常(尝试在销毁中创建 bean )
3、解决方案:
@Component
public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (containsBeanDefinition(beanFactory, "feignContext", "eurekaAutoServiceRegistration")) {
/* 调整依赖顺序,这样会先销毁 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));
}
}