Springboot事件监听

先看一个demo,加入依赖

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>1.8</maven.compiler.source>
  4. <maven.compiler.target>1.8</maven.compiler.target>
  5. </properties>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>1.5.4.RELEASE</version>
  10. </parent>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter</artifactId>
  15. </dependency>
  16. </dependencies>

定义一个自定义事件,继承ApplicationEvent类

  1. /**
  2. * 定义事件
  3. *
  4. */
  5. public class MyApplicationEvent extends ApplicationEvent {
  6. private static final long serialVersionUID = 1L;
  7. public MyApplicationEvent(Object source) {
  8. super(source);
  9. }
  10. }

定义一个事件监听器MyApplicationListener实现ApplicationListener接口,

  1. package com.zhihao.miao;
  2. import org.springframework.context.ApplicationListener;
  3. public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {
  4. public void onApplicationEvent(MyApplicationEvent event) {
  5. System.out.println("接收到事件:"+event.getClass());
  6. }
  7. }

主测试类:

  1. package com.zhihao.miao;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. @SpringBootApplication
  6. public class Application {
  7. public static void main(String[] args) {
  8. SpringApplication application = new SpringApplication(Application.class);
  9. //配置事件监听器
  10. application.addListeners(new MyApplicationListener());
  11. ConfigurableApplicationContext context =application.run(args);
  12. //发布事件
  13. context.publishEvent(new MyApplicationEvent(new Object()));
  14. context.close();
  15. }
  16. }

打印结果:format_png

总结:
springboot事件监听的流程:

  1. 自定义事件,一般是继承ApplicationEvent抽象类。
  2. 定义事件监听器,一般是实现ApplicationListener接口。
  3. 配置监听器,启动的时候,需要把监听器加入到spring容器中。
  4. 发布事件。

其中第三步(将监听器纳入到spring容器)除了上面的方法之外,

  1. application.addListeners(new MyApplicationListener());

还有三种方法

第二种方式
直接在MyApplicationListener类上加上@Component注解,纳入spring容器管理

  1. package com.zhihao.miao;
  2. import org.springframework.context.ApplicationListener;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> {
  6. public void onApplicationEvent(MyApplicationEvent event) {
  7. System.out.println("接收到事件:"+event.getClass());
  8. }
  9. }

主类测试:

  1. package com.zhihao.miao;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. @SpringBootApplication
  6. public class Application {
  7. public static void main(String[] args) {
  8. SpringApplication application = new SpringApplication(Application.class);
  9. ConfigurableApplicationContext context =application.run(args);
  10. //发布事件
  11. context.publishEvent(new MyApplicationEvent(new Object()));
  12. context.close();
  13. }
  14. }

format_png 1

第三种方式
在配置文件中配置

  1. context.listener.classes=com.zhihao.miao.MyApplicationListener

源码分析:
进入DelegatingApplicationListener类中的onApplicationEvent方法,getListeners是获取当前项目中的所有事件监听器。

format_png 2

第四种方式
使用@EventListener注解

  1. package com.zhihao.miao;
  2. import org.springframework.context.event.EventListener;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class MyEventHandle {
  6. /**
  7. * 参数任意(为Object)的时候所有事件都会监听到
  8. * 所有,该参数事件,或者其子事件(子类)都可以接收到
  9. */
  10. @EventListener
  11. public void event(Object event){
  12. System.out.println("MyEventHandle 接收到事件:" + event.getClass());
  13. }
  14. }

主类测试:

  1. package com.zhihao.miao;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.context.ConfigurableApplicationContext;
  5. @SpringBootApplication
  6. public class Application {
  7. public static void main(String[] args) {
  8. SpringApplication application = new SpringApplication(Application.class);
  9. ConfigurableApplicationContext context =application.run(args);
  10. //发布事件
  11. context.publishEvent(new MyApplicationEvent(new Object()));
  12. context.close();
  13. }
  14. }

打印结果:

format_png 3

源码分析:
进入@EventListener注解,文档说明中处理@EventListener是依靠EventListenerMethodProcessorbean,然后进入EventListenerMethodProcessorbean中,我们大概看一下流程,可以自己调试

format_png 4

format_png 5

总结
配置事件监听器的四种方法

  1. SpringApplication.addListeners 添加监听器
  2. 把监听器纳入到spring容器中管理
  3. 使用context.listener.classes配置项配置(详细内容参照:DelegatingApplicationListener)
  4. 使用@EventListener注解,在方法上面加入@EventListener注解,且该类需要纳入到spring容器中管理(详细内容参照:EventListenerMethodProcessor,EventListenerFactory)

spring及springboot已经定义好的事件

spring的事件

format_png 6

springboot的事件

format_png 7

测试一下spring自带的事件:

  1. @Component
  2. public class MyEventHandle {
  3. /**
  4. * 监听spring的事件(运用停止事件,Application.stop()方法时候监听到。
  5. *
  6. */
  7. @EventListener
  8. public void eventStop(ContextStoppedEvent event){
  9. System.out.println("应用停止事件==========:"+event.getClass());
  10. }
  11. }

主类测试:

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. ConfigurableApplicationContext context = SpringApplication.run(Application.class,args);
  5. context.stop();
  6. }
  7. }

测试:

format_png 8

发表评论

表情:
评论列表 (有 0 条评论,99人围观)

还没有评论,来说两句吧...

相关阅读

    相关 事件监听(基于SpringBoot示例)

    > 在实际开发中,业务代码与辅助代码的解耦是一个热点话题,如:通过AOP记录入参出参、使用事件监听记录错误信息等是一个不错的选择。 概述:         事件的发布与

    相关 springboot -03 事件监听

      在实际的开发中,常常遇到这种场景: 当某件事情完成后,需要通知其他的模块进行相应的处理。我们可以一个一个的发送请求去通知,但是更好的处理方式是通过事件监听来完成。事件监听