Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法

 

参照:https://blog.csdn.net/ilovejava_2010/article/details/7953419

一。写一个事件类继承ApplicationEvent

  

 1 public class FdataEvent extends ApplicationEvent {
 2 
 3     /**
 4      * 
 5      */
 6     private static final long serialVersionUID = 7554563854171912686L;
 7     
 8     private Integer fid ;
 9     private Long time ;
10     private Map<String,Object> datas;
11     
12     public FdataEvent(Object source,Integer fid,Long time,Map<String,Object> datas) {
13         super(source);
14         this.fid = fid;
15         this.time = time;
16         this.datas = datas;
17     }
18 
19     public Integer getFid() {
20         return fid;
21     }
22 
23     public void setFid(Integer fid) {
24         this.fid = fid;
25     }
26 
27     public Long getTime() {
28         return time;
29     }
30 
31     public void setTime(Long time) {
32         this.time = time;
33     }
34 
35     public Map<String, Object> getDatas() {
36         return datas;
37     }
38 
39     public void setDatas(Map<String, Object> datas) {
40         this.datas = datas;
41     }
42 
43 }

 

二。写一个监听类实现ApplicationListener中的onApplicationEvent方法

 

 1 @Component
 2 public class FdataEventListener implements ApplicationListener<FdataEvent> {
 3 
 4     @Override
 5     public void onApplicationEvent(FdataEvent event) {
 6         //写业务逻辑
 7 
 8     }
 9 
10 }

 

三。获取Spring引用上下环境ApplicationContext,通过ApplicationContext调用publishEvent方法

  1.通过main方法实现

  

1 public class Test {
2     public static void main(String[] args) {
3         ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
4         
5 
6         FdataEvent event = new FdataEvent(this, fid, timeStamp, param);
7         context.publishEvent(event);
8        
9     }

 

  2.在service层中调用SpringContextUtil中的publishEvent方法。

  

 1 public class SpringContextUtil implements ApplicationContextAware {
 2     
 3     // Spring应用上下文环境  
 4     private static ApplicationContext applicationContext;  
 5      
 6     @Override
 7     public void setApplicationContext(ApplicationContext applicationContext) {  
 8         SpringContextUtil.applicationContext = applicationContext;  
 9     }  
10      
11     public static ApplicationContext getApplicationContext() {  
12         return applicationContext;  
13     }  
14     /** 
15      * 获取spring管理的bean
16      *  
17      * @param name 
18      * @return T
19      * @throws BeansException 
20      */  
21     @SuppressWarnings("unchecked")
22     public static <T> T getBean(Class<T> clazz,String name) throws BeansException {  
23         return (T)applicationContext.getBean(name);  
24     }
25     /**
26      * 获取spring管理的bean
27      * @param clazz
28      * @return
29      */
30     public static <T> T getBean(Class<T> clazz) {
31         String name = StringUtils.uncapitalize(clazz.getSimpleName());
32         return getBean(clazz,name);
33     }
34     /**
35      * 发布事件
36      * @param event
37      */
38     public static void publishEvent(ApplicationEvent event) {
39         applicationContext.publishEvent(event);
40     }
41     
42 
43 }

 

posted @ 2018-11-07 14:36  郑某人1  阅读(745)  评论(0编辑  收藏  举报