21
21
22
22
import org .aopalliance .intercept .MethodInvocation ;
23
23
import org .junit .Test ;
24
+
24
25
import org .springframework .aop .framework .ProxyFactory ;
25
26
import org .springframework .beans .factory .config .RuntimeBeanReference ;
26
27
import org .springframework .beans .factory .support .RootBeanDefinition ;
29
30
import org .springframework .context .ApplicationListener ;
30
31
import org .springframework .context .BeanThatBroadcasts ;
31
32
import org .springframework .context .BeanThatListens ;
33
+ import org .springframework .context .support .AbstractApplicationContext ;
32
34
import org .springframework .context .support .StaticApplicationContext ;
33
35
import org .springframework .core .Ordered ;
34
36
import org .springframework .tests .sample .beans .TestBean ;
@@ -134,6 +136,34 @@ public void listenersInApplicationContext() {
134
136
assertTrue (listener1 .seenEvents .contains (event2 ));
135
137
assertTrue (listener1 .seenEvents .contains (event3 ));
136
138
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 ());
137
167
}
138
168
139
169
@ Test
@@ -157,6 +187,8 @@ public void nonSingletonListenerInApplicationContext() {
157
187
assertTrue (MyNonSingletonListener .seenEvents .contains (event3 ));
158
188
assertTrue (MyNonSingletonListener .seenEvents .contains (event4 ));
159
189
MyNonSingletonListener .seenEvents .clear ();
190
+
191
+ context .close ();
160
192
}
161
193
162
194
@ Test
@@ -171,6 +203,8 @@ public void listenerAndBroadcasterWithCircularReference() {
171
203
BeanThatBroadcasts broadcaster = context .getBean ("broadcaster" , BeanThatBroadcasts .class );
172
204
context .publishEvent (new MyEvent (context ));
173
205
assertEquals ("The event was not received by the listener" , 2 , broadcaster .receivedCount );
206
+
207
+ context .close ();
174
208
}
175
209
176
210
@ Test
@@ -180,10 +214,13 @@ public void innerBeanAsListener() {
180
214
listenerDef .getPropertyValues ().add ("friends" , new RootBeanDefinition (BeanThatListens .class ));
181
215
context .registerBeanDefinition ("listener" , listenerDef );
182
216
context .refresh ();
217
+
183
218
context .publishEvent (new MyEvent (this ));
184
219
context .publishEvent (new MyEvent (this ));
185
220
TestBean listener = context .getBean (TestBean .class );
186
221
assertEquals (3 , ((BeanThatListens ) listener .getFriends ().iterator ().next ()).getEventCount ());
222
+
223
+ context .close ();
187
224
}
188
225
189
226
0 commit comments