27
27
import org .springframework .beans .factory .support .RootBeanDefinition ;
28
28
import org .springframework .context .ApplicationListener ;
29
29
import org .springframework .context .event .ApplicationEventMulticaster ;
30
+ import org .springframework .util .ObjectUtils ;
30
31
31
32
/**
32
33
* {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener}
@@ -57,8 +58,8 @@ public ApplicationListenerDetector(AbstractApplicationContext applicationContext
57
58
58
59
@ Override
59
60
public void postProcessMergedBeanDefinition (RootBeanDefinition beanDefinition , Class <?> beanType , String beanName ) {
60
- if (this .applicationContext != null && beanDefinition . isSingleton () ) {
61
- this .singletonNames .put (beanName , Boolean . TRUE );
61
+ if (this .applicationContext != null ) {
62
+ this .singletonNames .put (beanName , beanDefinition . isSingleton () );
62
63
}
63
64
}
64
65
@@ -76,23 +77,23 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
76
77
// singleton bean (top-level or inner): register on the fly
77
78
this .applicationContext .addApplicationListener ((ApplicationListener <?>) bean );
78
79
}
79
- else if (flag == null ) {
80
+ else if (Boolean . FALSE . equals ( flag ) ) {
80
81
if (logger .isWarnEnabled () && !this .applicationContext .containsBean (beanName )) {
81
82
// inner bean with other scope - can't reliably process events
82
83
logger .warn ("Inner bean '" + beanName + "' implements ApplicationListener interface " +
83
84
"but is not reachable for event multicasting by its containing ApplicationContext " +
84
85
"because it does not have singleton scope. Only top-level listener beans are allowed " +
85
86
"to be of non-singleton scope." );
86
87
}
87
- this .singletonNames .put (beanName , Boolean . FALSE );
88
+ this .singletonNames .remove (beanName );
88
89
}
89
90
}
90
91
return bean ;
91
92
}
92
93
93
94
@ Override
94
95
public void postProcessBeforeDestruction (Object bean , String beanName ) {
95
- if (bean instanceof ApplicationListener ) {
96
+ if (this . applicationContext != null && bean instanceof ApplicationListener ) {
96
97
ApplicationEventMulticaster multicaster = this .applicationContext .getApplicationEventMulticaster ();
97
98
multicaster .removeApplicationListener ((ApplicationListener <?>) bean );
98
99
multicaster .removeApplicationListenerBean (beanName );
@@ -113,7 +114,7 @@ public boolean equals(Object other) {
113
114
114
115
@ Override
115
116
public int hashCode () {
116
- return this .applicationContext . hashCode ( );
117
+ return ObjectUtils . nullSafeHashCode ( this .applicationContext );
117
118
}
118
119
119
120
}
0 commit comments