通過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{
@Override
public void onApplicationEvent(ApplicationEvent event) {
// TODO Auto-generated method stub
if(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="[email protected]"></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","[email protected]","大家快來開會啊");
ctx.publishEvent(email); //主動觸發監聽器
}
}
最後,控制檯輸出如下:
正在發送郵件。。。
郵件地址[email protected]
郵件正文===大家快來開會啊
表明當new一個EmailEvent對象的時候會觸發ApplicationContext的事件,提醒EmailNotifier進行相應的動作。
注:
ApplicationEvent:容器事件,必須由ApplicationContext發佈,任何bean繼承了該類即可作爲事件觸發器。
ApplicationListener:監聽器,任何Bean實現該接口均可擔任監聽器的角色。