【Spring Framework】Spring入门教程(七)Spring 事件
内置事件
Spring中的事件是一个ApplicationEvent类的子类,由实现ApplicationEventPublisherAware接口的类发送,实现ApplicationListener接口的类监听。
ApplicationContext 事件
Spring中已经定义了一组内置事件,这些事件由ApplicationContext容器发出。
例如,ContextStartedEvent在ApplicationContext启动时发送,ContextStoppedEvent在ApplicationContext停止时发送。
实现ApplicationListener的类可以监听事件。
Spring的事件是同步的(单线程的),会被阻塞。
监听ApplicationContext事件
要监听ApplicationContext事件,监听类应该实现ApplicationListener接口并重写onApplicationEvent()方法。
ContextStartEventHandler.java
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class ContextStartEventHandler implements ApplicationListener<ContextStartedEvent>{
@Override
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ApplicationContext 启动... ");
}
}
Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// fire the start event.
// ((ConfigurableApplicationContext) context).start();
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// fire the start event.
context.start();
// ...
}
}
在XML配置文件中,将该类为声明为Bean,以便Spring容器加载该Bean,并向其传送事件。
<bean id="contextStartEventHandler" class="ContextStartEventHandler"></bean>
自定义事件
除了内置事件,Spring中也可以使用自定义事件。
怎样使用自定义事件:
- 创建事件类 – 扩展
ApplicationEvent类,创建事件类。 - 创建发送类 – 发送类获取
ApplicationEventPublisher实例发送事件。 - 创建监听类 – 实现
ApplicationListener接口,创建监听类。
事件类
事件类用于存储事件数据。下面创建一个简单的事件类。
CustomEvent.java
import org.springframework.context.ApplicationEvent;
public class CustomEvent extends ApplicationEvent {
public CustomEvent(Object source, String message) {
super(source);
}
public String toString() {
return "我是自定义事件";
}
}
发送类
发送类创建事件对象并发送。
要发送事件,这里介绍2种方法:
- 使用
@autowired注解注入ApplicationEventPublisher实例。
CustomEventPublisher.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
public class CustomEventPublisher {
@Autowired
private ApplicationEventPublisher publisher;
public void publish() {
CustomEvent event = new CustomEvent(this);
publisher.publishEvent(event);
}
}
- 发送类实现
ApplicationEventPublisherAware接口,获取ApplicationEventPublisher实例。
CustomEventPublisher.java
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
public class CustomEventPublisher implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;
// 必须重写这个方法获取ApplicationEventPublisher
public void setApplicationEventPublisher (ApplicationEventPublisher publisher){
this.publisher = publisher;
}
public void publish() {
CustomEvent event = new CustomEvent(this);
publisher.publishEvent(event);
}
}
如果发送类实现了ApplicationEventPublisherAware接口,发送类必须声明为bean,Spring容器将其标识为事件发送者。
<bean id="customEventPublisher" class="CustomEventPublisher"/>
监听类
监听类监听事件。监听类必须实现ApplicationListener接口,并且被定义为Bean以便Spring容器可以加载它。
beans.xml
<bean id="customEventHandler" class="CustomEventHandler"/>
CustomEventHandler.java
import org.springframework.context.ApplicationListener;
public class CustomEventHandler implements ApplicationListener<CustomEvent> {
public void onApplicationEvent(CustomEvent event) {
System.out.println("收到事件:" + event.toString());
}
}
运行
测试自定义事件。
Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
CustomEventPublisher publisher = (CustomEventPublisher) context.getBean("customEventPublisher");
publisher.publish();
}
}
【Spring Framework】Spring入门教程(七)Spring 事件的更多相关文章
- Spring Boot2 快速入门教程-到上手
Spring Boot2 教程合集 入门 纯 Java 代码搭建 SSM 环境 创建一个 Spring Boot 项目的三种方法 理解 Spring Boot 项目中的 parent 基础配置 配置文 ...
- spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包
下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...
- CPF 入门教程 - 属性和事件(七)
CPF C#跨平台桌面UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) CPF 入门教程 - 样式和动画(三) CPF 入门教程 - 绘图(四) CPF 入门 ...
- Caliburn.Micro 杰的入门教程4,事件聚合器
Caliburn.Micro 杰的入门教程1(原创翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(原创翻译)Caliburn.Micro 杰的入门 ...
- 无废话ExtJs 入门教程七[登陆窗体Demo:Login]
无废话ExtJs 入门教程七[登陆窗体Demo:Login] extjs技术交流,欢迎加群(201926085) 在这节我们通过前几节讲的内容做一个登陆页面,把前几节讲的内容贯穿一下. 1.代码如下: ...
- Caliburn.Micro 杰的入门教程3,事件和参数
Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...
- RabbitMQ入门教程(七):主题交换机Topics
原文:RabbitMQ入门教程(七):主题交换机Topics 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog. ...
- Spring Cloud 入门教程(七): 熔断机制 -- 断路器
对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...
- Spring Framework 学习笔记——核心技术之Spring IOC
Spring Framework 官网文档学习笔记--核心技术之Spring IOC 官方文档 spring-framework-5.3.9 1. Spring Framework 核心技术 1.1 ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
随机推荐
- 【Java】 List和Array转换
List转Array toArray 首先展示初学者容易犯的错误示例 List<String> strList = new ArrayList<>(); strList.add ...
- SpringBoot 居然有 44 种应用启动器
啥是应用启动器?SpringBoot集成了spring的很多模块,比如tomcat.redis等等.你用SpringBoot搭建项目,只需要在pom.xml引入相关的依赖,和在配置文件中简单的配置就可 ...
- K8S发布策略,无损发布
大家好,相信大部分公司都已经使用K8S进行容器管理和编排了,但是关于K8S的发布策略,还有很多同学不太清楚,通过这篇文章的介绍,相信大家对目前K8S的发布情况有一个概括的认识.总结下来,共有如下几种: ...
- SpringCloud config native 配置
1.概述 最近项目使用springCloud 框架,使用config搭建git作为配置中心. 在私有化部署中,出现很多比较麻烦的和鸡肋的设计. 每次部署都需要安装gitlab 有些环境安装完gitla ...
- React-Router基础(<HashRouter>)
<Router>使用URL(即window.location.hash)的哈希部分来保持UI与URL同步的A. 重要说明:哈希历史记录不支持location.key或location.st ...
- 大爽Python入门教程 3-4 实践例题
大爽Python入门公开课教案 点击查看教程总目录 1. 求和 使用循环,计算列表所有项的和,并输出这个和. 列表示例 lst = [8, 5, 7, 12, 19, 21, 10, 3, 2, 11 ...
- liunx基础知识点2:文件操作命令、系统资源查询、权限赋予命令、安装命令、解压命令
- Spark面试题(七)——Spark程序开发调优
Spark系列面试题 Spark面试题(一) Spark面试题(二) Spark面试题(三) Spark面试题(四) Spark面试题(五)--数据倾斜调优 Spark面试题(六)--Spark资源调 ...
- [bzoj1741]穿越小行星群
将每一行/每一列作为一个点,对于一个障碍(x,y),要么第x行和第y列的状态(是否攻击)只需要有一个就可以了,将第x行和第y列连边,就是二分图的最小点覆盖=最大匹配数. 1 #include<b ...
- HTML四种常见的定位-相对定位
相对定位 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset=&q ...