File tree 8 files changed +151
-0
lines changed
springboot-source-code-analysis
java/com/example/springboot/source/code/analysis
8 files changed +151
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### 自定义监听器实战
2
+
3
+
4
+
5
+ ![ image-20210921163734877] (/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163734877.png)
6
+
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ ![ image-20210921163836397] (/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163836397.png)
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+ ![ image-20210921163902692] (/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163902692.png)
Original file line number Diff line number Diff line change 1
1
package com .example .springboot .source .code .analysis ;
2
2
3
3
import com .example .springboot .source .code .analysis .initializer .SecondInitializer ;
4
+ import com .example .springboot .source .code .analysis .listener .SecondListener ;
4
5
import org .springframework .boot .SpringApplication ;
5
6
import org .springframework .boot .autoconfigure .SpringBootApplication ;
6
7
@@ -14,6 +15,9 @@ public static void main(String[] args) {
14
15
// 手动添加一个框架的初始化器
15
16
springApplication .addInitializers (new SecondInitializer ());
16
17
18
+ // 手动添加一个框架事件监听器
19
+ springApplication .addListeners (new SecondListener ());
20
+
17
21
springApplication .run ();
18
22
19
23
}
Original file line number Diff line number Diff line change
1
+ package com .example .springboot .source .code .analysis .listener ;
2
+
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+ import org .springframework .boot .context .event .ApplicationStartedEvent ;
6
+ import org .springframework .context .ApplicationListener ;
7
+ import org .springframework .core .annotation .Order ;
8
+
9
+ /**
10
+ * Created by ipipman on 2021/9/21.
11
+ *
12
+ * @version V1.0
13
+ * @Package com.example.springboot.source.code.analysis.listener
14
+ * @Description: (用一句话描述该文件做什么)
15
+ * @date 2021/9/21 4:12 下午
16
+ */
17
+
18
+ @ Order (1 )
19
+ public class FirstListener implements ApplicationListener <ApplicationStartedEvent > {
20
+
21
+ private final static Logger logger = LoggerFactory .getLogger (FirstListener .class );
22
+
23
+ @ Override
24
+ public void onApplicationEvent (ApplicationStartedEvent event ) {
25
+ logger .info ("设置框架事件监听器【ApplicationListener】成功 : run firstListener" );
26
+
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .springboot .source .code .analysis .listener ;
2
+
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+ import org .springframework .boot .context .event .ApplicationPreparedEvent ;
6
+ import org .springframework .boot .context .event .ApplicationStartedEvent ;
7
+ import org .springframework .context .ApplicationEvent ;
8
+ import org .springframework .context .event .SmartApplicationListener ;
9
+ import org .springframework .core .annotation .Order ;
10
+
11
+ /**
12
+ * Created by ipipman on 2021/9/21.
13
+ *
14
+ * @version V1.0
15
+ * @Package com.example.springboot.source.code.analysis.listener
16
+ * @Description: (用一句话描述该文件做什么)
17
+ * @date 2021/9/21 4:26 下午
18
+ */
19
+ @ Order (4 )
20
+ public class FourthListener implements SmartApplicationListener {
21
+
22
+ private final static Logger logger = LoggerFactory .getLogger (FourthListener .class );
23
+
24
+ @ Override
25
+ public boolean supportsEventType (Class <? extends ApplicationEvent > eventType ) {
26
+ return ApplicationStartedEvent .class .isAssignableFrom (eventType )
27
+ || ApplicationPreparedEvent .class .isAssignableFrom (eventType );
28
+ }
29
+
30
+ @ Override
31
+ public void onApplicationEvent (ApplicationEvent event ) {
32
+ logger .info ("设置框架事件监听器【SmartApplicationListener】成功 : run fourthListener" );
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .springboot .source .code .analysis .listener ;
2
+
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+ import org .springframework .boot .context .event .ApplicationStartedEvent ;
6
+ import org .springframework .context .ApplicationListener ;
7
+ import org .springframework .core .annotation .Order ;
8
+
9
+ /**
10
+ * Created by ipipman on 2021/9/21.
11
+ *
12
+ * @version V1.0
13
+ * @Package com.example.springboot.source.code.analysis.listener
14
+ * @Description: (用一句话描述该文件做什么)
15
+ * @date 2021/9/21 4:19 下午
16
+ */
17
+ @ Order (2 )
18
+ public class SecondListener implements ApplicationListener <ApplicationStartedEvent > {
19
+
20
+ private static final Logger logger = LoggerFactory .getLogger (SecondListener .class );
21
+
22
+ @ Override
23
+ public void onApplicationEvent (ApplicationStartedEvent event ) {
24
+ logger .info ("设置框架事件监听器【ApplicationListener】成功 : run secondListener" );
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package com .example .springboot .source .code .analysis .listener ;
2
+
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+ import org .springframework .boot .context .event .ApplicationStartedEvent ;
6
+ import org .springframework .context .ApplicationListener ;
7
+ import org .springframework .core .annotation .Order ;
8
+
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 4:21 下午
17
+ */
18
+ @ Order (3 )
19
+ public class ThirdListener implements ApplicationListener <ApplicationStartedEvent > {
20
+
21
+ private final static Logger logger = LoggerFactory .getLogger (ThirdListener .class );
22
+
23
+ @ Override
24
+ public void onApplicationEvent (ApplicationStartedEvent event ) {
25
+ logger .info ("设置框架事件监听器【ApplicationListener】成功 : run thirdListener" );
26
+ }
27
+ }
Original file line number Diff line number Diff line change 2
2
org.springframework.context.ApplicationContextInitializer=\
3
3
com.example.springboot.source.code.analysis.initializer.FirstInitializer
4
4
5
+
6
+ # 通过系统事件监听器,设置自定义事件监听器
7
+ org.springframework.context.ApplicationListener=\
8
+ com.example.springboot.source.code.analysis.listener.FirstListener
Original file line number Diff line number Diff line change 1
1
# 通过系统初始化器,设置环境属性
2
2
context.initializer.classes =\
3
3
com.example.springboot.source.code.analysis.initializer.ThirdInitializer
4
+
5
+ # 通过系统事件监听器,监听事件
6
+ context.listener.classes =\
7
+ com.example.springboot.source.code.analysis.listener.ThirdListener,\
8
+ com.example.springboot.source.code.analysis.listener.FourthListener
You can’t perform that action at this time.
0 commit comments