forked from ipipman/JavaSpringBootSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from ipipman:master #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
springboot-source-code-analysis/(8)自定义监听器实战.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| ### 自定义监听器实战 | ||
|
|
||
| #### 实现方式一 | ||
|
|
||
| - 实现ApplicationListener接口,ApplicationListener<ApplicationStartedEvent>声明感兴趣的事件 | ||
|
|
||
| ```java | ||
| @Order(1) | ||
| public class FirstListener implements ApplicationListener<ApplicationStartedEvent> { | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| #### 实现方式三 | ||
|
|
||
| - 实现ApplicationListener接口,声明感兴趣的事件ApplicationListener<ApplicationStartedEvent> | ||
|
|
||
| ```java | ||
| @Order(3) | ||
| public class ThirdListener implements ApplicationListener<ApplicationStartedEvent> { | ||
|
|
||
| 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<ApplicationEvent>, 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<? extends ApplicationEvent> 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() 三种方式注入该事件 | ||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
...sis/src/main/java/com/example/springboot/source/code/analysis/listener/FirstListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<ApplicationStartedEvent> { | ||
|
|
||
| private final static Logger logger = LoggerFactory.getLogger(FirstListener.class); | ||
|
|
||
| @Override | ||
| public void onApplicationEvent(ApplicationStartedEvent event) { | ||
| logger.info("设置框架事件监听器【ApplicationListener】成功 : run firstListener"); | ||
|
|
||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
...is/src/main/java/com/example/springboot/source/code/analysis/listener/FourthListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<? extends ApplicationEvent> eventType) { | ||
| return ApplicationStartedEvent.class.isAssignableFrom(eventType) | ||
| || ApplicationPreparedEvent.class.isAssignableFrom(eventType); | ||
| } | ||
|
|
||
| @Override | ||
| public void onApplicationEvent(ApplicationEvent event) { | ||
| logger.info("设置框架事件监听器【SmartApplicationListener】成功 : run fourthListener"); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
...is/src/main/java/com/example/springboot/source/code/analysis/listener/SecondListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<ApplicationStartedEvent> { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(SecondListener.class); | ||
|
|
||
| @Override | ||
| public void onApplicationEvent(ApplicationStartedEvent event) { | ||
| logger.info("设置框架事件监听器【ApplicationListener】成功 : run secondListener"); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
...sis/src/main/java/com/example/springboot/source/code/analysis/listener/ThirdListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<ApplicationStartedEvent> { | ||
|
|
||
| private final static Logger logger = LoggerFactory.getLogger(ThirdListener.class); | ||
|
|
||
| @Override | ||
| public void onApplicationEvent(ApplicationStartedEvent event) { | ||
| logger.info("设置框架事件监听器【ApplicationListener】成功 : run thirdListener"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
springboot-source-code-analysis/src/main/resources/application.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.