二、ApplicationContext的事件机制

来源:互联网 发布:软件销售合同范本 编辑:程序博客网 时间:2024/06/05 18:49

    通过ApplicationEvent类以及ApplicationListener接口,即可实现ApplicationContext的事件处理。

   以下是详细代码:

1、首先是编写 一个继承了ApplicationEvent类的EmailEvent类,代码如下:

package org.meify.bean;import org.springframework.context.ApplicationEvent;/** * 邮件事件 * @description  * @version 1.0 * @author meify  2014-1-2 下午3:04:43 */public class EmailEvent extends ApplicationEvent{public EmailEvent(Object source) {super(source);}public EmailEvent(Object source,String address,String text) {super(source);this.address=address;this.text=text;}private String address; //邮件地址private String text; //邮件内容public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getText() {return text;}public void setText(String text) {this.text = text;}}

2、编写一个实现了ApplicationListener接口的EmailListener类。

package org.meify.bean;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;/** * Spring容器监听器 * 实例化EmailEvent的时候会触发下面的提醒 * 监听器需要在配置文件中进行配置才能发挥监听作用 * @description  * @version 1.0 * @author meify  2014-1-2 下午3:11:23 */public class EmailNotifier implements ApplicationListener{@Overridepublic void onApplicationEvent(ApplicationEvent event) {// TODO Auto-generated method stubif(event instanceof EmailEvent){System.out.println("正在发送邮件。。。");EmailEvent emailEvent=(EmailEvent)event;System.out.println("邮件地址==="+emailEvent.getAddress());System.out.println("邮件正文==="+emailEvent.getText());}else{System.out.println("不是邮件动作。。。");}}}

3、在Spring配置文件中进行相应的配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd                     http://www.springframework.org/schema/context                     http://www.springframework.org/schema/context/spring-context-3.0.xsd                     http://www.springframework.org/schema/aop                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd                     http://www.springframework.org/schema/tx                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><!-- 配置BookBean的实例,在这里给实例初始化参数 注意: id——指定bean的唯一标识 class——bean的实现类,注意必须是实现类而不能是接口或者抽象类什么的 --><!-- 配置emailNotifier监听器 --><bean class="org.meify.bean.EmailNotifier" /><bean id="email" class="org.meify.bean.EmailEvent"><constructor-arg value="hello"></constructor-arg><constructor-arg value="meify@neusoft.com"></constructor-arg><constructor-arg value="大家快来开会啊"></constructor-arg></bean></beans>

4、编写测试程序,直接实例化一个EmailEvent对象,该动作将会触发EmailNotifier相应的动作。

package org.meify.test;import org.meify.bean.AuthorBean;import org.meify.bean.BookBean;import org.meify.bean.EmailEvent;import org.meify.bean.PersonBean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 获取Spring容器并获取bean实例 * 以下代码: * 先获取spring容器,再获取实体bean,将Spring接口与代码耦合在一起,造成代码污染。 * @description  * @version 1.0 * @author meify  2014-1-2 下午2:33:48 */public class Test01 {public static void main(String[] args) {//ApplicationContext的实例即Spring容器,也称之为Spring上下文ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");System.out.println(ctx);//2、测试容器事件EmailEvent email=new EmailEvent("hello","meify@neusoft.com","大家快来开会啊");ctx.publishEvent(email);   //主动触发监听器                }}

最后,控制台输出如下:

正在发送邮件。。。邮件地址===meify@neusoft.com邮件正文===大家快来开会啊

表明当new一个EmailEvent对象的时候会触发ApplicationContext的事件,提醒EmailNotifier进行相应的动作。

注:

ApplicationEvent:容器事件,必须由ApplicationContext发布,任何bean继承了该类即可作为事件触发器。

ApplicationListener:监听器,任何Bean实现该接口均可担任监听器的角色。

0 0
原创粉丝点击