Java Code Examples for org.springframework.context.support.StaticApplicationContext#addApplicationListener()

The following examples show how to use org.springframework.context.support.StaticApplicationContext#addApplicationListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void anonymousClassAsListener() {
	final Set<MyEvent> seenEvents = new HashSet<>();
	StaticApplicationContext context = new StaticApplicationContext();
	context.addApplicationListener(new ApplicationListener<MyEvent>() {
		@Override
		public void onApplicationEvent(MyEvent event) {
			seenEvents.add(event);
		}
	});
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	context.publishEvent(new MyOtherEvent(context));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertSame(2, seenEvents.size());
	assertTrue(seenEvents.contains(event1));
	assertTrue(seenEvents.contains(event2));

	context.close();
}
 
Example 2
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void lambdaAsListener() {
	final Set<MyEvent> seenEvents = new HashSet<>();
	StaticApplicationContext context = new StaticApplicationContext();
	ApplicationListener<MyEvent> listener = seenEvents::add;
	context.addApplicationListener(listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	context.publishEvent(new MyOtherEvent(context));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertSame(2, seenEvents.size());
	assertTrue(seenEvents.contains(event1));
	assertTrue(seenEvents.contains(event2));

	context.close();
}
 
Example 3
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void lambdaAsListenerWithErrorHandler() {
	final Set<MyEvent> seenEvents = new HashSet<>();
	StaticApplicationContext context = new StaticApplicationContext();
	SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
	multicaster.setErrorHandler(ReflectionUtils::rethrowRuntimeException);
	context.getBeanFactory().registerSingleton(
			StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
	ApplicationListener<MyEvent> listener = seenEvents::add;
	context.addApplicationListener(listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	context.publishEvent(new MyOtherEvent(context));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertSame(2, seenEvents.size());
	assertTrue(seenEvents.contains(event1));
	assertTrue(seenEvents.contains(event2));

	context.close();
}
 
Example 4
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void anonymousClassAsListener() {
	final Set<MyEvent> seenEvents = new HashSet<>();
	StaticApplicationContext context = new StaticApplicationContext();
	context.addApplicationListener(new ApplicationListener<MyEvent>() {
		@Override
		public void onApplicationEvent(MyEvent event) {
			seenEvents.add(event);
		}
	});
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	context.publishEvent(new MyOtherEvent(context));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertSame(2, seenEvents.size());
	assertTrue(seenEvents.contains(event1));
	assertTrue(seenEvents.contains(event2));

	context.close();
}
 
Example 5
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void lambdaAsListener() {
	final Set<MyEvent> seenEvents = new HashSet<>();
	StaticApplicationContext context = new StaticApplicationContext();
	ApplicationListener<MyEvent> listener = seenEvents::add;
	context.addApplicationListener(listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	context.publishEvent(new MyOtherEvent(context));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertSame(2, seenEvents.size());
	assertTrue(seenEvents.contains(event1));
	assertTrue(seenEvents.contains(event2));

	context.close();
}
 
Example 6
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void lambdaAsListenerWithErrorHandler() {
	final Set<MyEvent> seenEvents = new HashSet<>();
	StaticApplicationContext context = new StaticApplicationContext();
	SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
	multicaster.setErrorHandler(ReflectionUtils::rethrowRuntimeException);
	context.getBeanFactory().registerSingleton(
			StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
	ApplicationListener<MyEvent> listener = seenEvents::add;
	context.addApplicationListener(listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	context.publishEvent(new MyOtherEvent(context));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertSame(2, seenEvents.size());
	assertTrue(seenEvents.contains(event1));
	assertTrue(seenEvents.contains(event2));

	context.close();
}
 
Example 7
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void lambdaAsListenerWithJava8StyleClassCastMessage() {
	StaticApplicationContext context = new StaticApplicationContext();
	ApplicationListener<ApplicationEvent> listener =
			event -> { throw new ClassCastException(event.getClass().getName()); };
	context.addApplicationListener(listener);
	context.refresh();

	context.publishEvent(new MyEvent(context));
	context.close();
}
 
Example 8
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void lambdaAsListenerWithJava9StyleClassCastMessage() {
	StaticApplicationContext context = new StaticApplicationContext();
	ApplicationListener<ApplicationEvent> listener =
			event -> { throw new ClassCastException("spring.context/" + event.getClass().getName()); };
	context.addApplicationListener(listener);
	context.refresh();

	context.publishEvent(new MyEvent(context));
	context.close();
}
 
Example 9
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void lambdaAsListenerWithJava8StyleClassCastMessage() {
	StaticApplicationContext context = new StaticApplicationContext();
	ApplicationListener<ApplicationEvent> listener =
			event -> { throw new ClassCastException(event.getClass().getName()); };
	context.addApplicationListener(listener);
	context.refresh();

	context.publishEvent(new MyEvent(context));
	context.close();
}
 
Example 10
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void lambdaAsListenerWithJava9StyleClassCastMessage() {
	StaticApplicationContext context = new StaticApplicationContext();
	ApplicationListener<ApplicationEvent> listener =
			event -> { throw new ClassCastException("spring.context/" + event.getClass().getName()); };
	context.addApplicationListener(listener);
	context.refresh();

	context.publishEvent(new MyEvent(context));
	context.close();
}