Spring的事件發布機制


一:Spring的事件發布

    ApplicationContext提供了針對Bean的事件傳播功能,其中的主角是publishEvent()方法,通過這個方法可以將事件通知給系統內的監聽器(需實現ApplicationListener接口)。

    ApplicationContext這個接口,是Spring的上下文,通常獲取Bean就需要這個接口,這個接口並不是直接繼承於BeanFactory,其中最著名的是直接繼承了ApplicationPublisher接口,這個接口查看源碼可以發現:只有一個方法,那就是主角 void publishEvent(ApplicationEvent event);

    Spring提供的基於Aware相關的接口有ApplicationContextAware,ResourceloaderAware,ServletContextAware(注意:Struts2也有這個接口,注意區分),最常用的就這三個,而Spring的事件發布機制需要用到ApplicationContextAware接口。

    實現了ApplicationContextAware的Bean,在Bean初始化時將會被注入ApplicationContext實例(因為這個接口里有set(ApplictationContext ctx)方法)

二:有了以上基礎,看示例代碼:

1.首先創建事件類 TradeEvent

package net.wang.test;

import org.springframework.context.ApplicationEvent;

/**
 * 事件Event
 * @author LiuRuoWang
 */
public class TradeEvent extends ApplicationEvent{
	
	public TradeEvent(Object source) {
		super(source);
		System.out.println("事件:TradeEvent event !!");
	}
	
}
事件必須繼承Spring提供的ApplicationEvent抽象類
 
2.然后編寫 事件的發布者HelloWorld:
package net.wang.test;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

/**
 * 事件的發布者
 * @author LiuRuoWang
 */
public class HelloWorld implements ApplicationEventPublisherAware{
	
	private String word;
	
	public void setWord(String word) {
		this.word = word;
	}
	
	private ApplicationEventPublisher tradeEventPublisher;
	
	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
		this.tradeEventPublisher=applicationEventPublisher;
	}
	
	public void say(){
		System.out.println("say:"+this.word);
		TradeEvent tradeEvent = new TradeEvent(new String("HelloWorld!")); 
		this.tradeEventPublisher.publishEvent(tradeEvent);
	}
	
}
其中在say()方法里發布了事件
 
3.最后編寫 事件的接收者EventReceiver:
package net.wang.test;

import org.springframework.context.ApplicationListener;

/**
 * 事件的接收者
 * @author LiuRuoWang
 */
public class EventReceiver implements ApplicationListener<TradeEvent>{

	public void onApplicationEvent(TradeEvent event) {
		System.out.println("監聽到的事件:"+event.getSource());
	}
}
事件的接收者其實是一個監聽器,必須實現ApplicationListener,注意把事件TradeEvent直接寫到泛型中
 
4.applicationContext.xml:
<?xml version="1.0" encoding="GBK"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	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.1.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
	
	<bean name="helloWrold" class="net.wang.test.HelloWorld">
		<property name="word" value="Word!"/>
	</bean>
	
	<bean name="eventReceiver" class="net.wang.test.EventReceiver"/>
	
</beans>

注意把事件的接收者寫入配置文件中

5.測試Test:

package net.wang.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		HelloWorld h = (HelloWorld) ctx.getBean("helloWrold");
		h.say();
	}
}

6.結果顯示:

image

 

結果中已經顯示監聽到的事件,說明成功。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM