SpringBoot Application事件監(jiān)聽的實(shí)現(xiàn)方案
先說結(jié)論
SpringBoot Application共支持6種事件監(jiān)聽,按順序分別是:
- ApplicationStartingEvent:在Spring最開始啟動(dòng)的時(shí)候觸發(fā)
- ApplicationEnvironmentPreparedEvent:在Spring已經(jīng)準(zhǔn)備好上下文但是上下文尚未創(chuàng)建的時(shí)候觸發(fā)
- ApplicationPreparedEvent:在Bean定義加載之后、刷新上下文之前觸發(fā)
- ApplicationStartedEvent:在刷新上下文之后、調(diào)用application命令之前觸發(fā)
- ApplicationReadyEvent:在調(diào)用applicaiton命令之后觸發(fā)
- ApplicationFailedEvent:在啟動(dòng)Spring發(fā)生異常時(shí)觸發(fā)
另外:
- ApplicationRunner和CommandLineRunner的執(zhí)行在第五步和第六步之間
- Bean的創(chuàng)建在第三步和第四步之間
- 在啟動(dòng)類中,執(zhí)行SpringApplication.run()方法后的代碼,會(huì)在第六步后執(zhí)行
再上代碼:
ApplicationStartingEvent
public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent applicationStartingEvent) {
System.out.println("============>>>>> applicationStartingEvent is trigged");
System.out.println(applicationStartingEvent.getTimestamp());
System.out.println("============>>>>> End");
}
}
ApplicationEnvironmentPreparedEvent
public class ApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) {
System.out.println("============>>>>> ApplicationEnvironmentPreparedEvent is trigged");
System.out.println(applicationEnvironmentPreparedEvent.getTimestamp());
System.out.println("============>>>>> End");
}
}
ApplicationPreparedEvent
public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent) {
System.out.println("============>>>>> applicationPreparedEvent is trigged");
System.out.println(applicationPreparedEvent.getTimestamp());
System.out.println("============>>>>> End");
}
}
ApplicationStartedEvent
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
System.out.println("============>>>>> applicationStartedEvent is trigged");
System.out.println(applicationStartedEvent.getTimestamp());
System.out.println("============>>>>> End");
}
}
ApplicationReadyEvent
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
System.out.println("============>>>>> applicationReadyEvent is trigged");
System.out.println(applicationReadyEvent.getTimestamp());
System.out.println("============>>>>> End");
}
}
ApplicationFailedEvent
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {
@Override
public void onApplicationEvent(ApplicationFailedEvent applicationFailedEvent) {
System.out.println("============>>>>> ApplicationFailedEvent is trigged");
System.out.println(applicationFailedEvent.getTimestamp());
System.out.println("============>>>>> End");
}
}
主啟動(dòng)類
@SpringBootApplication
public class SpringBootTestApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SpringBootTestApplication.class);
springApplication.addListeners(new ApplicationEnvironmentPreparedEventListener());
springApplication.addListeners(new ApplicationFailedEventListener());
springApplication.addListeners(new ApplicationPreparedEventListener());
springApplication.addListeners(new ApplicationReadyEventListener());
springApplication.addListeners(new ApplicationStartedEventListener());
springApplication.addListeners(new ApplicationStartingEventListener());
springApplication.run(args);
}
}
運(yùn)行結(jié)果
Connected to the target VM, address: '127.0.0.1:62927', transport: 'socket'
============>>>>> applicationStartingEvent is trigged
============>>>>> End
============>>>>> ApplicationEnvironmentPreparedEvent is trigged
============>>>>> End
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)
2018-11-01 14:52:35.117 INFO 2044 --- [ main] c.l.s.b.test.SpringBootTestApplication : Starting SpringBootTestApplication on EDIANZU-ETGVGB5 with PID 2044 (D:\Code\SelfCode\SpringCloud\Test\SpringBootTest\target\classes started by Administrator in D:\Code\SelfCode\SpringCloud)
2018-11-01 14:52:35.122 INFO 2044 --- [ main] c.l.s.b.test.SpringBootTestApplication : No active profile set, falling back to default profiles: default
============>>>>> applicationPreparedEvent is trigged
============>>>>> End
2018-11-01 14:52:35.212 INFO 2044 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3406472c: startup date [Thu Nov 01 14:52:35 CST 2018]; root of context hierarchy
2018-11-01 14:52:36.891 INFO 2044 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-11-01 14:52:36.920 INFO 2044 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-11-01 14:52:36.920 INFO 2044 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-11-01 14:52:36.925 INFO 2044 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Program Files\Java\jdk1.8.0_191\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\ThinkPad\Bluetooth Software\;C:\Program Files\ThinkPad\Bluetooth Software\syswow64;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;D:\Program Files\Java\jdk1.8.0_191\bin;D:\Program Files\Git\cmd;D:\Program Files\apache-maven-3.5.4\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;.]
2018-11-01 14:52:37.048 INFO 2044 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-11-01 14:52:37.048 INFO 2044 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1841 ms
2018-11-01 14:52:37.377 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-11-01 14:52:37.389 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-01 14:52:37.390 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-01 14:52:37.390 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-11-01 14:52:37.390 INFO 2044 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-01 14:52:37.598 INFO 2044 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 14:52:37.857 INFO 2044 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3406472c: startup date [Thu Nov 01 14:52:35 CST 2018]; root of context hierarchy
2018-11-01 14:52:37.922 INFO 2044 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-11-01 14:52:37.923 INFO 2044 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-11-01 14:52:37.973 INFO 2044 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 14:52:37.973 INFO 2044 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-11-01 14:52:38.160 INFO 2044 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-11-01 14:52:38.206 INFO 2044 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-01 14:52:38.212 INFO 2044 --- [ main] c.l.s.b.test.SpringBootTestApplication : Started SpringBootTestApplication in 3.976 seconds (JVM running for 5.088)
============>>>>> applicationStartedEvent is trigged
============>>>>> End
============>>>>> applicationReadyEvent is trigged
============>>>>> End
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot 事件監(jiān)聽的實(shí)現(xiàn)方法
- springboot+redis過期事件監(jiān)聽實(shí)現(xiàn)過程解析
- SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實(shí)例
- SpringBoot監(jiān)聽事件和處理事件程序示例詳解
- SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽功能
- SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究
- SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解
- Springboot事件監(jiān)聽與@Async注解詳解
- Java?Springboot異步執(zhí)行事件監(jiān)聽和處理實(shí)例
- SpringBoot實(shí)現(xiàn)事件監(jiān)聽(異步執(zhí)行)的示例代碼
相關(guān)文章
java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)
這篇文章主要介紹了java如何通過FileOutputStream字節(jié)流向文件中寫數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Java ProcessBuilder執(zhí)行多次CMD命令的使用
本文介紹了Java的ProcessBuilder類,該類用于執(zhí)行外部命令,通過ProcessBuilder,我們可以在Java程序中靈活地執(zhí)行多次CMD命令,并控制輸入輸出流以及工作目錄等,感興趣的可以了解一下2024-11-11
java前后端使用ajax數(shù)據(jù)交互問題(簡單demo)
這篇文章主要介紹了java前后端使用ajax數(shù)據(jù)交互問題(簡單demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-06-06
SpringBoot實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳功能詳解
在處理大文件傳輸或網(wǎng)絡(luò)不穩(wěn)定的情況下,文件斷點(diǎn)續(xù)傳功能顯得尤為重要,本文將詳細(xì)介紹如何使用Spring Boot實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳功能,需要的可以了解下2025-04-04
解決Eclipse打開.java文件異常,提示用系統(tǒng)工具打開的問題
這篇文章主要介紹了解決Eclipse打開.java文件異常,提示用系統(tǒng)工具打開的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Spring使用Configuration注解管理bean的方式詳解
在Spring的世界里,Configuration注解就像是一位細(xì)心的園丁,它的主要職責(zé)是在這個(gè)繁花似錦的園子里,幫助我們聲明和管理各種各樣的bean,本文給大家介紹了在Spring中如何優(yōu)雅地管理你的bean,需要的朋友可以參考下2024-05-05
java并發(fā)高的情況下用ThreadLocalRandom來生成隨機(jī)數(shù)
如果我們想要生成一個(gè)隨機(jī)數(shù),通常會(huì)使用Random類。但是在并發(fā)情況下Random生成隨機(jī)數(shù)的性能并不是很理想,本文主要介紹了java并發(fā)高的情況下用ThreadLocalRandom來生成隨機(jī)數(shù),感興趣的可以了解一下2022-05-05
mybatis 解決從列名到屬性名的自動(dòng)映射失敗問題
這篇文章主要介紹了mybatis 解決從列名到屬性名的自動(dòng)映射失敗問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

