⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 blog.csdn.net/ignorewho/article/details/80702827 「ignorewho」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注**微信公众号:【芋道源码】**有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言将得到认真回复。甚至不知道如何读源码也可以请教噢
  4. 新的源码解析文章实时收到通知。每周更新一篇左右
  5. 认真的源码交流微信群。

springboot进行事件监听有四种方式:

  1. 手工向ApplicationContext中添加监听器
  2. 将监听器装载入spring容器
  3. 在application.properties中配置监听器
  4. 通过@EventListener注解实现事件监听

讲到事件监听,这里我们说下自定义事件和自定义监听器类的实现方式:

  • 自定义事件:继承自ApplicationEvent抽象类,然后定义自己的构造器
  • 自定义监听:实现ApplicationListener<T>接口,然后实现onApplicationEvent方法

下面讲下4种事件监听的具体实现

方式1

首先创建MyListener1类

public class MyListener1 implements ApplicationListener<MyEvent>
{
Logger logger = Logger.getLogger(MyListener1.class);

public void onApplicationEvent(MyEvent event)
{
logger.info(String.format("%s监听到事件源:%s.", MyListener1.class.getName(), event.getSource()));
}
}

然后在springboot应用启动类中获取ConfigurableApplicationContext上下文,装载监听

@SpringBootApplication
public class LisenterApplication
{
public static void main(String[] args)
{
ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args);
//装载监听
context.addApplicationListener(new MyListener1());
}
}

方式2

创建MyListener2类,并使用@Component注解将该类装载入spring容器中

@Component
public class MyListener2 implements ApplicationListener<MyEvent>
{
Logger logger = Logger.getLogger(MyListener2.class);

public void onApplicationEvent(MyEvent event)
{
logger.info(String.format("%s监听到事件源:%s.", MyListener2.class.getName(), event.getSource()));
}
}

方式3

首先创建MyListener3类

public class MyListener3 implements ApplicationListener<MyEvent>
{
Logger logger = Logger.getLogger(MyListener3.class);

public void onApplicationEvent(MyEvent event)
{
logger.info(String.format("%s监听到事件源:%s.", MyListener3.class.getName(), event.getSource()));
}
}

然后在application.properties中配置监听

context.listener.classes=com.listener.MyListener3

方式4

创建MyListener4类,该类无需实现ApplicationListener接口,使用@EventListener装饰具体方法

@Component
public class MyListener4
{
Logger logger = Logger.getLogger(MyListener4.class);

@EventListener
public void listener(MyEvent event)
{
logger.info(String.format("%s监听到事件源:%s.", MyListener4.class.getName(), event.getSource()));
}
}

自定义事件代码如下:

@SuppressWarnings("serial")
public class MyEvent extends ApplicationEvent
{
public MyEvent(Object source)
{
super(source);
}
}

进行测试(在启动类中加入发布事件的逻辑):

@SpringBootApplication
public class LisenterApplication
{
public static void main(String[] args)
{
ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args);
//装载事件
context.addApplicationListener(new MyListener1());
//发布事件
context.publishEvent(new MyEvent("测试事件."));
}
}

启动后,日志打印如下:

2018-06-15 10:51:20.198  INFO 4628 --- [           main] com.listener.MyListener3                 : com.listener.MyListener3监听到事件源:测试事件..
2018-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener4 : com.listener.MyListener4监听到事件源:测试事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener2 : com.listener.MyListener2监听到事件源:测试事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener1 : com.listener.MyListener1监听到事件源:测试事件..

由日志打印可以看出,SpringBoot四种事件的实现方式监听是有序的

完整的代码路径:

https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener

文章目录
  1. 1. 方式1
  2. 2. 方式2
  3. 3. 方式3
  4. 4. 方式4