先說結論
創新互聯專注為客戶提供全方位的互聯網綜合服務,包含不限于成都網站設計、成都做網站、甘肅網絡推廣、微信平臺小程序開發、甘肅網絡營銷、甘肅企業策劃、甘肅品牌公關、搜索引擎seo、人物專訪、企業宣傳片、企業代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創新互聯為所有大學生創業者提供甘肅建站搭建服務,24小時服務熱線:13518219792,官方網址:vcdvsql.cn
SpringBoot Application共支持6種事件監聽,按順序分別是:
另外:
再上代碼:
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"); } }
主啟動類
@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); } }
運行結果
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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。
本文名稱:SpringBootApplication事件監聽的實現方案
文章來源:http://vcdvsql.cn/article4/poogie.html
成都網站建設公司_創新互聯,為您提供網站排名、App開發、企業網站制作、品牌網站建設、移動網站建設、面包屑導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯