首页 技术 正文
技术 2022年11月17日
0 收藏 962 点赞 5,085 浏览 6175 个字

spring发布和接收定制的事件(spring事件传播)

2012-12-26 20:05 22111人阅读 评论(2) 举报 分类:开源技术(如Struts/spring/Hibernate等)(70)  架构和设计模式(59) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

有事件,即有事件监听器. 有人问你spring监听器有哪些你看了下文即也知道了。 事件传播ApplicationContext基于Observer模式(java.util包中有对应实现),提供了针对Bean的事件传播功能。通过Application. publishEvent方法,我们可以将事件通知系统内所有的ApplicationListener。 事件传播的一个典型应用是,当Bean中的操作发生异常(如数据库连接失败),则通过事件传播机制通知异常监听器进行处理。在笔者的一个项目中,就曾经借助事件机制,较好的实现了当系统异常时在监视终端上报警,同时发送报警SMS至管理员手机的功能。 

ApplicationContext容器提供了容器内部事件发布功能,是继承自JavaSE标准自定义事件类而实现的。

JavaSE标准自定义事件结构不在此详细描述,一张图很直观的描述清楚:

EventObject,为JavaSE提供的事件类型基类,任何自定义的事件都继承自该类,例如上图中右侧灰色的各个事件。spring中提供了该接口的子类ApplicationEvent。

EventListener为JavaSE提供的事件监听者接口,任何自定义的事件监听者都实现了该接口,如上图左侧的各个事件监听者。Spring中提供了该接口的子类ApplicationListener接口。

JavaSE中未提供事件发布者这一角色类,由各个应用程序自行实现事件发布者这一角色。Spring中提供了ApplicationEventPublisher接口作为事件发布者,并且ApplicationContext实现了这个接口,担当起了事件发布者这一角色。但ApplicationContext在具体实现上有所差异,Spring提供了ApplicationEventMulticaster接口,负责管理ApplicationListener和发布ApplicationEvent。ApplicationContext会把相应的事件相关工作委派给ApplicationEventMulticaster接口实现类来做。类图如下所示:

事件发布时序图如下:

 ————————————————————————————————-

Spring中提供一些Aware相关的接口,BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,其中最常用到的是ApplicationContextAware。实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入ApplicationContext的实例。ApplicationContextAware提供了publishEvent()方法,实现Observer(观察者)设计模式的事件传播机,提供了针对Bean的事件传播功能。通过Application.publishEvent方法,我们可以将事件通知系统内所有的ApplicationListener。

Spring事件处理一般过程:

◆定义Event类,继承org.springframework.context.ApplicationEvent。
◆编写发布事件类Publisher,实现org.springframework.context.ApplicationContextAware接口。
◆覆盖方法setApplicationContext(ApplicationContext applicationContext)和发布方法publish(Object obj)。
◆定义时间监听类EventListener,实现ApplicationListener接口,实现方法onApplicationEvent(ApplicationEvent event)。

1.发布

    1.1事件的发布者需要实现的接口        org.springframework.context.ApplicationEventPublisherAware    1.2 代码示例  

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

/**
*
* @author zq
*
*/
public class HelloWorld implements ApplicationEventPublisherAware{
private String word;
private ApplicationEventPublisher tradeEventPublisher;

public void setWord(String w){
this.word = w;
}

public void say(){
System.out.println(“say : “+ this.word);
//construct a TradeEvent instance and publish it
TradeEvent tradeEvent = new TradeEvent(new String(“tradeEvent”));
this.tradeEventPublisher.publishEvent(tradeEvent);
}

@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
// TODO Auto-generated method stub
this.tradeEventPublisher = applicationEventPublisher;
}
}

2.接受事件

  2.1需要实现的接口org.springframework.context.ApplicationListener  2.2代码示例

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;

public class TradeContextListener implements ApplicationListener{

@Override
public void onApplicationEvent(ApplicationEvent e) {

System.out.println(e.getClass().toString());
// TODO Auto-generated method stub
if (e instanceof ContextStartedEvent){
System.out.println(“it was contextStartedEvent”);
}

if (e instanceof TradeEvent){
System.out.println(e.getSource());
}

}

}

3配置文件

<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”>
<beans>
<bean name=”helloWorld” class=”study.HelloWorld”>
<property name=”word” value=”hello world”/>
</bean>

<bean id=”tradeContextListener” class=”study.TradeContextListener”/>
</beans>

4.测试代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import study.HelloWorld;
public class TestHelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“study-context.xml”);
HelloWorld bean = (HelloWorld)applicationContext.getBean(“helloWorld”);
bean.say();
}

}

 Spring中ApplicationContext的事件机制— 内定事件)在Spring中已经定义了五个标准事件,分别介绍如下:

1)      ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件。

2)      ContextClosedEvent:当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有单例Bean都被销毁。

3)      RequestHandleEvent:在Web应用中,当一个http请求(request)结束触发该事件。

ContestStartedEvent:Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的Start()方法开始/重新开始容器时触发该事件。

5) ContestStopedEvent:Spring2.5新增的事件,当容器调用ConfigurableApplicationContext的Stop()方法停止容器时触发该事件。

下面通过一个例子展示如何处理Spring内定的事件(例程3.8)。创建一个Java工程,添加Spring开发能力后,新建ioc.test包。在包中新建ApplicationEventListener类,实现ApplicationListener接口,在onApplicationEvent()方法中添加事件处理代码,如下:

1 package ioc.test;

3 //Import省略
4 publicclass ApplicationEventListenerimplements ApplicationListener {

6 publicvoid onApplicationEvent(ApplicationEvent event) {

8 //如果是容器刷新事件
9 if(eventinstanceof ContextClosedEvent ){
10             System.out.println(event.getClass().getSimpleName()+” 事件已发生!”);   
11         }elseif(eventinstanceof ContextRefreshedEvent ){//如果是容器关闭事件
12             System.out.println(event.getClass().getSimpleName()+” 事件已发生!”);   
13         }elseif(eventinstanceof ContextStartedEvent ){
14             System.out.println(event.getClass().getSimpleName()+” 事件已发生!”);
15         }elseif(eventinstanceof ContextStoppedEvent){
16             System.out.println(event.getClass().getSimpleName()+” 事件已发生!”);
17         }else{
18             System.out.println(“有其它事件发生:”+event.getClass().getName());
19         }
20                    
21     }
22 
23 }
24 

在Spring配置文件中定义一个Bean,类为ApplicationEventListener,代码如下:

1 <?xml version=”1.0″ encoding=”UTF-8″?>
2 <beans…………   

4    <bean id=”ApplicationEventListener” class=”ioc.test.ApplicationEventListener”/>

6 </beans>
7

添加含有主方法的TesMain类,在主方法中,调用容器的相应方法,触发Spring内定事件,代码如下:

1 package ioc.test;

3 //import省略
4 publicclass TesMain {

6 publicstaticvoid main(String[] args) {
7         AbstractApplicationContext ac=new ClassPathXmlApplicationContext(“applicationContext.xml”);
8         

10 //    ac.refresh();//触发ContextRefreshedEvent事件           
11         ac.start();//触发ContextStartedEvent事件
12         ac.stop(); //触发ContextStoppedEvent事件       
13         ac.close();//关闭容器,触发ContextClosedEvent事件
14 
15     }
16 }
17

运行主类,控制台输出如下:

从例子中可以知道,要注册事件监听器,我们只需要把它配置成一个Bean即可,ApplicationContext容器会自动将其注册。

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295