有事件,即有事件监听器. 有人问你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.发布<wbr><div> 1.1事件的发布者需要实现的接口</div> <div> org.springframework.context.ApplicationEventPublisherAware</div> <div> 1.2 代码示例 </div> <pre class="prettyprint"><p><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">context</span><span class="pun">.</span><span class="typ">ApplicationEventPublisher</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">context</span><span class="pun">.</span><span class="typ">ApplicationEventPublisherAware</span><span class="pun">;</span> <span class="com">/**</span> <span class="com"> * </span> <span class="com"> * @author zq</span> <span class="com"> *</span> <span class="com"> */</span> <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">HelloWorld</span><span class="pln"> </span><span style="color:#ff0000"><span class="kwd">implements</span><span class="pln"> </span><span class="typ">ApplicationEventPublisherAware</span></span><span class="pun">{</span> <span class="pln"> </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">String</span><span class="pln"> word</span><span class="pun">;</span> <span class="pln"> </span><span class="kwd">private</span><span class="pln"> </span><span class="typ">ApplicationEventPublisher</span><span class="pln"> tradeEventPublisher</span><span class="pun">;</span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setWord</span><span class="pun">(</span><span class="typ">String</span><span class="pln"> w</span><span class="pun">){</span> <span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">word </span><span class="pun">=</span><span class="pln"> w</span><span class="pun">;</span> <span class="pln"> </span><span class="pun">}</span> <span class="pln"> </span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> say</span><span class="pun">(){</span> <span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"say : "</span><span class="pun">+</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">word</span><span class="pun">);</span> <span class="pln"> </span><span style="color:#ff0000"><span class="com">//construct a TradeEvent instance and publish it</span> <span class="pln"> </span><span class="typ">TradeEvent</span><span class="pln"> tradeEvent </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">TradeEvent</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">String</span><span class="pun">(</span><span class="str">"tradeEvent"</span><span class="pun">));</span> <span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">tradeEventPublisher</span><span class="pun">.</span><span class="pln">publishEvent</span><span class="pun">(</span><span class="pln">tradeEvent</span><span class="pun">);</span></span> <span class="pln"> </span><span class="pun">}</span> <span class="pln"> </span><span class="lit">@Override</span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> setApplicationEventPublisher</span><span class="pun">(</span> <span class="pln"> </span><span class="typ">ApplicationEventPublisher</span><span class="pln"> applicationEventPublisher</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span> <span class="pln"> </span><span class="com">// TODO Auto-generated method stub</span> <span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">tradeEventPublisher </span><span class="pun">=</span><span class="pln"> applicationEventPublisher</span><span class="pun">;</span> <span class="pln"> </span><span class="pun">}</span> <span class="pun">}</span></p></pre> 2.接受事件 <div> 2.1需要实现的接口org.springframework.context.ApplicationListener</div> <div> 2.2代码示例</div> <pre class="prettyprint"><p><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">context</span><span class="pun">.</span><span class="typ">ApplicationEvent</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">context</span><span class="pun">.</span><span class="typ">ApplicationListener</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">context</span><span class="pun">.</span><span class="kwd">event</span><span class="pun">.</span><span class="typ">ContextStartedEvent</span><span class="pun">;</span> <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TradeContextListener</span><span class="pln"> </span><span class="kwd">implements</span><span class="pln"> </span><span class="typ">ApplicationListener</span><span class="pun">{</span> <span class="pln"> </span><span class="lit">@Override</span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> onApplicationEvent</span><span class="pun">(</span><span class="typ">ApplicationEvent</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span> <span class="pln"> </span> <span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">getClass</span><span class="pun">().</span><span class="pln">toString</span><span class="pun">());</span> <span class="pln"> </span><span class="com">// TODO Auto-generated method stub</span> <span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">e </span><span class="kwd">instanceof</span><span class="pln"> </span><span class="typ">ContextStartedEvent</span><span class="pun">){</span> <span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"it was contextStartedEvent"</span><span class="pun">);</span> <span class="pln"> </span><span class="pun">}</span> <span class="pln"> </span> <span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">e </span><span class="kwd">instanceof</span><span class="pln"> </span><span class="typ">TradeEvent</span><span class="pun">){</span> <span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="pln">e</span><span class="pun">.</span><span class="pln">getSource</span><span class="pun">());</span> <span class="pln"> </span><span class="pun">}</span> <span class="pln"> </span> <span class="pln"> </span><span class="pun">}</span> <span class="pun">}</span></p></pre> 3配置文件 <div> <pre class="prettyprint"><p><span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span> <span class="dec"><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"></span> <span class="tag"><beans></span> <span class="pln"> </span><span class="tag"><bean</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"helloWorld"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"study.HelloWorld"</span><span class="tag">></span> <span class="pln"> </span><span class="tag"><property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"word"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"hello world"</span><span class="tag">/></span> <span class="pln"> </span><span class="tag"></bean></span> <span class="pln"> </span><span class="tag"><bean</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"tradeContextListener"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"study.TradeContextListener"</span><span class="tag">/></span> <span class="tag"></beans></span></p></pre> 4.测试代码</div> <div> <pre class="prettyprint"><p><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">context</span><span class="pun">.</span><span class="typ">ApplicationContext</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">context</span><span class="pun">.</span><span class="pln">support</span><span class="pun">.</span><span class="typ">ClassPathXmlApplicationContext</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> study</span><span class="pun">.</span><span class="typ">HelloWorld</span><span class="pun">;</span> <span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestHelloWorld</span><span class="pln"> </span><span class="pun">{</span> <span class="pln"> </span><span class="com">/**</span> <span class="com"> * @param args</span> <span class="com"> */</span> <span class="pln"> </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span> <span class="pln"> </span><span class="com">// TODO Auto-generated method stub</span> <span class="pln"> </span> <span class="pln"> </span><span class="typ">ApplicationContext</span><span class="pln"> applicationContext </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">ClassPathXmlApplicationContext</span><span class="pun">(</span><span class="str">"study-context.xml"</span><span class="pun">);</span> <span class="pln"> </span><span class="typ">HelloWorld</span><span class="pln"> bean </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="typ">HelloWorld</span><span class="pun">)</span><span class="pln">applicationContext</span><span class="pun">.</span><span class="pln">getBean</span><span class="pun">(</span><span class="str">"helloWorld"</span><span class="pun">);</span> <span class="pln"> bean</span><span class="pun">.</span><span class="pln">say</span><span class="pun">();</span> <span class="pln"> </span><span class="pun">}</span> <span class="pun">}</span></p></pre> </div> </wbr>
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;
2
3
//
Import省略
4
public
class
ApplicationEventListener
implements
ApplicationListener {
5
6
public
void
onApplicationEvent(ApplicationEvent event) {
7
8
//
如果是容器刷新事件
9
if
(event
instanceof
ContextClosedEvent ){
10
System.out.println(event.getClass().getSimpleName()
+
"
事件已发生!
"
);
11
}
else
if
(event
instanceof
ContextRefreshedEvent ){
//
如果是容器关闭事件
12
System.out.println(event.getClass().getSimpleName()
+
"
事件已发生!
"
);
13
}
else
if
(event
instanceof
ContextStartedEvent ){
14
System.out.println(event.getClass().getSimpleName()
+
"
事件已发生!
"
);
15
}
else
if
(event
instanceof
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
…………
3
4
<bean id
="ApplicationEventListener"
class
="ioc.test.ApplicationEventListener"
/>
5
6
</
beans
>
7
添加含有主方法的TesMain类,在主方法中,调用容器的相应方法,触发Spring内定事件,代码如下:
1
package
ioc.test;
2
3
//
import省略
4
public
class
TesMain {
5
6
public
static
void
main(String[] args) {
7
AbstractApplicationContext ac
=
new
ClassPathXmlApplicationContext(
"
applicationContext.xml
"
);
8
9
10
//
ac.refresh();
//
触发ContextRefreshedEvent事件
11
ac.start();
//
触发ContextStartedEvent事件
12
ac.stop();
//
触发ContextStoppedEvent事件
13
ac.close();
//
关闭容器,触发ContextClosedEvent事件
14
15
}
16
}
17
运行主类,控制台输出如下:
从例子中可以知道,要注册事件监听器,我们只需要把它配置成一个Bean即可,ApplicationContext容器会自动将其注册。