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的更多相关文章

  1. [转]Getting started with SSIS - Part 10: Event Handling and Logging

    本文转自:http://beyondrelational.com/modules/12/tutorials/24/tutorials/9686/getting-started-with-ssis-pa ...

  2. Console Event Handling

    http://www.codeproject.com/Articles/2357/Console-Event-Handling Console Event Handling Kumar Gaurav ...

  3. 理解iOS Event Handling

    写在前面 最近的一个iOS App项目中遇到了这么问题:通过App访问服务器的大多数资源不需要登录,但是访问某些资源是需要用户提供验证的,一般来说,通常App的做法(譬如美团App)将这些资源放在“我 ...

  4. 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 ...

  5. Event Handling Guide for iOS--(二)---Gesture Recognizers

    Gesture Recognizers 手势识别器 Gesture recognizers convert low-level event handling code into higher-leve ...

  6. 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 ...

  7. UI Framework-1: Aura Event Handling

    Event Handling A diagram of the architecture of this system:     HWNDMessageHandler owns the WNDPROC ...

  8. Event Handling Guide for iOS(五)

    基本概念: 加速计: 又称加速度计,测量设备运动的加速度. 加速度: 矢量,描绘速度的方向和大小变化的快慢. 陀螺仪: 感测与维持方向的装置. 原文: Motion Event声明: 由于本人水平有限 ...

  9. Event Handling on Mac

    Keyboard/Mouse Event + Cocoa AppleEvent + Cocoa AppleEvent + CommandLine App(w/o UI) + CoreFoundatio ...

随机推荐

  1. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息]

    三.使用Azure AD管理用户信息 在上一章我们采用OpenID的方案和Azure AD交互进行身份验证,本章节我们继续了解如何在Azure AD中创建用户,列出用户信息,修改用户信息和删除用户信息 ...

  2. 测试rest接口的两个工具使用详解(restclient+soapUI)

    http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/31/3110764.html http://www.oschina.net/news/618 ...

  3. vs2012 + web api + OData + EF + MYsql 开发及部署

    先说下我的情况,b/s开发这块已经很久没有搞了,什么web api .MVC.OData都只是听过,没有实际开发过,因为最近要开发一个手机app的服务端,所以准备用这套框架来开发. 下面开始进入正题( ...

  4. sc7731 Android 5.1 LCD驱动简明笔记之二

    此篇笔记基于sc7731 - android 5.1,对lcd的framebuffer做一个简明笔记. 一共分为两大部分:第一部分,关于LCD的硬件方面的:第二部分,关于lcd核心处理(framebu ...

  5. xshell 远程连接Linux

    Linux系统(Ubuntu 16.04) Window系统(win10) 工具:xshell Linux下, sudo apt-get update sudo apt-get install ope ...

  6. jQuery.FlexiGrid使用总结

    经过对FlexiGrid的大量使用,及时不时琢磨下其代码,对她的脾性有了一定的了解,是该做总结的时候了. 一.FlexiGrid下载 1.原版代码 最近Paulo P. Marinas对FlexiGr ...

  7. Gym 100531H Problem H. Hiking in the Hills 二分

    Problem H. Hiking in the Hills 题目连接: http://codeforces.com/gym/100531/attachments Description Helen ...

  8. 32位Windows7上8G内存使用感受+xp 32位下使用8G内存 (转)

    32位Windows7上8G内存使用感受+xp 32位下使用8G内存 博客分类: Windows XPWindowsIE企业应用软件测试  我推荐做开发的朋友:赶快加入8G的行列吧....呵呵..超爽 ...

  9. 关于sources.list和apt-get [转载]

    个人觉得,Debian最大的方便在于用apt-get安装软件,apt-get的工作原理大概是这种:/etc/apt/sources.list文件中保存着一些server的设置,在这些server上有大 ...

  10. vs 设置生成的实体为复数