SpringBoot Application事件监听的实现方案
程序员文章站
2023-11-05 17:27:04
先说结论
springboot application共支持6种事件监听,按顺序分别是:
applicationstartingevent:在spring最...
先说结论
springboot application共支持6种事件监听,按顺序分别是:
- applicationstartingevent:在spring最开始启动的时候触发
- applicationenvironmentpreparedevent:在spring已经准备好上下文但是上下文尚未创建的时候触发
- applicationpreparedevent:在bean定义加载之后、刷新上下文之前触发
- applicationstartedevent:在刷新上下文之后、调用application命令之前触发
- applicationreadyevent:在调用applicaiton命令之后触发
- applicationfailedevent:在启动spring发生异常时触发
另外:
- applicationrunner和commandlinerunner的执行在第五步和第六步之间
- bean的创建在第三步和第四步之间
- 在启动类中,执行springapplication.run()方法后的代码,会在第六步后执行
再上代码:
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。