Several important callback mechanisms for events
Configuration in Meta-inf/spring.factories
Applicationcontextinitializer
Springapplicationrunlistener
Just put it in the IOC container.
Applicationrunner
Commandlinerunner
Start the process:
1. Create a Springapplication object
Initialize (sources);Private voidInitialize (object[] sources) {//Saving the master configuration class if(Sources! =NULL&& sources.length > 0) { This. Sources.addall (arrays.aslist (sources)); } //determine whether a web app is currently This. webenvironment =deducewebenvironment (); //Find all the Applicationcontextinitializer of the meta-inf/spring.factories configuration from the Classpath and save it.setinitializers (Collection) getspringfactoriesinstances (Applicationcontextinitializer.class)); //Find all the Applicationlistener of the eta-inf/spring.factories configuration from the ClasspathSetlisteners (Collection) getspringfactoriesinstances (Applicationlistener.class)); //Find the main configuration class with the main method from multiple configuration classes This. Mainapplicationclass =Deducemainapplicationclass ();}
2. Running the Run method
PublicConfigurableapplicationcontext Run (String ... args) {StopWatch StopWatch=NewStopWatch (); Stopwatch.start (); Configurableapplicationcontext Context=NULL; Failureanalyzers Analyzers=NULL; Configureheadlessproperty (); //get springapplicationrunlisteners; meta-inf/spring.factories from the ClasspathSpringapplicationrunlisteners listeners =getrunlisteners (args); //callback all the Get Springapplicationrunlistener.starting () methodlisteners.starting (); Try { //encapsulating command-line argumentsApplicationarguments applicationarguments =Newdefaultapplicationarguments (args); //Prepare the EnvironmentConfigurableenvironment environment =prepareenvironment (listeners, applicationarguments); //callback Springapplicationrunlistener.environmentprepared () after the creation of the environment completes, indicating that the environment is ready to completeBanner Printedbanner=Printbanner (Environment); //create ApplicationContext; Decide whether to create an IOC or a normal IOC for the webContext =CreateApplicationContext (); Analyzers=Newfailureanalyzers (context); //Prepare the context, save the environment to the IOC, and Applyinitializers ();//applyinitializers (): Callback before saving all Applicationcontextinitializer Initialize methods//callback all the Springapplicationrunlistener of the contextprepared ();//preparecontext (context, environment, listeners, applicationarguments, Printedbanner); //Preparecontext After the completion of the operation, callback all the Springapplicationrunlistener contextloaded ();//s refreshes the container; IOC container initialization (if the web app also creates embedded tomcat); Spring Annotation edition//Scan, create, load all components in place; (Configuration classes, components, automatic configuration)Refreshcontext (context); //Retrieve all Applicationrunner and Commandlinerunner from the IOC container for callbacks//Applicationrunner First callback, Commandlinerunner callback againAfterRefresh (context, applicationarguments); //all Springapplicationrunlistener callback finished methodsListeners.finished (Context,NULL); Stopwatch.stop (); if( This. Logstartupinfo) { NewStartupinfologger ( This. Mainapplicationclass). logstarted (Getapplicationlog (), stopWatch); } //the entire Springboot application launches after the completion of the launch of the IOC container; returncontext; } Catch(Throwable ex) {handlerunfailure (context, listeners, analyzers, ex); Throw NewIllegalStateException (ex); }}
3. Event Monitoring Mechanism
Configuration in Meta-inf/spring.factories
Applicationcontextinitializer
Public class Implements Applicationcontextinitializer<configurableapplicationcontext> { @Override public void Initialize (Configurableapplicationcontext applicationcontext) { System.out.println ( "Applicationcontextinitializer...initialize ..." +applicationcontext);} }
Springapplicationrunlistener
Public classHellospringapplicationrunlistenerImplementsSpringapplicationrunlistener {//Required Constructors PublicHellospringapplicationrunlistener (springapplication application, string[] args) {} @Override Public voidstarting () {System.out.println ("Springapplicationrunlistener...starting ..."); } @Override Public voidenvironmentprepared (configurableenvironment environment) {Object O= Environment.getsystemproperties (). Get ("Os.name"); System.out.println ("Springapplicationrunlistener...environmentprepared." +o); } @Override Public voidcontextprepared (Configurableapplicationcontext context) {System.out.println ("Springapplicationrunlistener...contextprepared ..."); } @Override Public voidcontextloaded (Configurableapplicationcontext context) {System.out.println ("Springapplicationrunlistener...contextloaded ..."); } @Override Public voidfinished (configurableapplicationcontext context, Throwable exception) {System.out.println ("Springapplicationrunlistener...finished ..."); }}
Configuration (meta-inf/spring.factories)
Org.springframework.context.applicationcontextinitializer= Com.atguigu.springboot.listener.HelloApplicationContextInitializerorg.springframework.boot.SpringApplicationRunListener =com.atguigu.springboot.listener.hellospringapplicationrunlistener
Just put it in the IOC container.
Applicationrunner
@Component Public class Implements Applicationrunner { @Override publicvoidthrows Exception { System.out.println ("Applicationrunner...run ....");} }
Commandlinerunner
@Component Public class Implements Commandlinerunner { @Override publicvoidthrows Exception { System.out.println ("Commandlinerunner...run ..." + arrays.aslist (args));} }
Seven, Spring boot configuration principle