在Spring中已经定义的五个标准事件:
1) ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件。
2) ContextClosedEvent:当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有单例Bean都被销毁。
3) RequestHandleEvent:在Web应用中,当一个http请求(request)结束触发该事件。
4) ContestStartedEvent:Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的Start()方法开始/重新开始容器时触发该事件。
5) ContestStopedEvent:Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的Stop()方法停止容器时触发该事件。
下面通过一个例子展示如何处理Spring内定的事件(例程3.8)。创建一个Java工程,添加Spring开发能力后,新建ioc.test包。在包中新建ApplicationEventListener类,实现ApplicationListener接口,在onApplicationEvent()方法中添加事件处理代码,如下:
package ioc.test;
// Import省略
public class ApplicationEventListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
//如果是容器刷新事件
if(event instanceof ContextClosedEvent ){
System.out.println(event.getClass().getSimpleName()+" 事件已发生!");
}else if(event instanceof ContextRefreshedEvent ){//如果是容器关闭事件
System.out.println(event.getClass().getSimpleName()+" 事件已发生!");
}else if(event instanceof ContextStartedEvent ){
System.out.println(event.getClass().getSimpleName()+" 事件已发生!");
}else if(event instanceof ContextStoppedEvent){
System.out.println(event.getClass().getSimpleName()+" 事件已发生!");
}else{
System.out.println("有其它事件发生:"+event.getClass().getName());
}
}
}
在Spring配置文件中定义一个Bean,类为ApplicationEventListener,代码如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans …………>
3
4 <bean id="ApplicationEventListener" class="ioc.test.ApplicationEventListener" />
5
6 </beans>
7
添加含有主方法的TesMain类,在主方法中,调用容器的相应方法,触发Spring内定事件,代码如下:
package ioc.test;
//import省略
public class TesMain {
public static void main(String[] args) {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// ac.refresh();//触发ContextRefreshedEvent事件
ac.start(); //触发ContextStartedEvent事件
ac.stop(); //触发ContextStoppedEvent事件
ac.close(); //关闭容器,触发ContextClosedEvent事件
}
}
从例子中可以知道,要注册事件监听器,我们只需要把它配置成一个Bean即可,ApplicationContext容器会自动将其注册。
==============================================================
1、监听事件
写一个class 实现 ApplicationListener 接口, 并且在spring 项目中进行配置,当spring 有事件发布时便会调用ApplicationListener 接口的void onApplicationEvent(ApplicationEvent evnet) 方法。
2、 发布事件定义
事件Class 要继承ApplicationEvent class
3、事件发布 使用 ApplicationContext.publishEvent(evnet);
当事件发布时所有的监听器ApplicationListener 都会收到事件通知,
如果bean 要发布事件要得到当前的ApplicationContext引用,需要实现ApplicationContextAware接口
实现BeanFactoryAware接口的class 能自动由环境中获得beanFactory接口,
需要强制转换为ApplicationContext
=======================================================
定义事件
public class ActionEvent extends ApplicationEvent {
public ActionEvent(Object source) {
super(source);
}
}
发布事件
public class LoginAction implements ApplicationContextAware {
private ApplicationContext applicationContext;
public void setApplicationContext(
ApplicationContext applicationContext
)
throws BeansException {
this.applicationContext = applicationContext;
}
public int login(String username,String password) {
ActionEvent event = new ActionEvent(username);
this.applicationContext.publishEvent(event);
return 0;
}
}
事件监听
public class ActionListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ActionEvent) {
System.out.println(event.toString());
}
}
}
配置bean
<bean id="loginaction" class="beans.LoginAction"/>
<bean id="listener" class="beans.ActionListener"/>
测试
ApplicationContext ctx=new
FileSystemXmlApplicationContext("bean.xml");
LoginAction action = (LoginAction)ctx.getBean("action");
action.login("Erica","mypass");
分享到:
相关推荐
主要介绍了使用Spring事件机制实现异步的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://xls9577087.iteye.com/blog/2121752
Spring 事件机制代码,有效解决了软件之间的耦合 学习项目代码
Spring框架作为Java开发的核心,其事件监听机制是理解和利用Spring的关键要素之一。这种机制基于观察者模式,由事件(ApplicationEvent)、事件监听器(ApplicationListener)和事件广播器...
Spring事件监听机制
对spring代理机制的实现和原理的详细讲解
如何通过spring自己的缓存机制缓存方法和数据,使系统性能更高!
spring传播机制个人学习
讲述spring事务机制,aopafter,aopbefore,aopmethodintercept,4个例子
希望大家看完这篇文章后能对Spring有更深入的了解,希望这篇文章对你有所帮助
全面的Spring 核心机制 依赖注入 的描述,运用,实例开发运用,
Spring ApplicationContext 事件机制
spring 事件机制(观察者模式)
Spring AOP的实现机制中文版,动态代理及原理,自定义类加载器
微信支付-简单易用 & Spring ...使用 spring 事件机制封装了微信支付通知,以简化使用方式。wiremock 是一个 web mock server,配置中可配置微信 API 的 basePath, 所以你可以使用 wiremock 构建一个完全的仿真环境。
主要介绍了EventBus与Spring Event区别,需要的朋友可以参考下
Spring Security以前叫做acegi,是后来才成为Spring的一个子项目,也是目前最为流行的一个安全权限管理框架,它与Spring紧密结合在一起。 Spring Security关注的重点是在企业应用安全层为您提供服务,你将发现业务...
spring安全机制demo 暂时还不能用 但是可以看出思路 入门
SpringIOC和AOP实现机制模拟,来自与网络。
【免费】深入描述spring的事务处理机制,很不错的资源。