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 ...
随机推荐
- c语言-格式控制字符 %XXd 用法
d格式字符 用来输出十进制整数,有以下几种用法: 1. %d, 按整型数据的实际长度输出. 2. %md,m为指定输出的整型位数的宽度,如果整型数据的实际位数小于m,则左端补以空格,如果大于m,则按 ...
- Java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;[Ljava/lang/Object;Ljava/lang/Throwable;)V
今天接入激光推送,一直报错: Java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker; ...
- java中final的用法
final从字面翻译来看,有最后的,最终的; 决定性的; 不可更改的等含义,在java里面主要还是“不可改变的”这层意思,不可改变或者不想改变的final主要用在类.方法.数据这三个方面. 一.f ...
- intval()和(int)转换使用与区别
<?php echo "<br/>数值强制转换:"; $string="2a"; $string1=intval($string); echo ...
- 【转】Python字符编码详解
转自:http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html 1. 字符编码简介 1.1. ASCII ASCII(American S ...
- sqldependency 支持的select
https://msdn.microsoft.com/library/ms181122.aspx 支持的 SELECT 语句 满足下列要求的 SELECT 语句支持查询通知: 必须显式说明 SEL ...
- Codeforces Round #288 (Div. 2)D. Tanya and Password 欧拉通路
D. Tanya and Password Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/508 ...
- Docker:使用Ambassador进行跨主机间容器通信
转载请注明出处:点我 由于Docker自身的网络的原因,想要在多主机间的容器之间进行通信是比较麻烦的事情.可以利用Ambassador容器来实现这一功能. 基本原理: 利用Ambassador来实现主 ...
- unity3d脚本编程
一 创建和使用脚本 1 概述 GameObject的行为都是被附加到其上面的组件控制,脚本本质上也是一个组件. 在unity中创建一个脚本,默认内容例如以下: using UnityEngine; u ...
- 安装linux系统并配置那点事
安装完成后,将看到如下控制台: 输入以上安装信息中所填写的用户名(user),随后输入密码(user),即可登录 Ubuntu. 随时可使用 Ctrl + Alt 快捷键可离开虚拟机. 3 配置 Ub ...