Spring(九)之事件处理
Spring的核心是ApplicationContext,它管理bean的完整生命周期。ApplicationContext在加载bean时发布某些类型的事件。例如,ContextStartedEvent当上下文启动,并公布ContextStoppedEvent当上下文停止出版。
ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口提供的。因此,如果bean实现了ApplicationListener,那么每次将ApplicationEvent发布到ApplicationContext时,都会通知该bean。

Spring的事件处理是单线程的,因此如果事件被发布,除非所有接收者都收到消息,否则流程将被阻止,流程将不会继续。因此,如果要使用事件处理,在设计应用程序时应该小心。
监听上下文事件
要监听上下文事件,bean应该实现ApplicationListener接口,该接口只有一个onApplicationEvent()方法。因此,让我们编写一个示例来查看事件如何传播以及如何使代码根据特定事件执行所需任务。
示例:
(1)编写HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
(2)编写CStartEventHandler.java
package com.tutorialspoint; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent; public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{ public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
(3)编写CStopEventHandler.java
package com.tutorialspoint; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent; public class CStopEventHandler
implements ApplicationListener<ContextStoppedEvent>{ public void onApplicationEvent(ContextStoppedEvent event) {
System.out.println("ContextStoppedEvent Received");
}
}
(4)编写MainApp.java
package com.tutorialspoint; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml"); // Let us raise a start event.
context.start(); HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage(); // Let us raise a stop event.
context.stop();
}
}
(5)编写Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean> <bean id = "cStartEventHandler" class = "com.tutorialspoint.CStartEventHandler"/>
<bean id = "cStopEventHandler" class = "com.tutorialspoint.CStopEventHandler"/> </beans>
(6)运行MainApp.java中的main方法,结果如下:

Spring(九)之事件处理的更多相关文章
- Spring中的事件处理
文章目录 Spring中的事件处理 Spring 的核心是 ApplicationContext,它负责管理 beans 的完整生命周期.当加载 beans 时,ApplicationContext ...
- Spring 中的事件处理
Spring 中的事件处理 Spring 的核心是 ApplicationContext,它负责管理 beans 的完整生命周期.当加载 beans 时,ApplicationContext 发布某些 ...
- spring 九种设计模式
spring中常用的设计模式达到九种,我们举例说明: 第一种:简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. 简单工厂模式的实质是由一 ...
- Spring(九)Spring对事务的支持
一.对事务的支持 事务:是一组原子操作的工作单元,要么全部成功,要么全部失败 Spring管理事务方式: JDBC编程事务管理:--可以控制到代码中的行 可以清楚的控制事务的边界,事务控制粒度化细(编 ...
- 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier
Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...
- Spring入门学习笔记(3)——事件处理类
目录 Spring中的事件处理 Spring内建事件 监听Context事件 Example 自定义Spring事件 Spring中的事件处理 ApplicationContext 是Spring的核 ...
- 狗鱼IT教程:推介最强最全的Spring系列教程
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建. 简单来说,Spring是一个分层的JavaSE/EEfull-stack( ...
- Spring 文件上传MultipartFile 执行流程分析
在了解Spring 文件上传执行流程之前,我们必须知道两点: 1.Spring 文件上传是基于common-fileUpload 组件的,所以,文件上传必须引入此包 2.Spring 文件上传需要在X ...
- Spring教程索引
Spring教程索引 2016-11-15 1 入门 1 概述.深入浅出Spring(一)Spring概述 2 体系结构 3 环境设置 4 Hello World 实例 5 IoC 容器 IoC容 ...
随机推荐
- eclipse的debug技巧之一
如下图,断点设置的地方会在i==0时就停住,假如我们想在i等于某个值的时候才停住,那么可以添加“过滤条件” 具体操作是在debug模式下,右键breakpoints标签下我们所设置的断点,点击右键菜单 ...
- 不得不知的npm常用指令
前端进阶肯定会遇到npm(包管理工具)的使用,下面是我总结的一些比较实用的npm指令: npm install <name>安装nodejs的依赖包 例如npm install expre ...
- "Error: ANDROID_HOME is not set and "android" command not in your PATH. You must fulfill at least one of these conditions.".
设置环境变量 set ANDROID_HOME=C:\\android-sdk-windows set PATH=%PATH%;%ANDROID_HOME%\tools;%ANDROID_HOME%\ ...
- jquery特效:无缝向上循环滚动列表
效果呈现 整个列表间隔设定的时间向上移动一个item的高度 html结构: <div class="slide-title"> <span>title1&l ...
- MAVLink Linux/QNX/MacOs Integration Tutorial (UDP)
MAVLink Linux/QNX/MacOs Integration Tutorial (UDP) Overview This program was written to test the udp ...
- javascript的 热点图怎么写
在gis中,你如果用js来写热点图 不借助后台怎么搞,as的话比较容易有相应的类库甚至官方都有.而且用js不借助arcgis发布rest服务,(注:热点图可以借助服务的形式发布,arcgis for ...
- Android SD卡上文件
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- 网络测速 php代码
<?php /*=====http://hi.csdn.net/yinyiniao=====*/ $fp=fopen("cs.txt","w"); for ...
- Qt初学——我的第一个UI
第一次打开Qt的时候,我是一脸懵逼的.没学过c++,里面的程序都看不懂.按照套路,我开始看教程,上手实践.连着搞了3天之后,我开始渐渐明白怎么写UI. 我现在的理解是:UI = 界面设计 + 信号槽响 ...
- spring+quarts常见问题
javax/transaction/UserTransactionCaused by: java.lang.NoClassDefFoundError: javax/transaction/UserTr ...