【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 的总结和学习 ...
随机推荐
- Invalid prop: type check failed for prop "xxx". Expected Number, got String.
在子组件progress-circle.vue的template中的定义如下: <svg :width="radius" :height="radius" ...
- Java连接redis之Jedis使用
测试联通 创建Maven工程,引入依赖 <dependency> <groupId>redis.clients</groupId> <artifactId&g ...
- python unicode escape
from: https://stackoverflow.com/questions/44742806/how-to-remove-escape-characters-escaping-unicode- ...
- [cf461D]Appleman and Complicated Task
假设该矩形是aij,那么有a(i,j)=a(i-1,j-1)^a(i-1,j+1)^a(i-2,j),不断递归下去可以发现a(i,j)=a(1,y-x+1)^a(1,y-x+3)^--^a(1,x+y ...
- [noi706]Sabotage
先可以将所有出度为0的节点连向一个点,然后问题变为求到这个点的必经之点这其实是一道模板题,因为有一个东西叫做支配树容易发现一个点的必经之点都是一条链,其实可以把这条链上最浅的点作为这个点的父亲,那么一 ...
- 解决 IDEA 2021.2.3 新建maven项目只有两个archetype项目模板的问题
最近把我的 IDEA 版本更新到 2021.2.3 了,发生了一个比较有意思的问题,做个小小的记录 思路分析 在新的 IDEA 中配置完Maven之后,想要创建Maven项目的时候没有自动加载arch ...
- 详解如何用 CSS3 完成 3D transform变换
Tips:阅读提示!!! 首先,本文针对的是3D transform变换的学习,所以你需要对 2D transform变换 有一定的了解 其次,需要说明的是,代码是一种需要自己不断实践的学科,建议各位 ...
- [JSC2021 A~D + F]
半小时打完了\(A~D\),想要一发\(F\)冲进前\(100\),结果平衡树常数大\(T\)了.据说\(G\)是矩阵树定. \(A\) 放代码吧. A // code by Dix_ #includ ...
- Codeforces 251D - Two Sets(异或方程组)
题面传送门 题意: 你有一个可重集 \(S=\{a_1,a_2,\dots,a_n\}\),你要把它划分成两个可重集 \(S_1,S_2\) 使得 \(S\) 中每个元素都恰好属于 \(S_1\) 与 ...
- CF1208H Red Blue Tree
CF1208H Red Blue Tree 原本应该放在这里但是这题过于毒瘤..单独开了篇blog 首先考虑如果 $ k $ 无限小,那么显然整个树都是蓝色的.随着 $ k $ 逐渐增大,每个点都会有 ...