【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 的总结和学习 ...
随机推荐
- 1.在项目中使用D3.js
在项目中使用D3.js D3.js(全称:Data-Driven Documents)是一个基于数据操作文档的JavaScript库.D3帮助您使用HTML.SVG和CSS使数据生动起来.D3对web ...
- fabric运行错误汇总
Error generating signCA for org org1.example.com: Failed storing key [ECDSAP256]: Failed storing ECD ...
- Java 初始化与清理
用构造器确保初始化 如何自定义构造器(constructor)? 构造器方法的名称与类名相同,并且没有返回值. 需要注意,在定义构方法时,方法名前面不要添加任何的类型说明符,格式:类名(){},构造方 ...
- ipython和pip,模块安装方法
先下载 pip-.tar.gz 解压文件 cmd进入这个加压后的文件 执行 python setup.py install 然后配置环境变量 把 python 下的 Scripts 文件目录添加到 P ...
- IDEA下载 使用快捷方式 以及一些小教程
IDEA下载 使用快捷方式 以及一些小教程 Idea下载 网址:链接: https://pan.baidu.com/s/1xRr3mhM6_VDHqC_w0F1MjQ 提取码: 6ypi 下载,安装方 ...
- 13-Semi-supervised Learning
半监督学习(semi-supervised learning) 1.introduction 2.Semi-supervised Learning for Generative Model 3.Low ...
- 【Azure 应用服务】App Service 无法连接到Azure MySQL服务,报错:com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
问题描述 App Service使用jdbc连接MySQL服务,出现大量的 Communications link failure: com.mysql.cj.jdbc.exceptions.Com ...
- 《重学Java高并发》Sempahore的使用场景与常见误区
大家好,我是威哥,<RocketMQ技术内幕>一书作者,荣获RocketMQ官方社区优秀布道师.CSDN2020博客执之星Top2等荣誉称号.目前担任中通快递技术平台部资深架构师,主要负责 ...
- 【R】行或列数目不同的两个数据框如何用rbind/cbind合并?
目录 前言 方法一:dplyr的bind_rows 方法二:plyr的rbind.fill 前言 通常我们用rbind和cbind合并相同行列的数据框.当两个数据框具有不同行列数目时,直接用会报错. ...
- 基于PASA进行基因预测
PASA, acronym for Program to Assemble Spliced Alignments, is a eukaryotic genome annotation tool tha ...