Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 269f190

Browse files
committed
add context refreshed event sample
1 parent 2612a44 commit 269f190

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

springboot-source-code-analysis/(5)监听器设计模式.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
<img src="https://tva1.sinaimg.cn/large/008i3skNly1gt3lkgk29nj31l60l0ju9.jpg" alt="image-20210803144505140" align="left" width="800" />
66

7+
8+
79
- 系统消息由广播器(Multicaster)发布出去,发布的内容统称为事件(Event),对关键事件产生订阅的是监听器(Listener);
810

911

springboot-source-code-analysis/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<groupId>org.springframework.boot</groupId>
3131
<artifactId>spring-boot-starter-web</artifactId>
3232
</dependency>
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.12</version>
37+
<scope>test</scope>
38+
</dependency>
3339
</dependencies>
3440

3541
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.springboot.source.code.analysis.listener;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.ApplicationListener;
5+
import org.springframework.context.event.ContextRefreshedEvent;
6+
import org.springframework.core.annotation.Order;
7+
import org.springframework.lang.Nullable;
8+
import org.springframework.stereotype.Component;
9+
10+
/**
11+
* Created by ipipman on 2021/9/21.
12+
*
13+
* @version V1.0
14+
* @Package com.example.springboot.source.code.analysis.listener
15+
* @Description: (用一句话描述该文件做什么)
16+
* @date 2021/9/21 5:49 下午
17+
*/
18+
@Component
19+
@Order(1)
20+
public class ApplicationContextContainer implements ApplicationListener<ContextRefreshedEvent> {
21+
22+
private ApplicationContext applicationContext;
23+
24+
private static ApplicationContextContainer applicationContextContainer;
25+
26+
@Override
27+
public void onApplicationEvent(@Nullable ContextRefreshedEvent event) {
28+
if (event != null && applicationContext == null) {
29+
synchronized (ApplicationContextContainer.class) {
30+
if (applicationContext == null) {
31+
applicationContext = event.getApplicationContext();
32+
}
33+
}
34+
}
35+
applicationContextContainer = this;
36+
}
37+
38+
public static ApplicationContextContainer getInstance() {
39+
return applicationContextContainer;
40+
}
41+
42+
public <T> T getBean(Class<T> clazz) {
43+
return applicationContext.getBean(clazz);
44+
}
45+
}

springboot-source-code-analysis/src/test/java/com/example/springboot/source/code/analysis/SpringbootSourceCodeAnalysisApplicationTests.java

+17-4
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,40 @@
22

33
import com.example.springboot.source.code.analysis.event.RainListener;
44
import com.example.springboot.source.code.analysis.event.WeatherRunListener;
5+
import com.example.springboot.source.code.analysis.listener.ApplicationContextContainer;
56
import org.junit.Test;
7+
import org.junit.runner.RunWith;
68
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
811

9-
@SpringBootTest
10-
class SpringbootSourceCodeAnalysisApplicationTests {
12+
@SpringBootTest(classes = SpringbootSourceCodeAnalysisApplication.class)
13+
@RunWith(SpringJUnit4ClassRunner.class)
14+
public class SpringbootSourceCodeAnalysisApplicationTests {
1115

1216
// 引入事件监听器
1317
@Autowired
1418
private WeatherRunListener weatherRunListener;
1519

1620
// 测试事件监听器
1721
@Test
18-
public void testEvent(){
22+
public void testEvent() {
1923
weatherRunListener.addListener(new RainListener());
2024
weatherRunListener.snow();
2125
weatherRunListener.rain();
2226
}
2327

28+
// 测试ContextRefreshEvent事件
2429
@Test
25-
void contextLoads() {
30+
public void testContextRefreshEvent() {
31+
WeatherRunListener weatherRunListener = ApplicationContextContainer.getInstance().getBean(WeatherRunListener.class);
32+
weatherRunListener.snow();
33+
weatherRunListener.rain();
34+
}
35+
36+
37+
@Test
38+
public void contextLoads() {
2639
}
2740

2841

0 commit comments

Comments
 (0)