spring容器事件

来源:互联网 发布:怎么用淘宝微淘 编辑:程序博客网 时间:2024/06/06 13:23
spring的ApplicationContext能够发布事件并且允许注册相应的事件监听器,它拥有一套完善的事件发布和监听机制.


事件体系:事件,事件监听器,事件源,事件监听器注册表,事件广播器.其中,事件源是事件的产生者,事件监听器注册表就是保存事件监听器的地方,事件广播器是事件与事件监听器沟通的桥梁,负责将事件通知给事件监听器.


事件类:ApplicationEvent包含ApplicationContextEvent和RequestHandledEvent两个子类,容器事件ApplicationContextEvent有ContextClosedEvent,ContextRefreshedEvent,ContextStartedEvent,ContextStoppedEvent四个子类,Web应用相关事件RequestHandledEvent有ServletRequestHandledEvent,PortletRequestHandledEvent两个子类.
事件监听器接口:ApplicationListener<E extends ApplicationEvent>包含SmartApplicationListener子接口.
事件广播器:ApplicationEventMulticaster->AbstractApplicationEventMulticaster->SimpleApplicationEventMulticaster.


spring容器事件体系的具体实现:主要参看org.springframework.context.support.AbstractApplicationContext#refresh

[java] view plain copy
  1. ...前略...  
  2. // Initialize event multicaster for this context.1.为此上下文初始化事件广播器  
  3. initApplicationEventMulticaster();  
  4.   
  5. // Initialize other special beans in specific context subclasses.  
  6. onRefresh();  
  7.   
  8. // Check for listener beans and register them.2:检查并注册监听器  
  9. registerListeners();  
  10.   
  11. // Instantiate all remaining (non-lazy-init) singletons.  
  12. finishBeanFactoryInitialization(beanFactory);  
  13.   
  14. // Last step: publish corresponding event.3:发布相应事件  
  15. finishRefresh();  
  16. ...后略...  

1.检查是否有名为applicationEventMulticaster的bean,如果有,就会将此bean作为事件广播器,否则将SimpleApplicationEventMulticaster实例作为事件广播器
2.找出监听器并注册到事件监听器注册表
3.调用事件广播器发布相应事件


下面是个模拟实例:
1.定义事件.

[java] view plain copy
  1. package org.exam.event;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.event.ApplicationContextEvent;  
  4. /** 
  5.  * Created by xin on 15/1/28. 
  6.  */  
  7. public class MailSenderEvent extends ApplicationContextEvent{  
  8.     private String to;  
  9.     public MailSenderEvent(ApplicationContext source,String to) {  
  10.         super(source);  
  11.         this.to=to;  
  12.     }  
  13.     public String getTo() {  
  14.         return to;  
  15.     }  
  16. }  
2.定义事件监听.
[java] view plain copy
  1. package org.exam.event;  
  2. import org.springframework.context.ApplicationListener;  
  3. public class MailSenderListener implements ApplicationListener<MailSenderEvent> {  
  4.     @Override  
  5.     public void onApplicationEvent(MailSenderEvent event) {  
  6.         System.out.println(getClass()+":向"+event.getTo()+"发送完一封邮件!");  
  7.     }  
  8. }  
3.使用org.springframework.context.ApplicationContext#publishEvent来发布事件.此类必须实现ApplicationContextAware接口,以便容器启动时注入ApplicationContext实例,这样此实例才有发布事件的能力.
[java] view plain copy
  1. package org.exam.event;  
  2. import org.springframework.beans.BeansException;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.ApplicationContextAware;  
  5. import org.springframework.context.ApplicationEvent;  
  6. /** 
  7.  * Created by xin on 15/1/28. 
  8.  */  
  9. public class MailSender implements ApplicationContextAware {  
  10.     private ApplicationContext applicationContext;  
  11.     @Override  
  12.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  13.         this.applicationContext=applicationContext;  
  14.     }  
  15.     public void sendMail(String to){  
  16.         System.out.println(getClass()+":模拟发送邮件:");  
  17.         ApplicationEvent applicationEvent=new MailSenderEvent(applicationContext,to);  
  18.         applicationContext.publishEvent(applicationEvent);  
  19.     }  
  20. }  
4.测试.
[java] view plain copy
  1. package org.exam.config;  
  2. import org.exam.event.MailSender;  
  3. import org.exam.event.MailSenderListener;  
  4. import org.springframework.context.ApplicationListener;  
  5. import org.springframework.context.annotation.Bean;  
  6. import org.springframework.context.annotation.Configuration;  
  7. /** 
  8.  * Created by xin on 15/1/28. 
  9.  */  
  10. @Configuration  
  11. public class AppConfig {  
  12.     @Bean  
  13.     public ApplicationListener applicationListener(){  
  14.         return new MailSenderListener();  
  15.     }  
  16.     @Bean  
  17.     public MailSender mailSender(){  
  18.         return new MailSender();  
  19.     }  
  20. }  
  21.   
  22. package org.exam.event;  
  23. import org.exam.config.AppConfig;  
  24. import org.springframework.context.ApplicationContext;  
  25. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  26. /** 
  27.  * Created by xin on 15/1/29. 
  28.  */  
  29. public class Main {  
  30.     public static void main(String[] args) {  
  31.         ApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);  
  32.         MailSender mailSender= (MailSender) context.getBean("mailSender");  
  33.         mailSender.sendMail("xiejx618@xx.io");  
  34.     }  
  35. }  
原创粉丝点击