2121
2222import org .aopalliance .intercept .MethodInvocation ;
2323import org .junit .Test ;
24+
2425import org .springframework .aop .framework .ProxyFactory ;
2526import org .springframework .beans .factory .config .RuntimeBeanReference ;
2627import org .springframework .beans .factory .support .RootBeanDefinition ;
2930import org .springframework .context .ApplicationListener ;
3031import org .springframework .context .BeanThatBroadcasts ;
3132import org .springframework .context .BeanThatListens ;
33+ import org .springframework .context .support .AbstractApplicationContext ;
3234import org .springframework .context .support .StaticApplicationContext ;
3335import org .springframework .core .Ordered ;
3436import org .springframework .tests .sample .beans .TestBean ;
@@ -134,6 +136,34 @@ public void listenersInApplicationContext() {
134136 assertTrue (listener1 .seenEvents .contains (event2 ));
135137 assertTrue (listener1 .seenEvents .contains (event3 ));
136138 assertTrue (listener1 .seenEvents .contains (event4 ));
139+
140+ context .close ();
141+ }
142+
143+ @ Test
144+ public void listenersInApplicationContextWithNestedChild () {
145+ StaticApplicationContext context = new StaticApplicationContext ();
146+ RootBeanDefinition nestedChild = new RootBeanDefinition (StaticApplicationContext .class );
147+ nestedChild .getPropertyValues ().add ("parent" , context );
148+ nestedChild .setInitMethodName ("refresh" );
149+ context .registerBeanDefinition ("nestedChild" , nestedChild );
150+ RootBeanDefinition listener1Def = new RootBeanDefinition (MyOrderedListener1 .class );
151+ listener1Def .setDependsOn (new String [] {"nestedChild" });
152+ context .registerBeanDefinition ("listener1" , listener1Def );
153+ context .refresh ();
154+
155+ MyOrderedListener1 listener1 = context .getBean ("listener1" , MyOrderedListener1 .class );
156+ MyEvent event1 = new MyEvent (context );
157+ context .publishEvent (event1 );
158+ assertTrue (listener1 .seenEvents .contains (event1 ));
159+
160+ SimpleApplicationEventMulticaster multicaster = context .getBean (
161+ AbstractApplicationContext .APPLICATION_EVENT_MULTICASTER_BEAN_NAME ,
162+ SimpleApplicationEventMulticaster .class );
163+ assertFalse (multicaster .getApplicationListeners ().isEmpty ());
164+
165+ context .close ();
166+ assertTrue (multicaster .getApplicationListeners ().isEmpty ());
137167 }
138168
139169 @ Test
@@ -157,6 +187,8 @@ public void nonSingletonListenerInApplicationContext() {
157187 assertTrue (MyNonSingletonListener .seenEvents .contains (event3 ));
158188 assertTrue (MyNonSingletonListener .seenEvents .contains (event4 ));
159189 MyNonSingletonListener .seenEvents .clear ();
190+
191+ context .close ();
160192 }
161193
162194 @ Test
@@ -171,6 +203,8 @@ public void listenerAndBroadcasterWithCircularReference() {
171203 BeanThatBroadcasts broadcaster = context .getBean ("broadcaster" , BeanThatBroadcasts .class );
172204 context .publishEvent (new MyEvent (context ));
173205 assertEquals ("The event was not received by the listener" , 2 , broadcaster .receivedCount );
206+
207+ context .close ();
174208 }
175209
176210 @ Test
@@ -180,10 +214,13 @@ public void innerBeanAsListener() {
180214 listenerDef .getPropertyValues ().add ("friends" , new RootBeanDefinition (BeanThatListens .class ));
181215 context .registerBeanDefinition ("listener" , listenerDef );
182216 context .refresh ();
217+
183218 context .publishEvent (new MyEvent (this ));
184219 context .publishEvent (new MyEvent (this ));
185220 TestBean listener = context .getBean (TestBean .class );
186221 assertEquals (3 , ((BeanThatListens ) listener .getFriends ().iterator ().next ()).getEventCount ());
222+
223+ context .close ();
187224 }
188225
189226
0 commit comments