Spring事件(Application Event)
2017-08-02 21:19
561 查看
Spring的事件(Application Event)为Bean与Bean之间的通信提供了支持。当一个Bean处理完一个任务时,希望另一个Bean知道并能做相应的处理,这时我们就需要让另外一个Bean监听当前所发送的事件。
Demo:
自定义事件DemoEvent.java 继承ApplicationEvent
自定义事件监听器DemoEventListener.java 实现ApplicationListener接口,并指定监听的事件类型,使用onApplicationEvent方法对消息进行接受处理
事件发布类DemoEventPublisher.java
测试类TestEventService.java
运行结果:
Demo:
自定义事件DemoEvent.java 继承ApplicationEvent
package com.example.mavenspringmvc.event; import org.springframework.context.ApplicationEvent; /** * 定义一个事件,继承ApplicationEvent */ public class DemoEvent extends ApplicationEvent{ private String eventMsg; public DemoEvent(Object source, String eventMsg) { super(source); this.eventMsg = eventMsg; } public String getEventMsg() { return eventMsg; } public void setEventMsg(String eventMsg) { this.eventMsg = eventMsg; } }
自定义事件监听器DemoEventListener.java 实现ApplicationListener接口,并指定监听的事件类型,使用onApplicationEvent方法对消息进行接受处理
package com.example.mavenspringmvc.event; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; @Component public class DemoEventListener implements ApplicationListener<DemoEvent>{ @Override public void onApplicationEvent(DemoEvent demoEvent) { String msg = demoEvent.getEventMsg(); System.out.println("我收到了消息:" + msg); } }
事件发布类DemoEventPublisher.java
package com.example.mavenspringmvc.event; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; /** * 事件发布类,需使用spring容器来发布事件,即ApplicationContext来发布 */ @Component public class DemoEventPublisher { @Autowired private ApplicationContext applicationContext; public void publishDemoEvent(String eventMsg){ applicationContext.publishEvent(new DemoEvent(this,eventMsg)); } }
测试类TestEventService.java
package com.example.mavenspringmvc.event; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class TestEventService { @Autowired private DemoEventPublisher demoEventPublisher; public void publishEvent(){ demoEventPublisher.publishDemoEvent("hello application event"); } }
运行结果:
相关文章推荐
- Spring4 事件(ApplicationEvent)
- Spring 通过ApplicationListener&ApplicationEvent实现事件监听
- Spring Boot 2.0 新特性(二):新增事件ApplicationStartedEvent
- spring 事件(Application Event)
- spring事件机制-onApplicationEvent执行两次
- Spring 事件(Application Event)
- Spring 的 ApplicationEvent and ApplicationListener 、spring框架加载完成后执行上下文刷新事件(ContextRefreshedEvent)
- 【Spring】Spring常用配置-事件(Application Event)
- Spring中使用ApplicationContext进行事件的监听之ApplicationEvent,ApplicationListener
- QT多线程及通过事件进行通信(通过自定义事件,然后QApplication::postEvent给主界面,我之前用的是信号槽)
- #611 – 在退出事件中设置退出码(Set Application Exit Code in Exit Event Handler)
- springboot的ApplicationReadyEvent
- Spring Application Event
- Oracle Internal Event:10201 consistent read undo application诊断事件
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- Spring中的onApplicationEvent
- spring @EventListener 事件与监听
- java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener ----good
- 当spring 容器初始化完成后执行某个方法 防止onApplicationEvent方法被执行两次
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)