Spring中使用ApplicationContext进行事件的监听之ApplicationEvent,ApplicationListener
来源:互联网 发布:手机自动优化软件 编辑:程序博客网 时间:2024/06/07 15:48
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. 定义StudentAddEvent监听事件
新建StudentAddEvent类,实现抽象类
org.springframework.context.ApplicationEvent
StudentAddEvent类中需要实现自己的构造函数,具体代码如下:
2. 定义StudentAddListener监听器
新建StudentAddListener类,实现接口
org.springframework.context.ApplicationListener
中的onApplicationEvent方法,在该方法中只处理StudentAddEvent类型的ApplicationEvent事件,代码如下:
3. 定义StudentAddBean触发StudentAddEvent事件
新建StudentAddBean类,实现接口
org.springframework.context.ApplicationContextAware
中的setApplicationContext方法,在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件,具体代码如下;
4. applicationContext.xml配置文件
<bean id="StudentAddBean" class="com.trs.spring.event.StudentAddBean"></bean>
<bean id="StudentAddListener" class="com.trs.spring.event.StudentAddListener"></bean>
5. 说明
ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
6. 执行StudentAddBean的main函数,结果如下:
增加了学生:::我是第一个学生
增加了学生:::第二个学生已经添加
- Spring中使用ApplicationContext进行事件的监听之ApplicationEvent,ApplicationListener
- Spring ApplicationContext事件机制:ApplicationEvent与ApplicationListener
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring的监听事件ApplicationListener和ApplicationEvent及@EventListener用法
- Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法
- Spring 通过ApplicationListener&ApplicationEvent实现事件监听
- Spring 的监听ApplicationListener 和 ApplicationEvent
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent ,ApplicationListener,ApplicationContextAware
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent和ApplicationListener
- Spring 的 ApplicationEvent and ApplicationListener
- Spring 的 ApplicationEvent and ApplicationListener
- Spring的ApplicationEvent和ApplicationListener
- json小记
- 面向对象程序设计上机练习九(对象指针)
- 面向对像编程实例
- OSI参考模型简介
- 模板方法模式设计答题问题
- Spring中使用ApplicationContext进行事件的监听之ApplicationEvent,ApplicationListener
- scope参数错误或没有scope权限解决方法
- 2074--叠框,2091空心三角形
- JSP -- EL表达式
- Java基础--Android中泛型的应用(二)
- Producer-Consumer模型:一、简介
- 368. Largest Divisible Subset
- java的jre和jdk的安装和配置很容易
- Tree Traversals Again (PAT) - 树的遍历问题 (网络搜集的代码;注释为自己添加)