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

The following examples show how to use org.springframework.context.support.GenericApplicationContext#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 spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example 2
Source File: CustomizedSpringEventDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    GenericApplicationContext context = new GenericApplicationContext();

    // 1.添加自定义 Spring 事件监听器
    // ListenerRetriever -> 0 .. N 个 ApplicationListener<MySpringEvent> 实例
    // MySpringEvent 以及它子孙类
    context.addApplicationListener(new MySpringEventListener());

    context.addApplicationListener(new ApplicationListener<ApplicationEvent>() {

        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            System.out.println("Event : " + event);
        }
    });

    // 2.启动 Spring 应用上下文
    context.refresh();

    // 3. 发布自定义 Spring 事件
    // ListenerCacheKey -> MySpringEvent
    context.publishEvent(new MySpringEvent("Hello,World"));
    context.publishEvent(new MySpringEvent2("2020"));

    // 4. 关闭 Spring 应用上下文
    context.close();
}
 
Example 3
Source File: ApplicationContextEventTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}
 
Example 4
Source File: ApplicationListenerBeanOnCustomEventBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // 创建 Spring 应用上下文 GenericApplicationContext
    GenericApplicationContext context = new GenericApplicationContext();
    // 注册 ApplicationListener<MyApplicationEvent> 实现 MyApplicationListener
    context.registerBean(MyApplicationListener.class); // registerBean 方法从 Spring 5 引入
    // 初始化上下文
    context.refresh();
    // 发布自定义事件 MyApplicationEvent
    context.publishEvent(new MyApplicationEvent("Hello World"));
    // 关闭上下文
    context.close();
    // 再次发布事件
    context.publishEvent(new MyApplicationEvent("Hello World Again"));
}
 
Example 5
Source File: ApplicationContextEventTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void beanPostProcessorPublishesEvents() {
	GenericApplicationContext context = new GenericApplicationContext();
	context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
	context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
	context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
	context.refresh();

	context.publishEvent(new MyEvent(this));
	BeanThatListens listener = context.getBean(BeanThatListens.class);
	assertEquals(4, listener.getEventCount());

	context.close();
}