zoukankan      html  css  js  c++  java
  • 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

     

    结果中已经显示监听到的事件,说明成功。

  • 相关阅读:
    Atitit  Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0
    Atitit  验证 数字验证 非空验证的最佳算法  h5
    Atitit 转移特效attilax总结
    Atitit Loading 动画效果
    Atitit 项目管理 提升开发效率的项目流程方法模型 哑铃型  橄榄型 直板型
    Atitit hibernate3 hinernate4 hibernate5新特性attilax总结
    Atitit js es5 es6新特性 attilax总结
    Atitit mybatis 3 3.2 3.3  3.4 新特性attilax总结
    Atitit spring 3.0 3.1 3.2 4.0 4.3 5.0 新特性
    Atitit jquery  1.4--v1.11  v1.12  v2.0  3.0 的新特性
  • 原文地址:https://www.cnblogs.com/liuruowang/p/4542739.html
Copyright © 2011-2022 走看看