35
35
36
36
import org .springframework .aop .framework .Advised ;
37
37
import org .springframework .aop .support .AopUtils ;
38
+ import org .springframework .beans .factory .BeanCreationException ;
38
39
import org .springframework .beans .factory .BeanInitializationException ;
40
+ import org .springframework .beans .factory .ObjectFactory ;
39
41
import org .springframework .beans .factory .annotation .Autowired ;
40
42
import org .springframework .context .ConfigurableApplicationContext ;
41
43
import org .springframework .context .PayloadApplicationEvent ;
55
57
import org .springframework .scheduling .annotation .Async ;
56
58
import org .springframework .scheduling .annotation .EnableAsync ;
57
59
import org .springframework .stereotype .Component ;
60
+ import org .springframework .util .Assert ;
58
61
59
62
import static org .hamcrest .Matchers .*;
60
63
import static org .junit .Assert .*;
@@ -238,6 +241,9 @@ public void eventListenerWorksWithSimpleInterfaceProxy() throws Exception {
238
241
assertTrue ("bean should be a proxy" , proxy instanceof Advised );
239
242
this .eventCollector .assertNoEventReceived (proxy .getId ());
240
243
244
+ this .context .publishEvent (new ContextRefreshedEvent (this .context ));
245
+ this .eventCollector .assertNoEventReceived (proxy .getId ());
246
+
241
247
TestEvent event = new TestEvent ();
242
248
this .context .publishEvent (event );
243
249
this .eventCollector .assertEvent (proxy .getId (), event );
@@ -252,6 +258,9 @@ public void eventListenerWorksWithAnnotatedInterfaceProxy() throws Exception {
252
258
assertTrue ("bean should be a proxy" , proxy instanceof Advised );
253
259
this .eventCollector .assertNoEventReceived (proxy .getId ());
254
260
261
+ this .context .publishEvent (new ContextRefreshedEvent (this .context ));
262
+ this .eventCollector .assertNoEventReceived (proxy .getId ());
263
+
255
264
TestEvent event = new TestEvent ();
256
265
this .context .publishEvent (event );
257
266
this .eventCollector .assertEvent (proxy .getId (), event );
@@ -266,10 +275,47 @@ public void eventListenerWorksWithCglibProxy() throws Exception {
266
275
assertTrue ("bean should be a cglib proxy" , AopUtils .isCglibProxy (proxy ));
267
276
this .eventCollector .assertNoEventReceived (proxy .getId ());
268
277
278
+ this .context .publishEvent (new ContextRefreshedEvent (this .context ));
279
+ this .eventCollector .assertNoEventReceived (proxy .getId ());
280
+
281
+ TestEvent event = new TestEvent ();
282
+ this .context .publishEvent (event );
283
+ this .eventCollector .assertEvent (proxy .getId (), event );
284
+ this .eventCollector .assertTotalEventsCount (1 );
285
+ }
286
+
287
+ @ Test
288
+ public void eventListenerWorksWithCustomScope () throws Exception {
289
+ load (CustomScopeTestBean .class );
290
+ CustomScope customScope = new CustomScope ();
291
+ this .context .getBeanFactory ().registerScope ("custom" , customScope );
292
+
293
+ CustomScopeTestBean proxy = this .context .getBean (CustomScopeTestBean .class );
294
+ assertTrue ("bean should be a cglib proxy" , AopUtils .isCglibProxy (proxy ));
295
+ this .eventCollector .assertNoEventReceived (proxy .getId ());
296
+
297
+ this .context .publishEvent (new ContextRefreshedEvent (this .context ));
298
+ this .eventCollector .assertNoEventReceived (proxy .getId ());
299
+
300
+ customScope .active = false ;
301
+ this .context .publishEvent (new ContextRefreshedEvent (this .context ));
302
+ customScope .active = true ;
303
+ this .eventCollector .assertNoEventReceived (proxy .getId ());
304
+
269
305
TestEvent event = new TestEvent ();
270
306
this .context .publishEvent (event );
271
307
this .eventCollector .assertEvent (proxy .getId (), event );
272
308
this .eventCollector .assertTotalEventsCount (1 );
309
+
310
+ try {
311
+ customScope .active = false ;
312
+ this .context .publishEvent (new TestEvent ());
313
+ fail ("Should have thrown IllegalStateException" );
314
+ }
315
+ catch (BeanCreationException ex ) {
316
+ // expected
317
+ assertTrue (ex .getCause () instanceof IllegalStateException );
318
+ }
273
319
}
274
320
275
321
@ Test
@@ -724,6 +770,17 @@ public void handleIt(TestEvent event) {
724
770
}
725
771
726
772
773
+ @ Component
774
+ @ Scope (scopeName = "custom" , proxyMode = ScopedProxyMode .TARGET_CLASS )
775
+ static class CustomScopeTestBean extends AbstractTestEventListener {
776
+
777
+ @ EventListener
778
+ public void handleIt (TestEvent event ) {
779
+ collectEvent (event );
780
+ }
781
+ }
782
+
783
+
727
784
@ Component
728
785
static class GenericEventListener extends AbstractTestEventListener {
729
786
@@ -779,4 +836,40 @@ public void handleSecond(String payload) {
779
836
}
780
837
}
781
838
839
+
840
+ private static class CustomScope implements org .springframework .beans .factory .config .Scope {
841
+
842
+ public boolean active = true ;
843
+
844
+ private Object instance = null ;
845
+
846
+ @ Override
847
+ public Object get (String name , ObjectFactory <?> objectFactory ) {
848
+ Assert .state (this .active , "Not active" );
849
+ if (this .instance == null ) {
850
+ this .instance = objectFactory .getObject ();
851
+ }
852
+ return this .instance ;
853
+ }
854
+
855
+ @ Override
856
+ public Object remove (String name ) {
857
+ return null ;
858
+ }
859
+
860
+ @ Override
861
+ public void registerDestructionCallback (String name , Runnable callback ) {
862
+ }
863
+
864
+ @ Override
865
+ public Object resolveContextualObject (String key ) {
866
+ return null ;
867
+ }
868
+
869
+ @ Override
870
+ public String getConversationId () {
871
+ return null ;
872
+ }
873
+ }
874
+
782
875
}
0 commit comments