歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring的事件發布機制

Spring的事件發布機制

日期:2017/3/1 9:13:44   编辑:Linux編程

一: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 !!");
}

}

<font face="Verdana">事件必須繼承Spring提供的ApplicationEvent抽象類</font>

<font face="Verdana"></font>

<font face="Verdana">2.然後編寫 事件的發布者HelloWorld:</font>

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.結果顯示:

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

Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm

運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved