Event Handling in Spring
Spring内置的event有
1.ContextRefreshedEvent
This event is published when the ApplicationContext is either initialized or refreshed. This can also be raised using the refresh() method on the ConfigurableApplicationContext interface.
2.ContextStartedEvent
This event is published when the ApplicationContext is started using the start() method on theConfigurableApplicationContext interface. You can poll your database or you can re/start any stopped application after receiving this event.
3.ContextStoppedEvent
This event is published when the ApplicationContext is stopped using the stop() method on the ConfigurableApplicationContext interface. You can do required housekeep work after receiving this event.
4.ContextClosedEvent
This event is published when the ApplicationContext is closed using the close() method on theConfigurableApplicationContext interface. A closed context reaches its end of life; it cannot be refreshed or restarted.
5.RequestHandledEvent
This is a web-specific event telling all beans that an HTTP request has been serviced.
Spring's event handling is single-threaded so if an event is published, until and unless all the receivers get the message, the processes are blocked and the flow will not continue. Hence, care should be taken when designing your application if event handling is to be used.
下面我们来测试一下
首先我们写一个helloword
package com.hyenas.spring.event;
public class HelloWorld {
private String msg;
public void setMsg(String msg) {
this.msg = msg;
}
public void getMsg() {
System.out.println("your message:" + msg);
}
}
CStartEventHandler.java
package com.hyenas.spring.event; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent; public class CStartEventHandler implements ApplicationListener<ContextStartedEvent>{ @Override
public void onApplicationEvent(ContextStartedEvent arg0) {
System.out.println("ContextStartedEvent Received");
} }
CRefreshedEventHandler.java
package com.hyenas.spring.event; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent; public class CRefreshedEventHandler implements ApplicationListener<ContextRefreshedEvent> { @Override
public void onApplicationEvent(ContextRefreshedEvent arg0) {
System.out.println("ContextRefreshedEvent received");
} }
CStopEventHandler.java
package com.hyenas.spring.event; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStoppedEvent; public class CStopEventHandler implements ApplicationListener<ContextStoppedEvent>{ @Override
public void onApplicationEvent(ContextStoppedEvent arg0) {
System.out.println("ContextStoppedEvent received");
} }
然后我们配置一个Spring 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="helloWorld" class="com.hyenas.spring.event.HelloWorld">
<property name="msg" value="Hello World!"/>
</bean> <bean id="cStartEventHandler"
class="com.hyenas.spring.event.CStartEventHandler"/> <bean id="cStopEventHandler"
class="com.hyenas.spring.event.CStopEventHandler"/> <bean id="cRefreshedEventHandler"
class="com.hyenas.spring.event.CRefreshedEventHandler"/> </beans>
现在我们来测试一下我们写的东西:
MainApp.java
package com.hyenas.spring.event; import org.springframework.beans.BeansException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext("event-handler.xml"); // Let us raise a start event.
context.start(); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMsg(); // Let us raise a refresh event
context.refresh(); // Let us raise a stop event.
context.stop();
} catch (BeansException e) {
if (context != null) {
context.close();
}
} }
}
运行结果:
ContextRefreshedEvent received
ContextStartedEvent Received
your message:Hello World!
ContextRefreshedEvent received
ContextStoppedEvent received
Event Handling in Spring的更多相关文章
- [转]Getting started with SSIS - Part 10: Event Handling and Logging
本文转自:http://beyondrelational.com/modules/12/tutorials/24/tutorials/9686/getting-started-with-ssis-pa ...
- Console Event Handling
http://www.codeproject.com/Articles/2357/Console-Event-Handling Console Event Handling Kumar Gaurav ...
- 理解iOS Event Handling
写在前面 最近的一个iOS App项目中遇到了这么问题:通过App访问服务器的大多数资源不需要登录,但是访问某些资源是需要用户提供验证的,一般来说,通常App的做法(譬如美团App)将这些资源放在“我 ...
- Event Handling Guide for iOS--(三)---Event Delivery: The Responder Chain
Event Delivery: The Responder Chain 事件传递:响应链 When you design your app, it’s likely that you want to ...
- Event Handling Guide for iOS--(二)---Gesture Recognizers
Gesture Recognizers 手势识别器 Gesture recognizers convert low-level event handling code into higher-leve ...
- Event Handling Guide for iOS--(一)--About Events in iOS
About Events in iOS Users manipulate their iOS devices in a number of ways, such as touching the scr ...
- UI Framework-1: Aura Event Handling
Event Handling A diagram of the architecture of this system: HWNDMessageHandler owns the WNDPROC ...
- Event Handling Guide for iOS(五)
基本概念: 加速计: 又称加速度计,测量设备运动的加速度. 加速度: 矢量,描绘速度的方向和大小变化的快慢. 陀螺仪: 感测与维持方向的装置. 原文: Motion Event声明: 由于本人水平有限 ...
- Event Handling on Mac
Keyboard/Mouse Event + Cocoa AppleEvent + Cocoa AppleEvent + CommandLine App(w/o UI) + CoreFoundatio ...
随机推荐
- hdu4291之矩阵快速幂
A Short problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Java程序打包成jar包
方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] ...
- U8记账凭证修改方法汇总
在输入记账凭证时,尽管账务系统提供了多种控制错误的措施,但错误凭证的出现是难免的,为此,系统必须能够提供对错误凭证修改的功能.目前,许多财 务软件(如:用友.安易.三门)都提供了“反审核.反记账.反结 ...
- 组件化CSS--管理你整站的CSS文件
为什么要拆分样式文件? 更易于查找样式规则. 简化维护,方便管理. 还可以针对某一页面提供特定的样式. 为什么要添加桥接样式? 你可以随时添加或移除样式而不需要修改HTML 文档. 为什么要定义两种媒 ...
- jq获取表单值与赋值代码
jq获取表单值与赋值代码 jq获取表单值与赋值代码 $("#keyword")[0].value = ""; /*获得TEXT.AREATEXT的值*/ var ...
- JavaScript OOP 思想
JS的核心是对象 {}, new function(){}这种形式也是对象. http://www.nowamagic.net/librarys/veda/detail/241 整理一些网上的资料,供 ...
- poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数
点击打开链接 Konig定理:二分图的最小顶点覆盖数 = 二分图的最大匹配数 题意: 在N*N的网络中有K颗小行星.小行星i的位置是(Ri, Ci).如今有一个强力的武器可以用一发光束将一整行或一整列 ...
- 获取div相对文档的位置
获取div相对文档的位置,两个方法 经测试 document.getElementById("btn").getBoundingClientRect() 在IE6下有2像素的bug ...
- 第1章 游戏之乐——让CPU占用率曲线听你指挥
让CPU占用率曲线听你指挥 写一个程序,让用于来决定Windows任务管理器(Task Manager)的CPU占用率.程序越精简越好,计算机语言不限.例如,可以实现下面三种情况: CPU的占用率固定 ...
- 使用redis做pv、uv、click统计
redis实时统计 设计思路: 1. 前端smarty插件(smarty_function_murl),将网站所有的连接生成一个urlid,后端根据获取的参数将需要的数据存入redis. 2.后端插件 ...