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

<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 的详细介绍请点这里
Spring 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2016-07/133642.htm
今日推荐