我有一个ApplicationListener
使用 a创建的应用程序FactoryBean
(创建很复杂,所以我FactoryBean
为它创建了一个),并且FactoryBean.isSingleton()
返回true
.
在 Spring 3 中,它具有魅力,但现在我正在升级到 Spring 5.3,但它不起作用。我诊断了很多,我在org.springframework.context.support.ApplicationListenerDetector
课堂上发现了它。
该类中有两种方法
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
if (ApplicationListener.class.isAssignableFrom(beanType)) {
this.singletonNames.put(beanName, beanDefinition.isSingleton());
}
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof ApplicationListener) {
// potentially not detected as a listener by getBeanNamesForType retrieval
Boolean flag = this.singletonNames.get(beanName);
if (Boolean.TRUE.equals(flag)) {
// singleton bean (top-level or inner): register on the fly
this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
}
else if (Boolean.FALSE.equals(flag)) {
if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
// inner bean with other scope - can't reliably process events
logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
"but is not reachable for event multicasting by its containing ApplicationContext " +
"because it does not have singleton scope. Only top-level listener beans are allowed " +
"to be of non-singleton scope.");
}
this.singletonNames.remove(beanName);
}
}
return bean;
}
虽然我FactoryBean.isSingleton()
返回true
了,但是第一个方法没有为 a 返回的 bean 调用FactoryBean
,因此第二个方法没有ApplicationListener
在ApplicationContext
.
所以现在我想看看是 Spring 5.3 的一个 it 错误,还是我错过了升级 Spring 版本的一些额外的东西?