|
1 | 1 | ### 自定义监听器实战 |
2 | 2 |
|
| 3 | +#### 实现方式一 |
3 | 4 |
|
| 5 | +- 实现ApplicationListener接口,ApplicationListener<ApplicationStartedEvent>声明感兴趣的事件 |
4 | 6 |
|
5 | | - |
| 7 | +```java |
| 8 | +@Order(1) |
| 9 | +public class FirstListener implements ApplicationListener<ApplicationStartedEvent> { |
6 | 10 |
|
| 11 | + private final static Logger logger = LoggerFactory.getLogger(FirstListener.class); |
7 | 12 |
|
| 13 | + @Override |
| 14 | + public void onApplicationEvent(ApplicationStartedEvent event) { |
| 15 | + logger.info("设置框架事件监听器【ApplicationListener】成功 : run firstListener"); |
8 | 16 |
|
| 17 | + } |
| 18 | +} |
9 | 19 |
|
| 20 | +``` |
10 | 21 |
|
| 22 | +- 在spring.factories 配置文件中配置FirstListener事件监听器,key值为org.springframework.context.ApplicationListener |
11 | 23 |
|
| 24 | +```java |
| 25 | +# 通过系统事件监听器,设置自定义事件监听器 |
| 26 | +org.springframework.context.ApplicationListener=\ |
| 27 | + com.example.springboot.source.code.analysis.listener.FirstListener |
| 28 | +``` |
12 | 29 |
|
13 | 30 |
|
14 | 31 |
|
15 | | - |
| 32 | +#### 实现方式三 |
16 | 33 |
|
| 34 | +- 实现ApplicationListener接口,声明感兴趣的事件ApplicationListener<ApplicationStartedEvent> |
17 | 35 |
|
| 36 | +```java |
| 37 | +@Order(3) |
| 38 | +public class ThirdListener implements ApplicationListener<ApplicationStartedEvent> { |
18 | 39 |
|
| 40 | + private final static Logger logger = LoggerFactory.getLogger(ThirdListener.class); |
19 | 41 |
|
| 42 | + @Override |
| 43 | + public void onApplicationEvent(ApplicationStartedEvent event) { |
| 44 | + logger.info("设置框架事件监听器【ApplicationListener】成功 : run thirdListener"); |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +- 在application.properties 内配置事件监听器,key值为context.listener.classes |
| 50 | + |
| 51 | +```java |
| 52 | +# 通过系统事件监听器,监听事件 |
| 53 | +context.listener.classes=\ |
| 54 | + com.example.springboot.source.code.analysis.listener.ThirdListener,\ |
| 55 | +``` |
| 56 | + |
| 57 | +> application.properties 内配置的 conext.listener.classes 事件监听器执行优先级最高 |
| 58 | +> |
| 59 | +> ```java |
| 60 | +> // context.listener.classes 是通过 DelegatingApplicationListener 委派给 ApplicationListener |
| 61 | +> public class DelegatingApplicationListener implements ApplicationListener<ApplicationEvent>, Ordered { |
| 62 | +> private static final String PROPERTY_NAME = "context.listener.classes"; |
| 63 | +> // 在委派过程中,DelegatingApplicationListener 会将 order 优先级设置为最高 |
| 64 | +> private int order = 0; |
| 65 | +> private SimpleApplicationEventMulticaster multicaster; |
| 66 | +> |
| 67 | +> ``` |
| 68 | +
|
| 69 | +
|
| 70 | +
|
| 71 | +#### 实现方式四 |
| 72 | +
|
| 73 | +- 实现SmartApplicationListener接口 |
| 74 | +
|
| 75 | +```java |
| 76 | +@Order(4) |
| 77 | +public class FourthListener implements SmartApplicationListener { |
| 78 | +
|
| 79 | + private final static Logger logger = LoggerFactory.getLogger(FourthListener.class); |
| 80 | +
|
| 81 | + // 通过重写 supportEventType()方法 判断该监听器对哪些事件感兴趣 |
| 82 | + @Override |
| 83 | + public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) { |
| 84 | + return ApplicationStartedEvent.class.isAssignableFrom(eventType) |
| 85 | + || ApplicationPreparedEvent.class.isAssignableFrom(eventType); |
| 86 | + } |
| 87 | +
|
| 88 | + // 编写事件触发逻辑 |
| 89 | + @Override |
| 90 | + public void onApplicationEvent(ApplicationEvent event) { |
| 91 | + logger.info("设置框架事件监听器【SmartApplicationListener】成功 : run fourthListener"); |
| 92 | + } |
| 93 | +} |
| 94 | +
|
| 95 | +``` |
| 96 | +
|
| 97 | +- 可以通过spring.factories 或 application.properties 或 new SpringApplication().addListeners() 三种方式注入该事件 |
20 | 98 |
|
21 | 99 |
|
22 | 100 |
|
23 | | - |
|
0 commit comments