内置事件

Spring中的事件是一个ApplicationEvent类的子类,由实现ApplicationEventPublisherAware接口的类发送,实现ApplicationListener接口的类监听。

ApplicationContext 事件

Spring中已经定义了一组内置事件,这些事件由ApplicationContext容器发出。

例如,ContextStartedEventApplicationContext启动时发送,ContextStoppedEventApplicationContext停止时发送。

实现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种方法:

  1. 使用@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);
}
}
  1. 发送类实现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 事件的更多相关文章

  1. Spring Boot2 快速入门教程-到上手

    Spring Boot2 教程合集 入门 纯 Java 代码搭建 SSM 环境 创建一个 Spring Boot 项目的三种方法 理解 Spring Boot 项目中的 parent 基础配置 配置文 ...

  2. spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包

    下载spring http://spring.io/ 最重要是在特征下面的这段话,需要注意: All avaible features and modules are described in the ...

  3. CPF 入门教程 - 属性和事件(七)

    CPF C#跨平台桌面UI框架 系列教程 CPF 入门教程(一) CPF 入门教程 - 数据绑定和命令绑定(二) CPF 入门教程 - 样式和动画(三) CPF 入门教程 - 绘图(四) CPF 入门 ...

  4. Caliburn.Micro 杰的入门教程4,事件聚合器

    Caliburn.Micro 杰的入门教程1(原创翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(原创翻译)Caliburn.Micro 杰的入门 ...

  5. 无废话ExtJs 入门教程七[登陆窗体Demo:Login]

    无废话ExtJs 入门教程七[登陆窗体Demo:Login] extjs技术交流,欢迎加群(201926085) 在这节我们通过前几节讲的内容做一个登陆页面,把前几节讲的内容贯穿一下. 1.代码如下: ...

  6. Caliburn.Micro 杰的入门教程3,事件和参数

    Caliburn.Micro 杰的入门教程1(翻译)Caliburn.Micro 杰的入门教程2 ,了解Data Binding 和 Events(翻译)Caliburn.Micro 杰的入门教程3, ...

  7. RabbitMQ入门教程(七):主题交换机Topics

    原文:RabbitMQ入门教程(七):主题交换机Topics 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog. ...

  8. Spring Cloud 入门教程(七): 熔断机制 -- 断路器

    对断路器模式不太清楚的话,可以参看另一篇博文:断路器(Curcuit Breaker)模式,下面直接介绍Spring Cloud的断路器如何使用. SpringCloud Netflix实现了断路器库 ...

  9. Spring Framework 学习笔记——核心技术之Spring IOC

    Spring Framework 官网文档学习笔记--核心技术之Spring IOC 官方文档 spring-framework-5.3.9 1. Spring Framework 核心技术 1.1 ...

  10. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

随机推荐

  1. 经过4次优化我把python代码耗时减少95%

    背景交代 团队做大学英语四六级考试相关服务.业务中有一个care服务,购买了care服务考试不过可以全额退款,不过有一个前提是要完成care服务的任务,比如坚持背单词N天,完成指定的试卷. 在这个背景 ...

  2. 自动下载MarkDown格式会议论文的程序

    近期师兄发给我一个压缩包让我整理文献,而我发现压缩包里的内容是这样: 这样: 和这样的: 我大概看了一下,可能有270多篇文章是这种格式,俗话说的好,没有困难的工作,只有勇敢的研究僧.所以决定用Pyt ...

  3. 《Python语言程序设计》【第1周】Python基本语法元素

    实例:温度转化 #TempConvert.py 单行注释 ''' TemConvert.py ''' # 多行注释 TempStr = input("请输入带有符号的温度值: ") ...

  4. 『学了就忘』Linux软件包管理 — 43、RPM包的校验和证书

    目录 1.RPM包的校验 (1)RPM包校验基本命令 (2)校验某个系统文件是否被修改举例 (3)验证内容中8个信息的具体内容 (4)文件类型有哪些 2.RPM包的证书 (1)数字证书 (2)数字证书 ...

  5. [bzoj1189]紧急疏散

    二分答案+判定,对于一个答案,源点向每一个点连一条流量为1的边,每一扇门向汇点连一条流量为时间的边,每一个人向每一个在答案时间内能走到的门连一条流量为1的边,跑最大流并判断流量是否等于人数. 然而自从 ...

  6. [hdu6715]算术

    首先要知道一个式子:$\mu(lcm(i,j))=\mu(i)\cdot \mu(j)\cdot \mu(gcd(i,j))$(分是否为0讨论)令$d=gcd(i,j)$,$n'=\lfloor n/ ...

  7. [atARC078F]Mole and Abandoned Mine

    注意到最终图的样子可以看作一条从1到$n$的路径,以及删去这条路径上的边后,路径上的每一个点所对应的一个连通块 考虑dp,令$f_{S,i}$表示当前1到$n$路径上的最后一个点以及之前点(包括$i$ ...

  8. Cannot connect to runtime process

    发生一个或多个错误. 未能启动调试适配器.可以在输出窗口中查看额外的信息. Cannot connect to runtime process, timeout after 10000 ms (rea ...

  9. 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...

  10. 使用 CSS 轻松实现一些高频出现的奇形怪状按钮

    背景 在群里会有同学问相关的问题,怎么样使用 CSS 实现一个内切角按钮呢.怎么样实现一个带箭头的按钮呢? 本文基于一些高频出现在设计稿中的,使用 CSS 实现稍微有点难度和技巧性的按钮,讲解使用 C ...