Skip to content

Commit 2612a44

Browse files
committed
add custom springboot event listener readme
1 parent cca3d3c commit 2612a44

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

springboot-source-code-analysis/(8)自定义监听器实战.md

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,100 @@
11
### 自定义监听器实战
22

3+
#### 实现方式一
34

5+
- 实现ApplicationListener接口,ApplicationListener<ApplicationStartedEvent>声明感兴趣的事件
46

5-
![image-20210921163734877](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163734877.png)
7+
```java
8+
@Order(1)
9+
public class FirstListener implements ApplicationListener<ApplicationStartedEvent> {
610

11+
private final static Logger logger = LoggerFactory.getLogger(FirstListener.class);
712

13+
@Override
14+
public void onApplicationEvent(ApplicationStartedEvent event) {
15+
logger.info("设置框架事件监听器【ApplicationListener】成功 : run firstListener");
816

17+
}
18+
}
919

20+
```
1021

22+
- 在spring.factories 配置文件中配置FirstListener事件监听器,key值为org.springframework.context.ApplicationListener
1123

24+
```java
25+
# 通过系统事件监听器,设置自定义事件监听器
26+
org.springframework.context.ApplicationListener=\
27+
com.example.springboot.source.code.analysis.listener.FirstListener
28+
```
1229

1330

1431

15-
![image-20210921163836397](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163836397.png)
32+
#### 实现方式三
1633

34+
- 实现ApplicationListener接口,声明感兴趣的事件ApplicationListener<ApplicationStartedEvent>
1735

36+
```java
37+
@Order(3)
38+
public class ThirdListener implements ApplicationListener<ApplicationStartedEvent> {
1839

40+
private final static Logger logger = LoggerFactory.getLogger(ThirdListener.class);
1941

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() 三种方式注入该事件
2098
2199
22100
23-
![image-20210921163902692](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163902692.png)

0 commit comments

Comments
 (0)