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

The following examples show how to use org.springframework.context.support.StaticApplicationContext#publishEvent() . 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 spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void listenersInApplicationContextWithPayloadEvents() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(MyPayloadListener.class));
	context.refresh();

	MyPayloadListener listener = context.getBean("listener", MyPayloadListener.class);
	context.publishEvent("event1");
	context.publishEvent("event2");
	context.publishEvent("event3");
	context.publishEvent("event4");
	assertTrue(listener.seenPayloads.contains("event1"));
	assertTrue(listener.seenPayloads.contains("event2"));
	assertTrue(listener.seenPayloads.contains("event3"));
	assertTrue(listener.seenPayloads.contains("event4"));

	AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
	assertEquals(2, multicaster.retrieverCache.size());

	context.close();
}
 
Example 2
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 3
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 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 nonSingletonListenerInApplicationContext() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition listener = new RootBeanDefinition(MyNonSingletonListener.class);
	listener.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	context.registerBeanDefinition("listener", listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	MyOtherEvent event2 = new MyOtherEvent(context);
	context.publishEvent(event2);
	MyEvent event3 = new MyEvent(context);
	context.publishEvent(event3);
	MyOtherEvent event4 = new MyOtherEvent(context);
	context.publishEvent(event4);
	assertTrue(MyNonSingletonListener.seenEvents.contains(event1));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event2));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event3));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event4));
	MyNonSingletonListener.seenEvents.clear();

	context.close();
}
 
Example 6
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void listenersInApplicationContextWithNestedChild() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition nestedChild = new RootBeanDefinition(StaticApplicationContext.class);
	nestedChild.getPropertyValues().add("parent", context);
	nestedChild.setInitMethodName("refresh");
	context.registerBeanDefinition("nestedChild", nestedChild);
	RootBeanDefinition listener1Def = new RootBeanDefinition(MyOrderedListener1.class);
	listener1Def.setDependsOn("nestedChild");
	context.registerBeanDefinition("listener1", listener1Def);
	context.refresh();

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	assertTrue(listener1.seenEvents.contains(event1));

	SimpleApplicationEventMulticaster multicaster = context.getBean(
			AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			SimpleApplicationEventMulticaster.class);
	assertFalse(multicaster.getApplicationListeners().isEmpty());

	context.close();
	assertTrue(multicaster.getApplicationListeners().isEmpty());
}
 
Example 7
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void nonSingletonListenerInApplicationContext() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition listener = new RootBeanDefinition(MyNonSingletonListener.class);
	listener.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	context.registerBeanDefinition("listener", listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	MyOtherEvent event2 = new MyOtherEvent(context);
	context.publishEvent(event2);
	MyEvent event3 = new MyEvent(context);
	context.publishEvent(event3);
	MyOtherEvent event4 = new MyOtherEvent(context);
	context.publishEvent(event4);
	assertTrue(MyNonSingletonListener.seenEvents.contains(event1));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event2));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event3));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event4));
	MyNonSingletonListener.seenEvents.clear();

	context.close();
}
 
Example 8
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 9
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 10
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void nonSingletonListenerInApplicationContext() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition listener = new RootBeanDefinition(MyNonSingletonListener.class);
	listener.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	context.registerBeanDefinition("listener", listener);
	context.refresh();

	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	MyOtherEvent event2 = new MyOtherEvent(context);
	context.publishEvent(event2);
	MyEvent event3 = new MyEvent(context);
	context.publishEvent(event3);
	MyOtherEvent event4 = new MyOtherEvent(context);
	context.publishEvent(event4);
	assertTrue(MyNonSingletonListener.seenEvents.contains(event1));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event2));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event3));
	assertTrue(MyNonSingletonListener.seenEvents.contains(event4));
	MyNonSingletonListener.seenEvents.clear();

	context.close();
}
 
Example 11
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void listenersInApplicationContextWithNestedChild() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition nestedChild = new RootBeanDefinition(StaticApplicationContext.class);
	nestedChild.getPropertyValues().add("parent", context);
	nestedChild.setInitMethodName("refresh");
	context.registerBeanDefinition("nestedChild", nestedChild);
	RootBeanDefinition listener1Def = new RootBeanDefinition(MyOrderedListener1.class);
	listener1Def.setDependsOn("nestedChild");
	context.registerBeanDefinition("listener1", listener1Def);
	context.refresh();

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyEvent event1 = new MyEvent(context);
	context.publishEvent(event1);
	assertTrue(listener1.seenEvents.contains(event1));

	SimpleApplicationEventMulticaster multicaster = context.getBean(
			AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
			SimpleApplicationEventMulticaster.class);
	assertFalse(multicaster.getApplicationListeners().isEmpty());

	context.close();
	assertTrue(multicaster.getApplicationListeners().isEmpty());
}
 
Example 12
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void innerBeanAsListener() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition listenerDef = new RootBeanDefinition(TestBean.class);
	listenerDef.getPropertyValues().add("friends", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("listener", listenerDef);
	context.refresh();

	context.publishEvent(new MyEvent(this));
	context.publishEvent(new MyEvent(this));
	TestBean listener = context.getBean(TestBean.class);
	assertEquals(3, ((BeanThatListens) listener.getFriends().iterator().next()).getEventCount());

	context.close();
}
 
Example 13
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 14
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 15
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void innerBeanAsListener() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition listenerDef = new RootBeanDefinition(TestBean.class);
	listenerDef.getPropertyValues().add("friends", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("listener", listenerDef);
	context.refresh();

	context.publishEvent(new MyEvent(this));
	context.publishEvent(new MyEvent(this));
	TestBean listener = context.getBean(TestBean.class);
	assertEquals(3, ((BeanThatListens) listener.getFriends().iterator().next()).getEventCount());

	context.close();
}
 
Example 16
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void listenerAndBroadcasterWithCircularReference() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerBeanDefinition("broadcaster", new RootBeanDefinition(BeanThatBroadcasts.class));
	RootBeanDefinition listenerDef = new RootBeanDefinition(BeanThatListens.class);
	listenerDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("broadcaster"));
	context.registerBeanDefinition("listener", listenerDef);
	context.refresh();

	BeanThatBroadcasts broadcaster = context.getBean("broadcaster", BeanThatBroadcasts.class);
	context.publishEvent(new MyEvent(context));
	assertEquals("The event was not received by the listener", 2, broadcaster.receivedCount);

	context.close();
}
 
Example 17
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void listenerAndBroadcasterWithCircularReference() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerBeanDefinition("broadcaster", new RootBeanDefinition(BeanThatBroadcasts.class));
	RootBeanDefinition listenerDef = new RootBeanDefinition(BeanThatListens.class);
	listenerDef.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("broadcaster"));
	context.registerBeanDefinition("listener", listenerDef);
	context.refresh();

	BeanThatBroadcasts broadcaster = context.getBean("broadcaster", BeanThatBroadcasts.class);
	context.publishEvent(new MyEvent(context));
	assertEquals("The event was not received by the listener", 2, broadcaster.receivedCount);

	context.close();
}
 
Example 18
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void innerBeanAsListener() {
	StaticApplicationContext context = new StaticApplicationContext();
	RootBeanDefinition listenerDef = new RootBeanDefinition(TestBean.class);
	listenerDef.getPropertyValues().add("friends", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("listener", listenerDef);
	context.refresh();

	context.publishEvent(new MyEvent(this));
	context.publishEvent(new MyEvent(this));
	TestBean listener = context.getBean(TestBean.class);
	assertEquals(3, ((BeanThatListens) listener.getFriends().iterator().next()).getEventCount());

	context.close();
}
 
Example 19
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void listenersInApplicationContext() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerBeanDefinition("listener1", new RootBeanDefinition(MyOrderedListener1.class));
	RootBeanDefinition listener2 = new RootBeanDefinition(MyOrderedListener2.class);
	listener2.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("listener1"));
	listener2.setLazyInit(true);
	context.registerBeanDefinition("listener2", listener2);
	context.refresh();
	assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2"));

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyOtherEvent event1 = new MyOtherEvent(context);
	context.publishEvent(event1);
	assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2"));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertTrue(context.getDefaultListableBeanFactory().containsSingleton("listener2"));
	MyEvent event3 = new MyEvent(context);
	context.publishEvent(event3);
	MyOtherEvent event4 = new MyOtherEvent(context);
	context.publishEvent(event4);
	assertTrue(listener1.seenEvents.contains(event1));
	assertTrue(listener1.seenEvents.contains(event2));
	assertTrue(listener1.seenEvents.contains(event3));
	assertTrue(listener1.seenEvents.contains(event4));

	listener1.seenEvents.clear();
	context.publishEvent(event1);
	context.publishEvent(event2);
	context.publishEvent(event3);
	context.publishEvent(event4);
	assertTrue(listener1.seenEvents.contains(event1));
	assertTrue(listener1.seenEvents.contains(event2));
	assertTrue(listener1.seenEvents.contains(event3));
	assertTrue(listener1.seenEvents.contains(event4));

	AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
	assertEquals(2, multicaster.retrieverCache.size());

	context.close();
}
 
Example 20
Source File: ApplicationContextEventTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void listenersInApplicationContext() {
	StaticApplicationContext context = new StaticApplicationContext();
	context.registerBeanDefinition("listener1", new RootBeanDefinition(MyOrderedListener1.class));
	RootBeanDefinition listener2 = new RootBeanDefinition(MyOrderedListener2.class);
	listener2.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("listener1"));
	listener2.setLazyInit(true);
	context.registerBeanDefinition("listener2", listener2);
	context.refresh();
	assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2"));

	MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
	MyOtherEvent event1 = new MyOtherEvent(context);
	context.publishEvent(event1);
	assertFalse(context.getDefaultListableBeanFactory().containsSingleton("listener2"));
	MyEvent event2 = new MyEvent(context);
	context.publishEvent(event2);
	assertTrue(context.getDefaultListableBeanFactory().containsSingleton("listener2"));
	MyEvent event3 = new MyEvent(context);
	context.publishEvent(event3);
	MyOtherEvent event4 = new MyOtherEvent(context);
	context.publishEvent(event4);
	assertTrue(listener1.seenEvents.contains(event1));
	assertTrue(listener1.seenEvents.contains(event2));
	assertTrue(listener1.seenEvents.contains(event3));
	assertTrue(listener1.seenEvents.contains(event4));

	listener1.seenEvents.clear();
	context.publishEvent(event1);
	context.publishEvent(event2);
	context.publishEvent(event3);
	context.publishEvent(event4);
	assertTrue(listener1.seenEvents.contains(event1));
	assertTrue(listener1.seenEvents.contains(event2));
	assertTrue(listener1.seenEvents.contains(event3));
	assertTrue(listener1.seenEvents.contains(event4));

	AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
	assertEquals(2, multicaster.retrieverCache.size());

	context.close();
}