SpringBoot使用監聽器Listener


  監聽器也叫Listener,是servlet的監聽器,可以用於監聽Web應用中某些對象,信息的創建,銷毀,增加,修改,刪除等動作的發生,然后做出相應的響應處理。當范圍對象的狀態發生變化時,服務器自動調用監聽器對象中的方法,常用於統計在線人數和在線用戶,系統加載時進行信息初始化,統計網站的訪問量等。

 

事件流程:
1.自定義事件,一般是繼承ApplicationEvent抽象類
2.定義事件監聽器,一般是實現ApplicationListener接口
3.發布事件

/**
* 簡單事件監聽流程:
* 1.自定義事件,一般是繼承ApplicationEvent抽象類
* 2.定義事件監聽器,一般是實現ApplicationListener接口
* 3.啟動的時候,需要把監聽器加入到spring容器中
* 4.發布事件:ApplicationContext.publishEvent發布事件
*/

 

/**
* 配置監聽器的方法
* 1.context.addApplicationListener(new Mylistener())
* 2.把監聽器加入到spring容器中管理
* 3.在application.properties文件中使用context.listenser.classes配置配置項

* 在application.properties如此寫: #context.listener.classes= com.example.demo.Mylistener
* 4.使用@EventListener注解,在方法上面加入@EventListener注解,同時該類需要納入到spring容器中管理
*
*/

MyEvent.java

 1 package com.example.demo;
 2 
 3 import org.springframework.context.ApplicationEvent;
 4 
 5 /**
 6  * 
 7  * 定義事件
 8  * 還需要定義一個監聽器
 9  *
10  */
11 
12 public class MyEvent extends ApplicationEvent{
13 
14     private static final long serialVersionUID = 1L;
15     
16     public MyEvent(Object source) {
17         //定義成Object類
18         super(source);
19     }
20 }

 

 

Mylistener.java

 1 package com.example.demo;
 2 
 3 import org.springframework.context.ApplicationListener;
 4 import org.springframework.stereotype.Component;
 5 /**
 6  * 定義事件監聽器
 7  * @author Administrator
 8  *
 9  */
10 @Component
11 public class Mylistener implements ApplicationListener<MyEvent>{
12     //<寫要監聽的對象>
13     
14     @Override
15     public void onApplicationEvent(MyEvent event) {
16         System.out.println("我開始監聽"+event.getClass());
17         
18     }
19 
20     
21 }

 

SpringbootApplication.java

 1 package com.example.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 //@EnableConfigurationProperties
 7 @SpringBootApplication
 8 //這個注解囊括了多個注解的大注解
 9 public class SpringbootApplication {
10 
11     public static void main(String[] args) {
12         //SpringApplication.run(SpringbootApplication.class, args);
13         ConfigurableApplicationContext context=SpringApplication.run(SpringbootApplication.class, args);
14         
15
16         //context.addApplicationListener(new Mylistener());//第三條:3.啟動的時候,需要把監聽器加入到spring容器中
17         //如果這里不把監聽器加入到spring容器中,那么需要在監聽器類中加上@component,標注,具體見如下MyEventHandler.java
18         context.publishEvent(new MyEvent(new Object()));  //發布消息       
19         context.close();
20     }
21 }

 

如果不想把監聽器加入到Spring容器中,需要使用@Component注解

 

MyEventHandler.java

package com.example.demo;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class MyEventHandler {

    @EventListener
    public void event(MyEvent event) {
        /**
         * 括號中的參數不能為空,可以任意
         * 若為Object event,則所有事件都可以監聽到
         */
        System.out.println("MyEventHandler又要監聽"+event.getClass());
        
    }
}

 

結果: 

2018-11-29 10:05:37.413  INFO 7040 --- [           main] com.example.demo.SpringbootApplication   : Starting SpringbootApplication on FINEAUUV9OAHWGS with PID 7040 (D:\eclipse-workspace\springboot\target\classes started by Administrator in D:\eclipse-workspace\springboot)
2018-11-29 10:05:37.420  INFO 7040 --- [           main] com.example.demo.SpringbootApplication   : No active profile set, falling back to default profiles: default
2018-11-29 10:05:39.144  INFO 7040 --- [           main] com.example.demo.SpringbootApplication   : Started SpringbootApplication in 2.458 seconds (JVM running for 3.25)
MyEventHandler又要監聽class com.example.demo.MyEvent
我開始監聽class com.example.demo.MyEvent

 

 

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM