Spring 事件(Application Event)
2017-03-14 14:42
561 查看
Spring的事件(Application Event)为Bean与Bean之间的消息通信提供了支持。当一个Bean处理完一个任务后,希望从另一个知道并做相应处理,这个时候我们就需要让另一个Bean监听当前Bean所发送的事件。
Spring的事件需要遵循如下流程:
自定义事件,继承ApplicationEvent。
定义事件监听器,实现ApplicationListener。
使用容器发布事件。
(1)自定义事件:
(2)定义事件监听器:
(3)使用容器发布事件:
(4)配置类:
(5)测试类:
(6)结果:
Spring的事件需要遵循如下流程:
自定义事件,继承ApplicationEvent。
定义事件监听器,实现ApplicationListener。
使用容器发布事件。
(1)自定义事件:
package com.chenfeng.xiaolyuh.event.custom_event; import org.springframework.context.ApplicationEvent; /** * * @ClassName DemoEvent * @Description 自定义事件 * @author yuhao.wang * @Date 2017年3月14日 下午1:54:57 * @version 1.0.0 */ public class DemoEvent extends ApplicationEvent { private static final long serialVersionUID = 1L; private String msg; public DemoEvent(Object source, String msg) { super(source); this.msg = msg; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
(2)定义事件监听器:
package com.chenfeng.xiaolyuh.event.listener; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import com.chenfeng.xiaolyuh.event.custom_event.DemoEvent; /** * * @ClassName DemoListener * @Description 事件监听器,实现ApplicationListener的接口并指定监听的事件类型。 * @author yuhao.wang * @Date 2017年3月14日 下午1:59:50 * @version 1.0.0 */ @Component public class DemoListener implements ApplicationListener<DemoEvent> { @Override // 使用onApplicationEvent方法对消息进行接收处理。 public void onApplicationEvent(DemoEvent event) { String msg = event.getMsg(); System.out.println("我(bean-demoListener)接收到了Bean-demoPublisher发布的消息:" + msg); } }
(3)使用容器发布事件:
package com.chenfeng.xiaolyuh.event.publisher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; import com.chenfeng.xiaolyuh.event.custom_event.DemoEvent; /** * * @ClassName DemoPublisher * @Description 发布事件 * @author yuhao.wang * @Date 2017年3月14日 下午2:14:20 * @version 1.0.0 */ @Component public class DemoPublisher { @Autowired // 注入容器applicationContext用来发布事件 ApplicationContext applicationContext; public void publish(String msg){ // 使用applicationContext的publishEvent方法来发布。 applicationContext.publishEvent(new DemoEvent(this, msg)); } }
(4)配置类:
package com.chenfeng.xiaolyuh.event.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration // 声明当前类是一个配置类,相当于Spring配置的XML文件 @ComponentScan(basePackages={"com.chenfeng.xiaolyuh.event"}) public class EventConfig { }
(5)测试类:
package com.chenfeng.xiaolyuh.test; import org.junit.After; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.chenfeng.xiaolyuh.event.config.EventConfig; import com.chenfeng.xiaolyuh.event.publisher.DemoPublisher; /** * Created by yuhao.wang on 2017/3/9. */ public class SpringEventTest { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class); @Test public void contextTest() { DemoPublisher demoPublisher = context.getBean(DemoPublisher.class); demoPublisher.publish("Hello Application Event"); } @After public void closeContext() { context.close(); } }
(6)结果:
三月 14, 2017 2:43:06 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ccd43c2: startup date [Tue Mar 14 14:43:06 CST 2017]; root of context hierarchy 我(bean-demoListener)接收到了Bean-demoPublisher发布的消息:Hello Application Event 三月 14, 2017 2:43:06 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose 信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ccd43c2: startup date [Tue Mar 14 14:43:06 CST 2017]; root of context hierarchy
相关文章推荐
- Spring中使用ApplicationContext进行事件的监听之ApplicationEvent,ApplicationListener
- Spring 的 ApplicationEvent and ApplicationListener 、spring框架加载完成后执行上下文刷新事件(ContextRefreshedEvent)
- Spring4 事件(ApplicationEvent)
- Spring Boot 2.0 新特性(二):新增事件ApplicationStartedEvent
- 【Spring】Spring常用配置-事件(Application Event)
- Spring事件(Application Event)
- Spring 通过ApplicationListener&ApplicationEvent实现事件监听
- spring 事件(Application Event)
- spring事件机制-onApplicationEvent执行两次
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- spring容器加载完毕干一件事情(利用ContextRefreshedEvent事件)
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent and ApplicationListener
- 通过ServletContextEvent加载spring的application.xml
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- Spring容器加载完毕干一件事情(利用ContextRefreshedEvent事件)
- Spring InitializingBean和ApplicationListener<ContextRefreshedEvent>
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- 当spring 容器初始化完成后执行某个方法 防止onApplicationEvent方法被执行两次