ApplicationContext的事件机制
来源:互联网 发布:淘宝借钱逾期 编辑:程序博客网 时间:2024/05/17 08:42
一、概述
applicationContext的事件机制是观察者模式实现的,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext的事件处理。
- ApplicationEvent:容器事件,必须由ApplicationContext发布
- ApplicationListener:监听器,可由容器中任何监听器Bean担任
二、简单示例代码说明
1.容器事件
package com.event;import org.springframework.context.ApplicationEvent;/** * 继承了ApplicationEvent,那该对象可作为Spring容器对象 */public class EmailEvent extends ApplicationEvent{ private String address; private String text; public EmailEvent(Object source) { super(source); // TODO Auto-generated constructor stub } 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; }}
2.容器事件监听器
package com.event;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;public class EmialNotifier implements ApplicationListener<ApplicationEvent>{ public void onApplicationEvent(ApplicationEvent event) { // TODO Auto-generated method stub if(event instanceof EmailEvent){//instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例 EmailEvent emailEvent=(EmailEvent) event; System.out.println("需要发送邮件的接收地址为:"+emailEvent.getAddress()); System.out.println("需要发送邮件的正文为:"+emailEvent.getText()); }else{ //其他时间不做任何处理 System.out.println("其他事件:"+event); } }}
3.配置文件
<!-- 配置事件监听器 --><bean class="com.event.EmialNotifier"/>
4.测试代码
package com.event;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class EventTest { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); //创建一个applicationEvent对象 EmailEvent emailEvent=new EmailEvent("text", "广东省茂名市", "spring_event@qq.com"); //发布事件 ctx.publishEvent(emailEvent); }}输出如下结果:其他事件:org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@78105738: startup date [Tue Aug 29 23:45:15 CST 2017]; root of context hierarchy]需要发送邮件的接收地址为:广东省茂名市需要发送邮件的正文为:spring_event@qq.com由此可以看出,监听器不仅能够监听程序所触发的事件,也能监听到内置的事件
四、内置事件简述
package com.bjhy.platform.supermarket.test;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;/** * Spring boot 启动容器加载完成之后,做一系列的事情可以实现ApplicationListener * Spring的内置事件: * 1.ContextRefreshedEvent:ApplicationContext容器初始化或者刷新时触发该事件。 * 2、 ContextStartedEvent:当使用ConfigurableApplicationContext接口的start()方法启动 ApplicationContext容器时触发该事件。 3、 ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发该事件。 4、 ContextStopedEvent: 当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发该事件。 5.RequestHandleEvent:Web相关的事件,只能应用于使用DispatherServlet的Web中,在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会触发改事件 * * */public class ApplicationRequestHandleEvent implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { ApplicationContext context = event.getApplicationContext(); //执行操作code }}
阅读全文
0 0
- ApplicationContext的事件机制
- ApplicationContext的事件机制
- ApplicationContext的事件机制
- ApplicationContext的事件机制
- 二、ApplicationContext的事件机制
- Spring---ApplicationContext的事件机制
- 5.1. ApplicationContext的事件机制
- Spring中ApplicationContext的事件机制
- Spring中ApplicationContext的事件机制 .
- Spring中ApplicationContext的事件机制
- Spring中ApplicationContext的事件机制
- Spring中ApplicationContext的事件机制
- ApplicationContext事件机制
- Spring ApplicationContext事件机制
- Spring-ApplicationContext事件机制
- Spring ApplicationContext事件机制
- Spring中ApplicationContext的事件机制(三 自定义事件)
- 《Pro Spring》学习笔记之ApplicationContext的事件机制
- Tomcat系列—基于端口的虚拟主机配置
- RecyclerView的简单使用
- 【2017年最全】 iOS面试题及答案
- MySQL B+树索引和Hash索引的区别
- Spark学习笔记(24)StreamingContext及JobScheduler源码图解
- ApplicationContext的事件机制
- spring boot入门(三) 热部署
- ubuntu16.04安装jdk以及详细jdk环境配置教程,亲测没毛病。
- Spark学习笔记(25)Spark Streaming的JobGenerator源码图解
- log4j怎样配置才能打印出执行语句所在的类名及行号
- linux 编译Jrtplib
- Pat(A) 1076. Forwards on Weibo (30)
- Spark学习笔记(26)在DStream的Action操作之外也可能产生Job操作
- 坚持了半个月刷CCF的算法题(第十四天)