ApplicationContext的事件机制
2018-02-05 00:00
453 查看
ApplicationContext的事件机制是观察者模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationConext的事件处理。如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEven时,ApplicationListener Bean将自动被触发。
Spring的事件框架有如下两个成员:
1)ApplicationEvent:容器事件,必须由ApplicationContext发布。
2)ApplicationListener:监听器,可由容器中的任何监听器承担。
Spring的事件机制与所有事件机制都基本相似,它们都需要事件源、事件和事件监听器组成。此处的事件源是ApplicationConetext,并且事件必须由Java程序显示触发。
onApplicationEvent(ApplictionEvent event);每当容器内发送任何事件时,此方法都被触发;
下面定义一个邮件事件:
在applicationContext-spring.xml中配置邮件事件:
通过ClassPathXmlApplicationContext读取配置文件:
控制台输出:
上文是通过ApplicationContext的pulishEvent()来主动触发容器事件,其实当系统创建Spring容器、加载Spring容器时也会自动触发容器事件。
Spring的事件框架有如下两个成员:
1)ApplicationEvent:容器事件,必须由ApplicationContext发布。
2)ApplicationListener:监听器,可由容器中的任何监听器承担。
Spring的事件机制与所有事件机制都基本相似,它们都需要事件源、事件和事件监听器组成。此处的事件源是ApplicationConetext,并且事件必须由Java程序显示触发。
onApplicationEvent(ApplictionEvent event);每当容器内发送任何事件时,此方法都被触发;
下面定义一个邮件事件:
package com.custle.spring; import org.springframework.context.ApplicationEvent; public class EmailEvent extends ApplicationEvent { private String address; private String text; public EmailEvent(Object source) { super(source); } public EmailEvent(Object source, String address, String text) { super(source); this.address = address; this.text = text; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
package com.custle.spring; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; public class EmailNotifiter implements ApplicationListener { //该方法会在容器发送时自动触发 @Override public void onApplicationEvent(ApplicationEvent event) { if(event instanceof EmailEvent){ //只处理EmailEvent,发送email通知 EmailEvent emailEvent = (EmailEvent)event; System.out.println("需要发送邮件的接收地址:"+emailEvent.getAddress()); }else{ //容器内置事件不作处理 System.out.println("容器本身的事件: "+ event); } } }
在applicationContext-spring.xml中配置邮件事件:
<!-- 配置邮件监听器 --> <bean id="emailNotifiter" class="com.custle.spring.EmailNotifiter" />
通过ClassPathXmlApplicationContext读取配置文件:
package com.custle.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class EmailTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-spring.xml"); EmailEvent emailEvent = new EmailEvent("hello", "526295149@qq.com", "send email"); //将EmailEvent事件添加至ApplicationContext容器 applicationContext.publishEvent(emailEvent); } }
控制台输出:
2018-02-05 13:24:27 INFO (AbstractApplicationContext.java:513) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@22f4bf02: startup date [Mon Feb 05 13:24:27 CST 2018]; root of context hierarchy 2018-02-05 13:24:27 INFO (XmlBeanDefinitionReader.java:316) - Loading XML bean definitions from class path resource [applicationContext-spring.xml] 容器本身的事件: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@22f4bf02: startup date [Mon Feb 05 13:24:27 CST 2018]; root of context hierarchy] 需要发送邮件的接收地址:526295149@qq.com
上文是通过ApplicationContext的pulishEvent()来主动触发容器事件,其实当系统创建Spring容器、加载Spring容器时也会自动触发容器事件。
相关文章推荐
- View事件分发机制
- Android触屏事件分发机制
- Android事件驱动机制--点击事件
- UITouch事件处理机制
- 事件的分发机制
- js事件监听机制(事件捕获)总结
- Nginx的事件处理机制
- java事件处理机制
- 从Listview与Button点击事件冲突看安卓点击事件分发机制
- Android事件分发机制
- Android View深入解析(二)事件分发机制
- 安卓点击事件的分发机制?事件是怎么被分发的?
- View的事件分发机制
- Qt事件机制
- java事件处理机制(自定义事件)
- Miranda IM (1) 事件处理机制
- [Android 分享] View 和 ViewGroup 事件传递机制
- JavaScript事件机制兼容性解决方案
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)
- c#中事件机制学习