Spring Boot Combat (second) event monitoring
Preface
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 Implement the Listening step:
1. Monitor class implements Applicationlistener interface
2. Add the Listener class to the Springapplication instance applicationstartedevent
Applicationstartedevent:spring the event that is executed when boot starts
Create the corresponding listener class Myapplicationstartedeventlistener.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 PM 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);//Do not display banner information
logger.info ("==myapplicationstartedeventlistener==");}
}
You can get to the Springapplication object in this event, and you can do some pre-execution settings.
Application.java class
Package com.lkl.springboot;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Com.lkl.springboot.listener.MyApplicationStartedEventListener;
@SpringBootApplication public
class Application {public
static void Main (string[] args) {
Springapplication app = new Springapplication (application.class);
App.addlisteners (New Myapplicationstartedeventlistener ());
App.run (args);
}
}
applicationenvironmentpreparedevent
The applicationenvironmentpreparedevent:spring boot corresponds to enviroment is ready, but the contextual context has not yet been created.
Myapplicationenvironmentpreparedeventlistener.java
Package Com.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> {PR
Ivate Logger Logger = Loggerfactory.getlogger (Myapplicationenvironmentpreparedeventlistener.class);
@Override public void Onapplicationevent (Applicationenvironmentpreparedevent event) { Configurableenvironment envi = Event.getenvironment ();
Mutablepropertysources MPs = envi.getpropertysources ();
if (MPs = null) {iterator<propertysource<?>> iter = Mps.iterator ();
while (Iter.hasnext ()) {propertysource<?> PS = Iter.next (); Logger. Info ("ps.getname:{};p s.getsource:{};p s.getclass:{}", Ps.getname (), Ps.getsource (), Ps.getclass
());
}
}
}
}
After obtaining the configurableenvironment in this listener, the configuration information can be manipulated, for example: Modify the default configuration information, add additional configuration information, etc. ~ ~ ~ applicationpreparedevent
Applicationpreparedevent:spring Boot Context 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 2015 2 Day Afternoon 11:29:38 Liaokailin EXP $ */public class Myapplicationpreparedeventlistener implements Applicationlistener<appli
cationpreparedevent> {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 ()}}
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:spring Boot Exception Execution event
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) {
}
}
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);
}
}
Recycle and dispose of resources in the Doclose () method. Concluding remarks
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.
Reprint Please specify
http://blog.csdn.net/liaokailin/article/details/48186331 Welcome attention, your affirmation is my biggest support