http://blog.csdn.net/liaokailin/article/details/48186331
Objective
Spring boot increases the event monitoring mechanism during the boot process, which provides great convenience for user function development.
Types of events supported four
ApplicationStartedEvent
ApplicationEnvironmentPreparedEvent
ApplicationPreparedEvent
ApplicationFailedEvent
To implement the listening step:
1. Monitoring class implementation ApplicationListener
interface
2. Adding a listener class to an SpringApplication
instance
Applicationstartedevent
ApplicationStartedEvent
: Event executed at the start of Spring boot boot
Create a corresponding listener classMyApplicationStartedEventListener.java
Package Com.lkl.springboot.listener;Import Org.slf4j.Logger;Import Org.slf4j.LoggerFactory;Import org.springframework.boot.SpringApplication;Import org.springframework.boot.context.event.ApplicationStartedEvent;Import Org.springframework.context.ApplicationListener;/** * Spring Boot Boot listener class * *@author Liaokailin * @version $Id: Myapplicationstartedeventlistener.java, v 0.1 September 2, 2015 11:06:04 Liaokailin EXP $ */public class Myapplicationstartedeventlistener implements Applicationlistener<applicationstartedevent> {private Logger Logger = Loggerfactory.getlogger (Myapplicationstartedeventlistener. Class); @Override public void Onapplicationevent (Applicationstartedevent event) { Springapplication app = Event.getspringapplication (); App.setshowbanner (false);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
You can get to the object in this event SpringApplication
, you can do some pre-execution settings.
Application.java
Class
PackageCom. LKL. springboot; Import org. springframework. Boot. Springapplication; Import org.springframework.boot .autoconfigure;import com .lkl.springboot .listener.class" .addlisteners (new Myapplicationstartedeventlistener ()) .run (args) Span class= "hljs-comment" >; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Applicationenvironmentpreparedevent
ApplicationEnvironmentPreparedEvent
: Spring boot corresponds to enviroment is ready, but context contexts have not yet been created.
MyApplicationEnvironmentPreparedEventListener.java
PackageCom. LKL. springboot. Listener; Import Java. util. Iterator; Import org. slf4j. Logger; Import org. slf4j. Loggerfactory; Import org. springframework. Boot. Context. event. Applicationenvironmentpreparedevent; Import org. springframework. Context. Applicationlistener; Import org. springframework. Core. env. Configurableenvironment; Import org. springframework. Core. env. Mutablepropertysources; Import org. springframework. Core. env. Propertysource;/** * Spring Boot Configuration Environment Event Listener * @author Liaokailin * @version $Id: Myapplicationenvironmentpreparedeventlistener.java, v 0.1 September 2, 2015 PM 11:21:15 liaokailin EXP $ */public Class Myapplicationenvironmentpreparedeventlistener implements applicationlistener<applicationenvironmentpreparedevent> {private Logger Logger = Loggerfactory. GetLogger (Myapplicationenvironmentpreparedeventlistener. Class); @Override public void Onapplicationevent (Applicationenvironmentpreparedevent event) {Configurableenvironment Envi = Event.getenvironment () .getpropertysources () ; if (MPs = null) {iterator<propertysource<?>> iter = Mps.iterator () .hasnext ()) {propertysource<?> PS = Iter.next () .info (" ps.getname:{};p s.getsource:{};p s.getclass:{} ", Ps.getName (), ps< Span class= "Hljs-preprocessor" >.getsource (), Ps.getclass ())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
The configuration information can be manipulated when the listener is acquired ConfigurableEnvironment
, such as modifying the default configuration information, adding additional configuration information, etc. ~ ~ ~
Applicationpreparedevent
ApplicationPreparedEvent
: Spring Boot context creation is complete, but at this point the bean in spring is not fully loaded.
MyApplicationPreparedEventListener.java
Package Com.lkl.springboot.listener;Import Org.slf4j.Logger;Import Org.slf4j.LoggerFactory;Import org.springframework.boot.context.event.ApplicationPreparedEvent;Import Org.springframework.context.ApplicationContext;Import Org.springframework.context.ApplicationListener;Import Org.springframework.context.ConfigurableApplicationContext;/** * Event listener executed after context creation is complete *@author Liaokailin *@version $Id: Myapplicationpreparedeventlistener.java, v 0.1 September 2, 2015 PM 11:29:38 liaokailin EXP $ */publicclass Myapplicationpreparedeventlistener implements Applicationlistener<applicationpreparedevent> { Private Logger Logger = Loggerfactory.getlogger (Myapplicationpreparedeventlistener. Class); @Override public void Onapplicationevent (Applicationpreparedevent event) { Configurableapplicationcontext CAC = Event.getapplicationcontext (); Passcontextinfo (CAC); } /** * Delivery context * @param CAC */ private void Passcontextinfo (ApplicationContext CAC) {//dosomething ()}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21st
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
After the context has been obtained, you can pass the context out to do some extra work.
In this listener, you cannot get the custom bean and manipulate it.
Applicationfailedevent
ApplicationFailedEvent
: Execute event when spring boot exception is started
MyApplicationFailedEventListener.java
package Com.lkl.springboot.listener;import Org.springframework.boot.context.event.applicationfailedevent;import Org.springframework.context.ApplicationListener; public class myapplicationfailedeventlistener Implements applicationlistener<applicationfailedevent> { @Override public void onapplicationevent ( Applicationfailedevent event) {throwable throwable = event.getexception (); handlethrowable (Throwable);} /* Handling Exception */private void handlethrowable ( Throwable throwable) {}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
In the event of an exception, it is better to add a virtual machine corresponding to the hook for the recovery and release of resources, can be friendly to handle the exception information.
This has been considered for everyone in spring boot, and the corresponding feature is turned on by default:
public void registerShutdownHook() { if (this.shutdownHook == null) { // No shutdown hook registered yet. this.shutdownHook = new Thread() { @Override public void run() { doClose(); } }; Runtime.getRuntime().addShutdownHook(this.shutdownHook); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
The doClose()
collection and release of resources in the method.
Conclusion
Spring boot provides four kinds of listening events to end here, for real business can add a custom listener, in the next section will be in the spring boot of the listening source analysis, understand why this is the case.
Spring Boot Combat (second) event monitoring