File tree 4 files changed +70
-4
lines changed
springboot-source-code-analysis
main/java/com/example/springboot/source/code/analysis/listener
test/java/com/example/springboot/source/code/analysis
4 files changed +70
-4
lines changed Original file line number Diff line number Diff line change 4
4
5
5
<img src =" https://tva1.sinaimg.cn/large/008i3skNly1gt3lkgk29nj31l60l0ju9.jpg " alt =" image-20210803144505140 " align =" left " width =" 800 " />
6
6
7
+
8
+
7
9
- 系统消息由广播器(Multicaster)发布出去,发布的内容统称为事件(Event),对关键事件产生订阅的是监听器(Listener);
8
10
9
11
Original file line number Diff line number Diff line change 30
30
<groupId >org.springframework.boot</groupId >
31
31
<artifactId >spring-boot-starter-web</artifactId >
32
32
</dependency >
33
+ <dependency >
34
+ <groupId >junit</groupId >
35
+ <artifactId >junit</artifactId >
36
+ <version >4.12</version >
37
+ <scope >test</scope >
38
+ </dependency >
33
39
</dependencies >
34
40
35
41
<build >
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 2
2
3
3
import com .example .springboot .source .code .analysis .event .RainListener ;
4
4
import com .example .springboot .source .code .analysis .event .WeatherRunListener ;
5
+ import com .example .springboot .source .code .analysis .listener .ApplicationContextContainer ;
5
6
import org .junit .Test ;
7
+ import org .junit .runner .RunWith ;
6
8
import org .springframework .beans .factory .annotation .Autowired ;
7
9
import org .springframework .boot .test .context .SpringBootTest ;
10
+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
8
11
9
- @ SpringBootTest
10
- class SpringbootSourceCodeAnalysisApplicationTests {
12
+ @ SpringBootTest (classes = SpringbootSourceCodeAnalysisApplication .class )
13
+ @ RunWith (SpringJUnit4ClassRunner .class )
14
+ public class SpringbootSourceCodeAnalysisApplicationTests {
11
15
12
16
// 引入事件监听器
13
17
@ Autowired
14
18
private WeatherRunListener weatherRunListener ;
15
19
16
20
// 测试事件监听器
17
21
@ Test
18
- public void testEvent (){
22
+ public void testEvent () {
19
23
weatherRunListener .addListener (new RainListener ());
20
24
weatherRunListener .snow ();
21
25
weatherRunListener .rain ();
22
26
}
23
27
28
+ // 测试ContextRefreshEvent事件
24
29
@ 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 () {
26
39
}
27
40
28
41
You can’t perform that action at this time.
0 commit comments