From cca3d3c07e4c54a75d9c8f03c7b0a94a4ef8ccd4 Mon Sep 17 00:00:00 2001 From: ipipman Date: 2021年9月21日 16:42:16 +0800 Subject: [PATCH 1/2] add custom springboot event listener --- ...54345円231円250円345円256円236円346円210円230円.md" | 23 +++++++++++++ ...ringbootSourceCodeAnalysisApplication.java | 4 +++ .../code/analysis/listener/FirstListener.java | 28 +++++++++++++++ .../analysis/listener/FourthListener.java | 34 +++++++++++++++++++ .../analysis/listener/SecondListener.java | 26 ++++++++++++++ .../code/analysis/listener/ThirdListener.java | 27 +++++++++++++++ .../main/resources/META-INF/spring.factories | 4 +++ .../src/main/resources/application.properties | 5 +++ 8 files changed, 151 insertions(+) create mode 100644 "springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" create mode 100644 springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FirstListener.java create mode 100644 springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FourthListener.java create mode 100644 springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/SecondListener.java create mode 100644 springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/ThirdListener.java diff --git "a/springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" "b/springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" new file mode 100644 index 0000000..8f0ec8d --- /dev/null +++ "b/springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" @@ -0,0 +1,23 @@ +### 自定义监听器实战 + + + +![image-20210921163734877](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163734877.png) + + + + + + + + + +![image-20210921163836397](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163836397.png) + + + + + + + +![image-20210921163902692](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163902692.png) \ No newline at end of file diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/SpringbootSourceCodeAnalysisApplication.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/SpringbootSourceCodeAnalysisApplication.java index 2712d71..af01364 100644 --- a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/SpringbootSourceCodeAnalysisApplication.java +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/SpringbootSourceCodeAnalysisApplication.java @@ -1,6 +1,7 @@ package com.example.springboot.source.code.analysis; import com.example.springboot.source.code.analysis.initializer.SecondInitializer; +import com.example.springboot.source.code.analysis.listener.SecondListener; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -14,6 +15,9 @@ public static void main(String[] args) { // 手动添加一个框架的初始化器 springApplication.addInitializers(new SecondInitializer()); + // 手动添加一个框架事件监听器 + springApplication.addListeners(new SecondListener()); + springApplication.run(); } diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FirstListener.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FirstListener.java new file mode 100644 index 0000000..29be824 --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FirstListener.java @@ -0,0 +1,28 @@ +package com.example.springboot.source.code.analysis.listener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.annotation.Order; + +/** + * Created by ipipman on 2021年9月21日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.listener + * @Description: (用一句话描述该文件做什么) + * @date 2021年9月21日 4:12 下午 + */ + +@Order(1) +public class FirstListener implements ApplicationListener { + + private final static Logger logger = LoggerFactory.getLogger(FirstListener.class); + + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + logger.info("设置框架事件监听器【ApplicationListener】成功 : run firstListener"); + + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FourthListener.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FourthListener.java new file mode 100644 index 0000000..6b8eaba --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/FourthListener.java @@ -0,0 +1,34 @@ +package com.example.springboot.source.code.analysis.listener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.context.event.ApplicationPreparedEvent; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.event.SmartApplicationListener; +import org.springframework.core.annotation.Order; + +/** + * Created by ipipman on 2021年9月21日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.listener + * @Description: (用一句话描述该文件做什么) + * @date 2021年9月21日 4:26 下午 + */ +@Order(4) +public class FourthListener implements SmartApplicationListener { + + private final static Logger logger = LoggerFactory.getLogger(FourthListener.class); + + @Override + public boolean supportsEventType(Class eventType) { + return ApplicationStartedEvent.class.isAssignableFrom(eventType) + || ApplicationPreparedEvent.class.isAssignableFrom(eventType); + } + + @Override + public void onApplicationEvent(ApplicationEvent event) { + logger.info("设置框架事件监听器【SmartApplicationListener】成功 : run fourthListener"); + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/SecondListener.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/SecondListener.java new file mode 100644 index 0000000..dd54f03 --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/SecondListener.java @@ -0,0 +1,26 @@ +package com.example.springboot.source.code.analysis.listener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.annotation.Order; + +/** + * Created by ipipman on 2021年9月21日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.listener + * @Description: (用一句话描述该文件做什么) + * @date 2021年9月21日 4:19 下午 + */ +@Order(2) +public class SecondListener implements ApplicationListener { + + private static final Logger logger = LoggerFactory.getLogger(SecondListener.class); + + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + logger.info("设置框架事件监听器【ApplicationListener】成功 : run secondListener"); + } +} diff --git a/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/ThirdListener.java b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/ThirdListener.java new file mode 100644 index 0000000..1c69a4b --- /dev/null +++ b/springboot-source-code-analysis/src/main/java/com/example/springboot/source/code/analysis/listener/ThirdListener.java @@ -0,0 +1,27 @@ +package com.example.springboot.source.code.analysis.listener; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.annotation.Order; + + +/** + * Created by ipipman on 2021年9月21日. + * + * @version V1.0 + * @Package com.example.springboot.source.code.analysis.listener + * @Description: (用一句话描述该文件做什么) + * @date 2021年9月21日 4:21 下午 + */ +@Order(3) +public class ThirdListener implements ApplicationListener { + + private final static Logger logger = LoggerFactory.getLogger(ThirdListener.class); + + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + logger.info("设置框架事件监听器【ApplicationListener】成功 : run thirdListener"); + } +} diff --git a/springboot-source-code-analysis/src/main/resources/META-INF/spring.factories b/springboot-source-code-analysis/src/main/resources/META-INF/spring.factories index fd14307..b386fd2 100644 --- a/springboot-source-code-analysis/src/main/resources/META-INF/spring.factories +++ b/springboot-source-code-analysis/src/main/resources/META-INF/spring.factories @@ -2,3 +2,7 @@ org.springframework.context.ApplicationContextInitializer=\ com.example.springboot.source.code.analysis.initializer.FirstInitializer + +# 通过系统事件监听器,设置自定义事件监听器 +org.springframework.context.ApplicationListener=\ + com.example.springboot.source.code.analysis.listener.FirstListener \ No newline at end of file diff --git a/springboot-source-code-analysis/src/main/resources/application.properties b/springboot-source-code-analysis/src/main/resources/application.properties index 6d3ccbb..82918ee 100644 --- a/springboot-source-code-analysis/src/main/resources/application.properties +++ b/springboot-source-code-analysis/src/main/resources/application.properties @@ -1,3 +1,8 @@ # 通过系统初始化器,设置环境属性 context.initializer.classes=\ com.example.springboot.source.code.analysis.initializer.ThirdInitializer + +# 通过系统事件监听器,监听事件 +context.listener.classes=\ + com.example.springboot.source.code.analysis.listener.ThirdListener,\ + com.example.springboot.source.code.analysis.listener.FourthListener \ No newline at end of file From 2612a44733091fd45d50d31b70229a567faebec7 Mon Sep 17 00:00:00 2001 From: ipipman Date: 2021年9月21日 17:05:10 +0800 Subject: [PATCH 2/2] add custom springboot event listener readme --- ...54345円231円250円345円256円236円346円210円230円.md" | 83 ++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git "a/springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" "b/springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" index 8f0ec8d..2ebee71 100644 --- "a/springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" +++ "b/springboot-source-code-analysis/(8)350円207円252円345円256円232円344円271円211円347円233円221円345円220円254円345円231円250円345円256円236円346円210円230円.md" @@ -1,23 +1,100 @@ ### 自定义监听器实战 +#### 实现方式一 +- 实现ApplicationListener接口,ApplicationListener声明感兴趣的事件 -![image-20210921163734877](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163734877.png) +```java +@Order(1) +public class FirstListener implements ApplicationListener { + private final static Logger logger = LoggerFactory.getLogger(FirstListener.class); + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + logger.info("设置框架事件监听器【ApplicationListener】成功 : run firstListener"); + } +} +``` +- 在spring.factories 配置文件中配置FirstListener事件监听器,key值为org.springframework.context.ApplicationListener +```java +# 通过系统事件监听器,设置自定义事件监听器 +org.springframework.context.ApplicationListener=\ + com.example.springboot.source.code.analysis.listener.FirstListener +``` -![image-20210921163836397](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163836397.png) +#### 实现方式三 +- 实现ApplicationListener接口,声明感兴趣的事件ApplicationListener +```java +@Order(3) +public class ThirdListener implements ApplicationListener { + private final static Logger logger = LoggerFactory.getLogger(ThirdListener.class); + @Override + public void onApplicationEvent(ApplicationStartedEvent event) { + logger.info("设置框架事件监听器【ApplicationListener】成功 : run thirdListener"); + } +} +``` + +- 在application.properties 内配置事件监听器,key值为context.listener.classes + +```java +# 通过系统事件监听器,监听事件 +context.listener.classes=\ + com.example.springboot.source.code.analysis.listener.ThirdListener,\ +``` + +> application.properties 内配置的 conext.listener.classes 事件监听器执行优先级最高 +> +> ```java +> // context.listener.classes 是通过 DelegatingApplicationListener 委派给 ApplicationListener +> public class DelegatingApplicationListener implements ApplicationListener, Ordered { +> private static final String PROPERTY_NAME = "context.listener.classes"; +> // 在委派过程中,DelegatingApplicationListener 会将 order 优先级设置为最高 +> private int order = 0; +> private SimpleApplicationEventMulticaster multicaster; +> +> ``` + + + +#### 实现方式四 + +- 实现SmartApplicationListener接口 + +```java +@Order(4) +public class FourthListener implements SmartApplicationListener { + + private final static Logger logger = LoggerFactory.getLogger(FourthListener.class); + + // 通过重写 supportEventType()方法 判断该监听器对哪些事件感兴趣 + @Override + public boolean supportsEventType(Class eventType) { + return ApplicationStartedEvent.class.isAssignableFrom(eventType) + || ApplicationPreparedEvent.class.isAssignableFrom(eventType); + } + + // 编写事件触发逻辑 + @Override + public void onApplicationEvent(ApplicationEvent event) { + logger.info("设置框架事件监听器【SmartApplicationListener】成功 : run fourthListener"); + } +} + +``` + +- 可以通过spring.factories 或 application.properties 或 new SpringApplication().addListeners() 三种方式注入该事件 -![image-20210921163902692](/Users/huangyan110110114/Library/Application Support/typora-user-images/image-20210921163902692.png) \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /