SpringBoot Application事件监听的实现方案
先说结论
SpringBootApplication共支持6种事件监听,按顺序分别是:
- ApplicationStartingEvent:在Spring最开始启动的时候触发
- ApplicationEnvironmentPreparedEvent:在Spring已经准备好上下文但是上下文尚未创建的时候触发
- ApplicationPreparedEvent:在Bean定义加载之后、刷新上下文之前触发
- ApplicationStartedEvent:在刷新上下文之后、调用application命令之前触发
- ApplicationReadyEvent:在调用applicaiton命令之后触发
- ApplicationFailedEvent:在启动Spring发生异常时触发
另外:
- ApplicationRunner和CommandLineRunner的执行在第五步和第六步之间
- Bean的创建在第三步和第四步之间
- 在启动类中,执行SpringApplication.run()方法后的代码,会在第六步后执行
再上代码:
ApplicationStartingEvent
publicclassApplicationStartingEventListenerimplementsApplicationListener{ @Override publicvoidonApplicationEvent(ApplicationStartingEventapplicationStartingEvent){ System.out.println("============>>>>>applicationStartingEventistrigged"); System.out.println(applicationStartingEvent.getTimestamp()); System.out.println("============>>>>>End"); } }
ApplicationEnvironmentPreparedEvent
publicclassApplicationEnvironmentPreparedEventListenerimplementsApplicationListener{ @Override publicvoidonApplicationEvent(ApplicationEnvironmentPreparedEventapplicationEnvironmentPreparedEvent){ System.out.println("============>>>>>ApplicationEnvironmentPreparedEventistrigged"); System.out.println(applicationEnvironmentPreparedEvent.getTimestamp()); System.out.println("============>>>>>End"); } }
ApplicationPreparedEvent
publicclassApplicationPreparedEventListenerimplementsApplicationListener{ @Override publicvoidonApplicationEvent(ApplicationPreparedEventapplicationPreparedEvent){ System.out.println("============>>>>>applicationPreparedEventistrigged"); System.out.println(applicationPreparedEvent.getTimestamp()); System.out.println("============>>>>>End"); } }
ApplicationStartedEvent
publicclassApplicationStartedEventListenerimplementsApplicationListener{ @Override publicvoidonApplicationEvent(ApplicationStartedEventapplicationStartedEvent){ System.out.println("============>>>>>applicationStartedEventistrigged"); System.out.println(applicationStartedEvent.getTimestamp()); System.out.println("============>>>>>End"); } }
ApplicationReadyEvent
publicclassApplicationReadyEventListenerimplementsApplicationListener{ @Override publicvoidonApplicationEvent(ApplicationReadyEventapplicationReadyEvent){ System.out.println("============>>>>>applicationReadyEventistrigged"); System.out.println(applicationReadyEvent.getTimestamp()); System.out.println("============>>>>>End"); } }
ApplicationFailedEvent
publicclassApplicationFailedEventListenerimplementsApplicationListener{ @Override publicvoidonApplicationEvent(ApplicationFailedEventapplicationFailedEvent){ System.out.println("============>>>>>ApplicationFailedEventistrigged"); System.out.println(applicationFailedEvent.getTimestamp()); System.out.println("============>>>>>End"); } }
主启动类
@SpringBootApplication publicclassSpringBootTestApplication{ publicstaticvoidmain(String[]args){ SpringApplicationspringApplication=newSpringApplication(SpringBootTestApplication.class); springApplication.addListeners(newApplicationEnvironmentPreparedEventListener()); springApplication.addListeners(newApplicationFailedEventListener()); springApplication.addListeners(newApplicationPreparedEventListener()); springApplication.addListeners(newApplicationReadyEventListener()); springApplication.addListeners(newApplicationStartedEventListener()); springApplication.addListeners(newApplicationStartingEventListener()); springApplication.run(args); } }
运行结果
ConnectedtothetargetVM,address:'127.0.0.1:62927',transport:'socket' ============>>>>>applicationStartingEventistrigged ============>>>>>End ============>>>>>ApplicationEnvironmentPreparedEventistrigged ============>>>>>End ._________ /\\/___'_____(_)______\\\\ (()\___|'_|'_||'_\/_`|\\\\ \\/___)||_)|||||||(_||)))) '|____|.__|_||_|_||_\__,|//// =========|_|==============|___/=/_/_/_/ ::SpringBoot::(v2.0.3.RELEASE) 2018-11-0114:52:35.117INFO2044---[main]c.l.s.b.test.SpringBootTestApplication:StartingSpringBootTestApplicationonEDIANZU-ETGVGB5withPID2044(D:\Code\SelfCode\SpringCloud\Test\SpringBootTest\target\classesstartedbyAdministratorinD:\Code\SelfCode\SpringCloud) 2018-11-0114:52:35.122INFO2044---[main]c.l.s.b.test.SpringBootTestApplication:Noactiveprofileset,fallingbacktodefaultprofiles:default ============>>>>>applicationPreparedEventistrigged ============>>>>>End 2018-11-0114:52:35.212INFO2044---[main]ConfigServletWebServerApplicationContext:Refreshingorg.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3406472c:startupdate[ThuNov0114:52:35CST2018];rootofcontexthierarchy 2018-11-0114:52:36.891INFO2044---[main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcatinitializedwithport(s):8080(http) 2018-11-0114:52:36.920INFO2044---[main]o.apache.catalina.core.StandardService:Startingservice[Tomcat] 2018-11-0114:52:36.920INFO2044---[main]org.apache.catalina.core.StandardEngine:StartingServletEngine:ApacheTomcat/8.5.31 2018-11-0114:52:36.925INFO2044---[ost-startStop-1]o.a.catalina.core.AprLifecycleListener:TheAPRbasedApacheTomcatNativelibrarywhichallowsoptimalperformanceinproductionenvironmentswasnotfoundonthejava.library.path:[D:\ProgramFiles\Java\jdk1.8.0_191\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramFiles(x86)\CommonFiles\Oracle\Java\javapath;C:\ProgramFiles(x86)\Intel\iCLSClient\;C:\ProgramFiles\Intel\iCLSClient\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramFiles(x86)\Intel\Intel(R)ManagementEngineComponents\DAL;C:\ProgramFiles\Intel\Intel(R)ManagementEngineComponents\DAL;C:\ProgramFiles(x86)\Intel\Intel(R)ManagementEngineComponents\IPT;C:\ProgramFiles\Intel\Intel(R)ManagementEngineComponents\IPT;C:\ProgramFiles\Intel\WiFi\bin\;C:\ProgramFiles\CommonFiles\Intel\WirelessCommon\;C:\ProgramFiles\ThinkPad\BluetoothSoftware\;C:\ProgramFiles\ThinkPad\BluetoothSoftware\syswow64;C:\ProgramFiles(x86)\Intel\OpenCLSDK\2.0\bin\x86;C:\ProgramFiles(x86)\Intel\OpenCLSDK\2.0\bin\x64;D:\ProgramFiles\Java\jdk1.8.0_191\bin;D:\ProgramFiles\Git\cmd;D:\ProgramFiles\apache-maven-3.5.4\bin;C:\ProgramFiles\Intel\WiFi\bin\;C:\ProgramFiles\CommonFiles\Intel\WirelessCommon\;.] 2018-11-0114:52:37.048INFO2044---[ost-startStop-1]o.a.c.c.C.[Tomcat].[localhost].[/]:InitializingSpringembeddedWebApplicationContext 2018-11-0114:52:37.048INFO2044---[ost-startStop-1]o.s.web.context.ContextLoader:RootWebApplicationContext:initializationcompletedin1841ms 2018-11-0114:52:37.377INFO2044---[ost-startStop-1]o.s.b.w.servlet.ServletRegistrationBean:ServletdispatcherServletmappedto[/] 2018-11-0114:52:37.389INFO2044---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'characterEncodingFilter'to:[/*] 2018-11-0114:52:37.390INFO2044---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'hiddenHttpMethodFilter'to:[/*] 2018-11-0114:52:37.390INFO2044---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'httpPutFormContentFilter'to:[/*] 2018-11-0114:52:37.390INFO2044---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:Mappingfilter:'requestContextFilter'to:[/*] 2018-11-0114:52:37.598INFO2044---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/**/favicon.ico]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-11-0114:52:37.857INFO2044---[main]s.w.s.m.m.a.RequestMappingHandlerAdapter:Lookingfor@ControllerAdvice:org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3406472c:startupdate[ThuNov0114:52:35CST2018];rootofcontexthierarchy 2018-11-0114:52:37.922INFO2044---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/error]}"ontopublicorg.springframework.http.ResponseEntity>org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-11-0114:52:37.923INFO2044---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:Mapped"{[/error],produces=[text/html]}"ontopublicorg.springframework.web.servlet.ModelAndVieworg.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-11-0114:52:37.973INFO2044---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/webjars/**]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-11-0114:52:37.973INFO2044---[main]o.s.w.s.handler.SimpleUrlHandlerMapping:MappedURLpath[/**]ontohandleroftype[classorg.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-11-0114:52:38.160INFO2044---[main]o.s.j.e.a.AnnotationMBeanExporter:RegisteringbeansforJMXexposureonstartup 2018-11-0114:52:38.206INFO2044---[main]o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcatstartedonport(s):8080(http)withcontextpath'' 2018-11-0114:52:38.212INFO2044---[main]c.l.s.b.test.SpringBootTestApplication:StartedSpringBootTestApplicationin3.976seconds(JVMrunningfor5.088) ============>>>>>applicationStartedEventistrigged ============>>>>>End ============>>>>>applicationReadyEventistrigged ============>>>>>End
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。