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 }

待续.......

猜你喜欢

转载自www.cnblogs.com/biehongli/p/10202541.html