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

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

有事件,即有事件监听器. 有人问你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;
2
3 //Import省略
4 publicclass ApplicationEventListenerimplements ApplicationListener {
5
6 publicvoid onApplicationEvent(ApplicationEvent event) {
7
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…………   
3
4    <bean id="ApplicationEventListener" class="ioc.test.ApplicationEventListener"/>
5
6 </beans>
7

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

1 package ioc.test;
2
3 //import省略
4 publicclass TesMain {
5
6 publicstaticvoid 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容器会自动将其注册。

时间: 2024-08-24 19:08:34

spring发布和接收定制的事件(spring事件传播)的相关文章

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

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

spring发布和接收事件

EventObject,为JavaSE提供的事件类型基类,任何自定义的事件都继承自该类,例如上图中右侧灰色的各个事件.Spring中提供了该接口的子类ApplicationEvent. EventListener为JavaSE提供的事件监听者接口,任何自定义的事件监听者都实现了该接口,如上图左侧的各个事件监听者.Spring中提供了该接口的子类ApplicationListener接口. JavaSE中未提供事件发布者这一角色类,由各个应用程序自行实现事件发布者这一角色.Spring中提供了Ap

Spring 4.2框架中注释驱动的事件监听器详解

事件交互已经成为很多应用程序不可或缺的一部分,spring框架提供了一个完整的基础设施来处理瞬时事件.下面我们来看看Spring 4.2框架中基于注释驱动的事件监听器. 1.早期的方式 在早期,组件要从Spring事件获知自定义域事件中获取通知,那么组件必须实现ApplicationListener接口并覆写onApplicationEvent方法. @Component class OldWayBlogModifiedEventListener implements ApplicationLi

Spring Boot实战之定制URL匹配规则

本文首发于个人网站:Spring Boot实战之定制URL匹配规则 构建web应用程序时,并不是所有的URL请求都遵循默认的规则.有时,我们希望RESTful URL匹配的时候包含定界符".",这种情况在Spring中可以称之为"定界符定义的格式":有时,我们希望识别斜杠的存在.Spring提供了接口供开发人员按照需求定制. 在之前的几篇文章中,可以通过WebConfiguration类来定制程序中的过滤器.格式化工具等等,同样得,也可以在这个类中用类似的办法配置&

Spring Boot实战之定制type Formatters

本文首发于个人网站:Spring Boot实战之定制type Formatters 前面我们有篇文章介绍了PropertyEditors,是用来将文本类型转换成指定的Java类型,不过,考虑到PropertyEditor的无状态和非线程安全特性,Spring 3增加了一个Formatter接口来替代它.Formatters提供和PropertyEditor类似的功能,但是提供线程安全特性,也可以实现字符串和对象类型的互相转换. 假设在我们的程序中,需要根据一本书的ISBN字符串得到对应的book

spring mvc 处理接收的map 转换为 json 格式

开发中会遇到传递的map 参数以json 格式处理 ,springMVC中配置如下代码即可实现 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- JSON解析对象 --> <bean

Spring MVC在接收复杂集合参数

Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring MVC在解析集合请求参数时不会自动的转换成JSON数据再解析成相应的集合.以下列举接收List<String>.List<User>.List<Map<String,Object>&g

webService总结(三)——使用CXF + Spring发布webService

近些年来,Spring一直很火,许多框架都能跟Spring完美集成,CXF也不例外.下面,我就介绍一下如何使用CXF + Spring发布webService.我们还是使用前两篇博客使用的实例. 服务端: 目录结构: 这里需要的所有Spring的包,CXF的lib目录下都有. IHelloWorldServer代码: package com.test.server; import javax.jws.WebService; @WebService public interface IHelloW

CXF结合spring发布WS服务,含SOAP services、RESTful services

1.访问:http://localhost:8088/sniperWS/services/查看有哪些服务,包含Available SOAP services.Available RESTful services 2.客户端调用RESTful services:http://localhost:8088/sniperWS/services/address/getSuggestions.query 调用示例:$.ajax({    url: "http://ip:port/sniperWS/serv