spring-第三篇之ApplicationContext的事件机制
1、通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext的事件处理。
如果容器中有一个ApplicationListener bean,当ApplicationContext发布ApplicationEvent时,ApplicationListener bean将自动被触发。
2、spring的事件框架的两个重要成员(即Event、Listener):
1》ApplicationEvent:容器事件,必须由ApplicationContext发布。
2》ApplicationListener:监听器,可由容器中的任何监听器bean担任。
事件机制中的3要素:事件源(ApplicationContext)、事件(Event)、事件监听器(Listener)。
Event事件——>ApplicationContext事件源发布事件——>触发Listener监听器——>监听器执行内部onApplicationEvent(ApplicationEvent e)方法,Event事件作为传入参数。
由上面的流程可知,只需要在容器中注册实现了ApplicationListener的bean,当ApplicationContext发布事件的时候,就会被监听器自动捕获了。
beans.xml注册事件监听器实示例:
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 配置监听器 -->
<bean class="com.lfy.listener.EmailNotifier"/>
</beans>
实现ApplicationListener接口的简单EmailNotifier.java示例代码如下:
package com.lfy.listener; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import com.lfy.event.EmailEvent; /**
* 容器事件的监听器类
* @author lfy
*
*/
public class EmailNotifier implements ApplicationListener {
//该方法会在容器发生事件时自动触发
@Override
public void onApplicationEvent(ApplicationEvent evt) {
//只处理EmailEvent,模拟发送email通知
if(evt instanceof EmailEvent) {
EmailEvent emailEvent=(EmailEvent)evt;
System.out.println("需要发送邮件的接收地址 "+emailEvent.getAddress());
System.out.println("需要发送邮件的邮件正文 "+emailEvent.getText());
}else {
//其他事件不做任何处理
System.out.println("其他事件:"+evt);
}
}
}
继承ApplicationEvent的简单事件java bean举例EmailEvent.java
package com.lfy.event; import org.springframework.context.ApplicationEvent; /**
* ApplicationContext的事件机制
* 只要一个java类继承了ApplicationEvent基类,则该类的对象
* 就可以作为spring容器的容器事件
* @author lfy
*
*/
public class EmailEvent extends ApplicationEvent {
private String address;
private String 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;
} public EmailEvent(Object source) {
super(source);
}
//初始化全部成员变量的构造器
public EmailEvent(Object source,String address,String text) {
super(source);
this.address=address;
this.text=text;
} }
简单的ApplicationContext事件源演示,用于发布事件,触发监听器执行处理:
SpringListenerTest.java
package com.lfy.main; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lfy.event.EmailEvent; public class SpringListenerTest { public static void main(String[] args) {
//创建spring容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
EmailEvent ele=new EmailEvent("test","spring_test@163.com","this is a test");
//发布容器事件
ctx.publishEvent(ele);
} }
执行结果:
注:上面运行结果中的“其他事件”,是因为当系统创建spring容器、加载spring容器、容器销毁时会自动触发容器事件(内置事件),容器事件监听器可以监听到这些事件。ApplicationContext的publishEvent(...)用于主动触发指定Event事件的容器事件。
如果想让bean中业务过程发布指定容器事件,则应该先让bean获得ApplicationContext容器的引用,然后将指定容器事件Event交由ApplicationContext发布。spring-让bean获取所在的容器
3、spring的提供的内置事件:
1》ContextRefreshedEvent:ApplicationContext容器初始化或刷新触发该事件。此处说的初始化,是指所有的bean被成功加载,后处理的bean被检测激活,所有的singleton bean被预初始化,ApplicationContext容器已就绪可用。
2》ContextStartdEvent:当使用ApplicationContext的子接口ConfigurableApplicationContex接口的start()方法启动ApplicationContext容器时触发该事件。容器管理生命周期的bean实例将获得一个指定的启动信号,这在经常需要停止后重新启动的场合比较常见。
3》ContextClossedEvent:当使用ConfigurableApplicationContex接口的close()方法关闭ApplicationContext容器时触发该事件。
4》ContextStoppedEvent:当使用ConfigurableApplicationContex接口的stop()方法使ApplicationContext容器停止时触发该事件 。此处的“停止”意味着,容器管理生命周期的bean实例将获得一个指定的停止信号。被停止的spring容器可以再次通过调用start()方法重新启动。
5》RequestHandledEvent:Web相关的事件,只能应用于使用DispatcherServlet的Web应用中。在使用spring作为前端的MVC控制器时,当spring处理用户请求结束后,系统会自动触发该事件。
spring-第三篇之ApplicationContext的事件机制的更多相关文章
- spring第三篇
在昨天下午更新sprin第二篇中,叙述了将对象交给spring创建和管理,今天在spring第三篇中,主要写两个点一是spring的思想 二是spring中bean元素的属性配置. 1 spring思 ...
- Spring ApplicationContext的事件机制
ApplicationContext的事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationCon ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- Spring第三篇【Core模块之对象依赖】
前言 在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容 ...
- 初学Java ssh之Spring 第三篇
在这篇中,我学习了依赖注入的两种方式:设值注入和构造注入. 在我们以前的思维中,如果调用一个类时,我们都需要将其手动实例化,当我们创建被调用的工作不需要我们完成时,这就是控制反转,当这个将被调用的实例 ...
- SSH框架之Spring第三篇
1.1 AOP概述 1.1.1 什么是AOP? AOP : 全称是Aspect Oriented Progamming既 : 面向切面编程.通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技 ...
- 【第三篇】学习 android 事件总线androidEventbus之发布事件,子线程中接收
发送和接收消息的方式类似其他的发送和接收消息的事件总线一样,不同的点或者应该注意的地方: 1,比如在子线程构造方法里面进行实现总线的注册操作: 2,要想子线程中接收消息的功能执行,必须启动线程. 3, ...
- 【第三篇】学习 android 事件总线androidEventbus之list数据事件的传递,发送list数据事件到另外一个Activity
这个和普通的事件总线的发送接收一样. package com.example.mysimpleeventbus; import java.util.ArrayList; import java.uti ...
- spring(三):ApplicationContext
随机推荐
- ][mybatis]MyBatis mapper文件中的变量引用方式#{}与${}的差别
转自https://blog.csdn.net/szwangdf/article/details/26714603 MyBatis mapper文件中的变量引用方式#{}与${}的差别 默认情况下,使 ...
- .NET Reactor使用教程(加密源代码示例)
更多:https://www.cnblogs.com/PiaoMiaoGongZi/category/1120300.html 1.打开 Eziriz .NET Reactor,主界面如图1所示: 图 ...
- openstack stein部署手册 9. neutron
# 安装程序包 yum -y install openstack-neutron-linuxbridge ebtables ipset # 变更配置文件 mv /etc/neutron/neutron ...
- Oracle单引号转义符
作用:Increase readability and usability (增加可读性和可用性) 用法:select q'[ select * from ]'||table_name|| ';' ...
- mysql 8.0.13 zip windows 10安装
1.下载安装包 https://dev.mysql.com/downloads/mysql/ 下载后解压到D:\Program Files\mysql-8.0.13-winx64 2.添加配置文件my ...
- JavaWeb(七):EL表达式、自定义标签和JSTL
一.EL表达式 语法 el.jsp <%@page import="java.util.Date"%> <%@page import="com.atgu ...
- 【leetcode】1054. Distant Barcodes
题目如下: In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i]. Rearrange t ...
- javascript实现表单提交加密
javascript实现表单提交加密 通常表单的提交有两种方式,一是直接通过html的form提交,代码如下: <form action="" method="&q ...
- UE4 中的Blutilities
该功能是为编辑器中的简单扩展功能而设置的. 一般而言用蓝图在编辑器中做功能扩展都会用到Construction Script,但该功能有一些缺陷: 首先在actor发生任何变化(包括Transform ...
- 27 August
高精度 struct bigint{ int a[1000],an; bigint operator = (int b){ an=0; while (b){a[an++]=b%10;b/=10;} r ...