2727import org .springframework .beans .factory .support .RootBeanDefinition ;
2828import org .springframework .context .ApplicationListener ;
2929import org .springframework .context .event .ApplicationEventMulticaster ;
30+ import org .springframework .util .ObjectUtils ;
3031
3132/**
3233 * {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener}
@@ -57,8 +58,8 @@ public ApplicationListenerDetector(AbstractApplicationContext applicationContext
5758
5859 @ Override
5960 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 () );
6263 }
6364 }
6465
@@ -76,23 +77,23 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
7677 // singleton bean (top-level or inner): register on the fly
7778 this .applicationContext .addApplicationListener ((ApplicationListener <?>) bean );
7879 }
79- else if (flag == null ) {
80+ else if (Boolean . FALSE . equals ( flag ) ) {
8081 if (logger .isWarnEnabled () && !this .applicationContext .containsBean (beanName )) {
8182 // inner bean with other scope - can't reliably process events
8283 logger .warn ("Inner bean '" + beanName + "' implements ApplicationListener interface " +
8384 "but is not reachable for event multicasting by its containing ApplicationContext " +
8485 "because it does not have singleton scope. Only top-level listener beans are allowed " +
8586 "to be of non-singleton scope." );
8687 }
87- this .singletonNames .put (beanName , Boolean . FALSE );
88+ this .singletonNames .remove (beanName );
8889 }
8990 }
9091 return bean ;
9192 }
9293
9394 @ Override
9495 public void postProcessBeforeDestruction (Object bean , String beanName ) {
95- if (bean instanceof ApplicationListener ) {
96+ if (this . applicationContext != null && bean instanceof ApplicationListener ) {
9697 ApplicationEventMulticaster multicaster = this .applicationContext .getApplicationEventMulticaster ();
9798 multicaster .removeApplicationListener ((ApplicationListener <?>) bean );
9899 multicaster .removeApplicationListenerBean (beanName );
@@ -113,7 +114,7 @@ public boolean equals(Object other) {
113114
114115 @ Override
115116 public int hashCode () {
116- return this .applicationContext . hashCode ( );
117+ return ObjectUtils . nullSafeHashCode ( this .applicationContext );
117118 }
118119
119120}
0 commit comments