第十七章 Spring 事件处理(Spring Framework3.1教程)
来源:互联网 发布:js根据日期计算星期几 编辑:程序博客网 时间:2024/06/05 11:35
你已经看到了所有的Spring核心章节ApplicationContext,它管理所有bean的整个声明周期。ApplicationContext发布确定类型的事件在加载类时。例如一个ContextStarterEvent被发布当上下文启动时,ContextStoppedEvent被发布当上下文结束时。
ApplicationContext中的事件处理通过ApplicationEvent类和ApplicationListener接口发布。因此如果一个bean继承了ApplicationListener,每次一个ApplicationEvent被发布到ApplicationContext,这个bean将得到通知。
Spring提供了如下标准的事件:
序号
Spring内置事件&描述
1
ContextRefreshedEvent
这个事件被发布当Application Context被初始化或者刷新时,它可以由refresh()方法引起在ConfigurableApplicationtext接口中。
2
ContextStartedEvent
这个事件被发布当ApplicationContext通过ConfiugrableApplication()接口的start()方法启动时。你可以查询数据库或者你可以重新启动任何停止的应用在收到这个事件后。
3
ContextStoppedEvent
这个事件被发布当ApplicationContext通过ConfigurableApplication接口的stop()方法停止时。你可以做必须的看家工作在收到这个事件时。
4
ContextClosedEvent
这个事件被发布当ApplicationContext通过ConfigurableApplicationContext接口的close()方法关闭时。一个关闭的上下文到达它声明的终点,不能在被刷新或者重启。
5
RequestHandledEvent
这是一个web专有的事件,告诉所有bean有一个HTTP请求已经被处理。
Spring的事件处理是单线程的,因此如果一个事件被发布、直到除非收到所有的消息,否则处理将阻塞并且流程将不会继续。因此,设计你的应用应该小心如果事件处理被使用。
监听Context Events
监听一个上下文事件,bean需要继承ApplicationListener接口它只有一个方法onApplicationEvent()。因此让我们写一个例子看看事件如何传播及如何放置你的代码处理需要的任务基于特定的事件。
让我们使用Eclipse IDE按如下的步骤创建一个Spring应用:
步骤
描述
1
创建一个SpringExample的项目并在src目录下创建com.tutorialspoint包。
2
在Add External JARs选项卡中添加Spring库如在Spring Hello World章节中所讲。
3
在com.tutorialspoint包下创建 HelloWorld、 CStartEventHandler、CStopEventHandler 、 MainApp类。
4
在scr目录下创建配置文件Beans.xml
5
最后一步在Java类中和Bean配置文件中添加内容,并运行应用。
如下是HelloWorld的源代码:
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); }}
如下是CStartEventHandler源代码:
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"); }}
如下是CStopEventHandler:
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"); }}
如下是MainApp的源代码:
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(); }}
如下是配置文件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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.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>
一旦你完成了源代码和配置文件的创建,运行这个应用。如果应用一切正常,将会打印如下消息:
ContextStartedEvent ReceivedYour Message : Hello World!ContextStoppedEvent Received
如果你喜欢,你可以发布你自己定制的事件,你可以俘获它然后做一些事情处理定制的事件。
- 第十七章 Spring 事件处理(Spring Framework3.1教程)
- 第十八章 Spring中定制事件(Spring Framework3.1教程)
- 第二章 Spring框架体系结构(Spring Framework3.1教程)
- 第三章 Spring环境设置(Spring Framework3.1教程)
- 第四章 Spring Hello World示例 (Spring Framework3.1教程)
- 第五章 Spring IoC容器(Spring Framework3.1教程)
- 第六章 Spring Bean定义(Spring Framework3.1教程)
- 第七章 Spring Bean的范围(Spring Framework3.1教程)
- 第八章 Spring Bean的生命周期(Spring Framework3.1教程)
- 第九章 Spring Bean Post 处理器(Spring Framework3.1教程)
- 第十一章 Spring依赖注入(Spring Framework3.1教程)
- 第十二章 Spring内部Bean注入(Spring Framework3.1教程)
- 第十三章 Spring注入集合(Spring Framework3.1教程)
- 第十四章 Spring Bean自动注入(Spring Framework3.1教程)
- 第十九章 Spring框架的AOP(Spring Framework3.1教程)
- 第一章 Spring框架概述 (Spring Framework3.1教程)
- 第十章 定义Spring Bean的继承(Spring Framework3.1教程)
- 第十五章 Spring基于注解的配置(Spring Framework3.1教程)
- 基于IEEE754标准的Modbus浮点数转换程序实现
- JDBC中ResultSetMetaData 的使用
- Prism MVVM实例
- Poj 1496 Word Index & Poj 1850 Code (计数 组合数)
- 简介CSND博文中如何插入源代码
- 第十七章 Spring 事件处理(Spring Framework3.1教程)
- UML九中图总结
- 世界上最短的数字判断代码
- 学习计算机
- HDU 1880 魔咒词典
- 最小生成树:Prim算法和Kruskal算法
- document.body.clientHeight 和 document.documentElement.clientHeight 的区别
- js 实现全选和反选
- 在UBuntu下搭建LAMP