Spring中ApplicationContext的事件机制
2016-08-24 15:11
260 查看
ApplicationContext事件机制是观察者设计模式的 实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。 如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发。 Spring的事件框架有如下两个重要的成员: ApplicationEvent:容器事件,必须由ApplicationContext发布; ApplicationListener:监听器,可由容器中的任何监听器Bean担任。 实际上,Spring的事件机制与所有时间机制都基本相似,它们都需要事件源、事件和事件监听器组成。只是此处的事件源是 ApplicationContext,且事件必须由Java程序显式触发。下面的程序将演示Spring容器的事件机制。 Spring框架事先定义了一个 ApplicationEvent类,其对象就是一个Spring容器事件,其源码中的解释是:Class to be extended by all application events. Abstract as itdoesn't make sense for generic events to be published directly.
代码如下:
package com.cxg.test.springPlatfrom; import org.springframework.context.ApplicationEvent; /** * Title: email之事件类 * EmailEvent类继承了ApplicationEvent类,除此之外,它就是一个普通的Java类 * Description: dataPlatfrom * @author: xg.chen * @date:2016年8月24日 */ public class EmailEvent extends ApplicationEvent{ private static final long serialVersionUID = 1L; //属性 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; } //getter和setter设置 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; } }
上面的EmailEvent类继承了ApplicationEvent类,除此之外,它就是一个普通的Java类。 容器事件的监听器类必须实现ApplicationListener接口,实现该接口就必须实现如下方法:onApplicationEvent(ApplicationEvent event):每当容器内发生任何事件时,此方法都会被触发。在Spring源码中给出的解释是:Interface to be implemented by application event listeners. Based on the standard {@code java.util.EventListener} interface for the Observer design pattern.
容器监听器类代码如下:
package com.cxg.test.springPlatfrom; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; /** * Title: email之监听类 * 容器事件的监听器类必须实现ApplicationListener接口,实现该接口就必须实现 * Description: dataPlatfrom * @author: xg.chen * @date:2016年8月24日 */ public class EmailNotifier implements ApplicationListener<ApplicationEvent>{ @Override public void onApplicationEvent(ApplicationEvent event) { if(event instanceof EmailEvent){ EmailEvent emailEvent = (EmailEvent) event; System.out.println("email's address:"+emailEvent.getAddress()); System.out.println("email's text:"+emailEvent.getText()); } else { System.out.println("the Spring's event:"+event); } } }
将监听器配置在Spring的容器中,代码如下:
<!-- 配置事件监听 --> <bean class="com.cxg.test.springPlatfrom.EmailNotifier" />
为Spring容器注册监听器,不需要像AWT编程那样采用代码进行注册,只需要在Spring的配置文件中进行简单配置即可。当我们住唉 Spring中配置了一个实现了ApplicationListener的Bean,Spring容器就会把这个Bean当成容器事件的监听器。当系统创建Spring容器、加载Spring容器时会自动触发容器事件,容器事件监听器可以监听到这些事件。除此之外,程序也可以调用ApplicationContext的publishEvent()方法来主动触发一个容器事件。
测试代码如下:
package com.cxg.test.springPlatfrom; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Title: Spring的ApplicationContexet单元成测试 * Description: dataPlatfrom * @author: xg.chen * @date:2016年8月24日 */ public class SpringTest { public static void main(String arg[]){ //读取Spring容器的配置文件 @SuppressWarnings("resource") ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml"); //创建一个事件对象 EmailEvent emailEvent = new EmailEvent("hello Spring!", "cxg@126.com", "This is SpringApplicatoinContext test!"); //主动触发事件监视机制 applicationContext.publishEvent(emailEvent); } }
运行输出:
email's address:cxg@126.com email's text:This is SpringApplicatoinContext test!
注意:如果Bean想发布事件,则Bean必须获得其容器的引用。如果程序中没有直接获取容器的引用,则应该让Bean实现ApplicationContextAware或者BeanFactoryAware接口,从而可以获得容器的引用。 Spring提供如下几个内置事件: ContextRefreshedEvent:ApplicationContext容器初始化或刷新时触发该事件。此处的初始化是指:所有的 Bean被成功装载,后处理Bean被检测并激活,所有Singleton Bean 被预实例化,ApplicationContext容器已就绪可用。 ContextStartedEvent:当使用 ConfigurableApplicationContext(ApplicationContext的子接口)接口的start()方法启动 ApplicationContext容器时触发该事件。容器管理声明周期的Bean实例将获得一个指定的启动信号,这在经常需要停止后重新启动的场合比 较常见。 ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext时触发该事件。 ContextStoppedEvent:当使用ConfigurableApplicationContext接口的stop()方法使 ApplicationContext容器停止时触发该事件。此处的停止,意味着容器管理生命周期的Bean实例将获得一个指定的停止信号,被停止的 Spring容器可再次调用start()方法重新启动。 RequestHandledEvent:Web相关事件,只能应用于使用DispatcherServlet的Web应用。在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会自动触发该事件。
相关文章推荐
- [导入]Spring中ApplicationContext的事件机制(二 内定事件)
- Spring中的IOC(四):IOC中其他接口的使用及Spring的事件处理机制(监听机制)
- Spring4.1新特性——异步调度和事件机制的异常处理
- 【Java EE (Struts2 + Spring + Hibernate)开发】 :Hibernate(二)之【数据过滤|事务控制|二级缓存和查询缓存|事件机制】
- 一起学spring--spring事件机制--监听器
- Spring学习-webcontex的自动配置和事件传播机制及使用
- Spring4.1新特性——异步调度和事件机制的异常处理
- Spring ApplicationContext事件机制
- Spring---ApplicationContext的事件机制
- Spring进阶之路(2)-ApplicationContext容器以及事件机制
- Spring中ApplicationContext的事件机制
- 深入浅出Spring事件机制
- spring中事件机制
- spring的事件机制
- 7.4.4: 使用spring容器---ApplicationContext的事件机制
- [导入]Spring中ApplicationContext的事件机制(三 自定义事件)
- 深入浅出Spring事件机制
- Spring ApplicationContext的事件机制
- 15、Spring事件机制_观察者模式
- Spring进阶之路(2)-ApplicationContext容器以及事件机制