1. 程式人生 > >SpringBoot的事件監聽

SpringBoot的事件監聽

事件監聽的流程分為三步:
1、自定義事件,一般是繼承ApplicationEvent抽象類。
2、定義事件監聽器,一般是實現ApplicationListener介面。
3、a、啟動的時候,需要將監聽器加入到Spring容器中。

   b、或者將監聽器加入到容器中。@Component

   c、使用@EventListener註解,在方法上面加入@EventListener註解,且該類需要納入到spring容器中進行管理。 

   d、或者使用配置項,在預設的配置檔案application.properties配置檔案裡面加入進去,context.listener.classes配置項。context.listener.classes=com.bie.license.ListenerApplicationListener


4、釋出事件。使用ApplicationContext.publishEvent釋出事件。

1、事件監聽第一步,定義一個事件,繼承ApplicationEvent抽象類。

 1 package com.bie.license;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 
 5 /**
 6  * 
 7  * @Description TODO
 8  * @author biehl
 9  * @Date 2018年12月31日 下午5:02:43
10  * 1、第一步,建立一個事件,繼承ApplicationEvent
11 * 定義事件 12 */ 13 14 public class EventApplicationEvent extends ApplicationEvent{ 15 16 /** 17 * 18 */ 19 private static final long serialVersionUID = 1L; 20 21 public EventApplicationEvent(Object source) { 22 super(source); 23 } 24 25 }

2、第二步,定義一個監聽器,看看是監聽那個事件。繼承ApplicationListener類。

 1 package com.bie.license;
 2 
 3 import org.springframework.context.ApplicationListener;
 4 
 5 /**
 6  * 
 7  * @Description TODO
 8  * @author biehl
 9  * @Date 2018年12月31日 下午5:05:46
10  * 2、第二步,定義一個監聽器,監聽哪一個事件。如果不執行第三步,將ListenerApplicationListener加入到容器中,使用@Component註解也可以的。
11  */
12 
13 public class ListenerApplicationListener implements ApplicationListener<EventApplicationEvent>{
14 
15     @Override
16     public void onApplicationEvent(EventApplicationEvent event) {
17         System.out.println("接受到事件 : " + event.getClass());
18     }
19 
20 }

3、第三步,啟動的時候,需要將監聽器加入到Spring容器中。釋出事件。使用ApplicationContext.publishEvent釋出事件。

 1 package com.bie.license;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 
 7 /**
 8  * 
 9  * @Description TODO
10  * @author biehl
11  * @Date 2018年12月31日 下午5:09:10
12  *
13  */
14 @SpringBootApplication
15 public class ListenerApplication {
16 
17     public static void main(String[] args) {
18         SpringApplication app = new SpringApplication(ListenerApplication.class);
19         app.addListeners(new ListenerApplicationListener());//app.addListeners(new ListenerApplicationListener());或者將ListenerApplicationListener加入到bean中也可以。
20         ConfigurableApplicationContext context = app.run(args);
21         // 第三步,釋出事件
22         context.publishEvent(new EventApplicationEvent(new Object()));
23         // 關閉
24         context.close();
25     }
26 }

執行效果如下所示:

使用@EventListener註解來進行加入到Spring容器中:

 1 package com.bie.license;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 import org.springframework.context.event.EventListener;
 5 import org.springframework.stereotype.Component;
 6 
 7 /**
 8  * 
 9  * @Description TODO
10  * @author biehl
11  * @Date 2018年12月31日 下午5:38:10
12  *
13  */
14 @Component
15 public class EventHandle {
16 
17     /**
18      * 引數任意
19      */
20     @EventListener
21     public void event(ApplicationEvent event) {
22         System.out.println("EventHandle 接受到事件 : "  + event.getClass());
23     }
24 }

 

待續.......