BeanCreationNotAllowedException:遇到异常方不方?
程序猿:慌毛线,我们跟着trace看一下
org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'stringRedisTemplate': Singleton bean
creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy metho
d implementation!)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:216)
~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.6.
RELEASE.jar!/:4.3.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220) ~[spring-beans-4.3.6.RE
LEASE.jar!/:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1018)
~[spring-beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345) ~[spring-
beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) ~[spring-
beans-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093) ~[spring-conte
xt-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
...
- 首先看下异常信息,优秀的框架日志都很好很清晰,基本可以一目了然,起码能给你指引
- 日志已经很清楚的说明了:单例bean不允许创建,在单例们的工厂destruction销毁的时候
- 碰到该异常基本是在应用停止时,因为停止时会销毁工厂,在销毁的期间如果有业务线程进来处理业务,这时从工厂获取bean的时候可能获取不到,因为缓存均都已经清空,缓存取不到当然要创建bean了,在创建的时候发现工厂当前已经被标识为销毁状态,于是就抛出了该异常,那么下面我们来跟着代码一步步验证我们的结论
1. 销毁bean:AbstractApplicationContext的关闭钩子
- AbstractApplicationContext实现了ConfigurableApplicationContext接口的钩子方法,而且方法中调用了doClose方法
public void registerShutdownHook() {
if (this.shutdownHook == null) {
// No shutdown hook registered yet.
this.shutdownHook = new Thread() {
@Override
public void run() {
synchronized (startupShutdownMonitor) {
doClose();
}
}
};
Runtime.getRuntime().addShutdownHook(this.shutdownHook);
}
}
- AbstractApplicationContext中的doClose方法中先销毁了beans,然后销毁了bean工厂
- 销毁bean是调用工厂的销毁方法destroySingletons
- DefaultListableBeanFactory中的销毁方法调用父类DefaultSingletonBeanRegistry进行对各种bean销毁,而且会对singletonsCurrentlyInDestruction标识置为true
public void destroySingletons() {
...
synchronized (this.singletonObjects) {
this.singletonsCurrentlyInDestruction = true;
}
...
}
2. 获取Bean:此时正在执行的业务流程会有可能调用到getBean
- 然后我们看下此时getBean中是否判断了此标识并抛出了相应的异常
- AbstractBeanFactory根据名称获取getBean调用doGetBean
- 调用父类getSingleton根据名称从缓存中获取bean(如果此时工厂正在销毁,此时取到的是可能会为null的),如果为null则会重新创建,首先获取bean定义判断是否是单例类型,如果是则按照单例的方式创建:AbstractBeanFactory根据beanName与ObjectFactory工厂获取共享实例getSingleton
- 调用DefaultSingletonBeanRegistry实现的getSingleton
- 可以看到这里首先判断了singletonsCurrentlyInDestruction标识,如果工厂正常销毁则抛出异常BeanCreationNotAllowedException
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
...
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName,
"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!)");
}
...
}
3. 总结
至此证明了我们的推论,出现该情况的场景需要满足以下几点
- 工厂正在销毁
- 此时业务中刚好执行到去工厂中获取bean
- 并且bean是单例模式
到这里我们基本明白该异常的原因,如果我们刚好是在停止或者重启spring应用,出现该异常属于偶发的现象且基本不会产生什么业务影响