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

 

 

  

 

 

posted @ 2018-11-29 10:07  壬人任仁  阅读(17504)  评论(0编辑  收藏  举报