Spring的ApplicationEvent的使用
2016-01-21 10:06
441 查看
Spring的ApplicationEvent的使用
Spring 3.0中提供了很多类似*Aware的类,其中ApplicationContextAware接口可以实现我们在初始化bean的时候给bean注入ApplicationConxt(Spring上下文对象)对象。ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。因此,我们只需要构造好我们自己的ApplicationEvent和ApplicationListener,就可以在系统中实现相应的监听器。
下面以增加学生的示例来演示如何构造Spring的监听器,StudentAddEvent是监听的事件对象,StudentAddListener是事件的监听器(负责处理接收到的监听事件),StudentAddBean负责触发StudentAddEvent事件。具体步骤如下:
1[b]. 定义StudentAddEvent监听事件[/b]
新建StudentAddEvent类,实现抽象类
org.springframework.context.ApplicationEvent
StudentAddEvent类中需要实现自己的构造函数,具体代码如下:
2[b]. 定义StudentAddListener监听器[/b]
新建StudentAddListener类,实现接口
org.springframework.context.ApplicationListener
中的onApplicationEvent方法,在该方法中只处理StudentAddEvent类型的ApplicationEvent事件,代码如下:
3[b]. 定义StudentAddBean触发StudentAddEvent事件[/b]
新建StudentAddBean类,实现接口
org.springframework.context.ApplicationContextAware
中的setApplicationContext方法,在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件,具体代码如下:
4[b]. applicationContext.xml配置文件[/b]
5[b]. 说明[/b]
ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
6[b]. 执行StudentAddBean的main函数,结果如下:[/b]
7[b]. 测试工程下载地址:[/b]
下载地址:http://download.csdn.net/detail/wgw335363240/4022181
Spring 3.0中提供了很多类似*Aware的类,其中ApplicationContextAware接口可以实现我们在初始化bean的时候给bean注入ApplicationConxt(Spring上下文对象)对象。ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。因此,我们只需要构造好我们自己的ApplicationEvent和ApplicationListener,就可以在系统中实现相应的监听器。
下面以增加学生的示例来演示如何构造Spring的监听器,StudentAddEvent是监听的事件对象,StudentAddListener是事件的监听器(负责处理接收到的监听事件),StudentAddBean负责触发StudentAddEvent事件。具体步骤如下:
1[b]. 定义StudentAddEvent监听事件[/b]
新建StudentAddEvent类,实现抽象类
org.springframework.context.ApplicationEvent
StudentAddEvent类中需要实现自己的构造函数,具体代码如下:
package com<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>trs<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>spring<strong style="color: black; background-color: rgb(160, 255, 255);">.event</strong>; import org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationEvent; /** * 增加学生的监听事件 */ public class StudentAddEvent extends ApplicationEvent { /** * */ private static final long serialVersionUID = 20L; /** * 学生姓名 */ private String m_sStudentName; /** * @param source */ public StudentAddEvent(Object source, String _sStudentName) { super(source); this<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>m_sStudentName = _sStudentName; } /** * 获取学生姓名 * * @return */ public String getStudentName() { return m_sStudentName; } }
2[b]. 定义StudentAddListener监听器[/b]
新建StudentAddListener类,实现接口
org.springframework.context.ApplicationListener
中的onApplicationEvent方法,在该方法中只处理StudentAddEvent类型的ApplicationEvent事件,代码如下:
package com<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>trs<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>spring<strong style="color: black; background-color: rgb(160, 255, 255);">.event</strong>; import org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationEvent; import org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationListener; public class StudentAddListener implements ApplicationListener { /* * (non-Javadoc) * * @see * org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationListener#onApplicationEvent(org * <strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationEvent) */ public void onApplicationEvent(ApplicationEvent _<strong style="color: black; background-color: rgb(255, 153, 153);">event</strong>) { // 1<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>判断是否是增加学生对象的事件 if (!(_<strong style="color: black; background-color: rgb(255, 153, 153);">event</strong> instanceof StudentAddEvent)) { return; } // 2<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>是增加学生事件的对象,进行逻辑处理,比如记日志、积分等 StudentAddEvent studentAddEvent = (StudentAddEvent) _<strong style="color: black; background-color: rgb(255, 153, 153);">event</strong>; System<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>out<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>println("增加了学生:::" + studentAddEvent<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>getStudentName()); } }
3[b]. 定义StudentAddBean触发StudentAddEvent事件[/b]
新建StudentAddBean类,实现接口
org.springframework.context.ApplicationContextAware
中的setApplicationContext方法,在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件,具体代码如下:
package com<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>trs<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>spring<strong style="color: black; background-color: rgb(160, 255, 255);">.event</strong>; import org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>beans<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>BeansException; import org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationContext; import org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationContextAware; import org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>support<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ClassPathXmlApplicationContext; public class StudentAddBean implements ApplicationContextAware { /** * 定义Spring上下文对象 */ private ApplicationContext m_applicationContext = null; /* * (non-Javadoc) * * @see * org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationContextAware#setApplicationContext * (org<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>springframework<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>context<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>ApplicationContext) */ public void setApplicationContext(ApplicationContext _applicationContext) throws BeansException { this<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>m_applicationContext = _applicationContext; } /** * 增加一个学生 * * @param _sStudentName */ public void addStudent(String _sStudentName) { // 1<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>构造一个增加学生的事件 StudentAddEvent aStudentEvent = new StudentAddEvent( m_applicationContext, _sStudentName); // 2<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>触发增加学生事件 m_applicationContext<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>publishEvent(aStudentEvent); } /** * @param args */ public static void main(String[] args) { String[] xmlConfig = new String[] { "applicationContext<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>xml" }; // 使用ApplicationContext来初始化系统 ApplicationContext context = new ClassPathXmlApplicationContext( xmlConfig); StudentAddBean studentBean = (StudentAddBean) context <strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>getBean("StudentAddBean"); studentBean<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>addStudent("我是第一个学生"); studentBean<strong style="color: black; background-color: rgb(160, 255, 255);">.</strong>addStudent("第二个学生已经添加"); } }
4[b]. applicationContext.xml配置文件[/b]
<bean id="StudentAddBean" class="com.trs.spring.event.StudentAddBean"></bean> <bean id="StudentAddListener" class="com.trs.spring.event.StudentAddListener"></bean> |
ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
6[b]. 执行StudentAddBean的main函数,结果如下:[/b]
增加了学生:::我是第一个学生 增加了学生:::第二个学生已经添加 |
下载地址:http://download.csdn.net/detail/wgw335363240/4022181
相关文章推荐
- 微信处理红包
- 2.4 The Object Model -- Computed Properties and Aggregate Data with @each(计算的属性和使用@each聚合数据)
- android中的提示信息显示方法(toast应用)
- IOS无限滚动
- iOS通知中心的使用流程
- 最新Google App Engine应用申请教程
- NSUserDefaults -- 写读&配置
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
- Android Studio 断点调试(debug)
- Android开发之使用ExifInterface获取拍照后的图片属性
- 源码学习 : View视图 生命周期及各方法的作用(1)
- Swift 基础知识
- 关于ADT和AndroidStudio集成SVN的一些小问题
- iOS开发-动态和静态FrameWork
- IOS实用的小图表
- ANDROID_DAILY_OVERRIDE 错误
- Android内存泄露与内存溢出是什么,什么情况是内存泄露,什么情况是内存溢出
- android加固系列—5.加固前先学会破解,hook(钩子)jni层系统api
- android中的sqlite注意事项
- iOS开发:初识xib