Spring Boot Boot configuration principle

Source: Internet
Author: User
Tags throwable

Configuration in Meta-inf/spring.factories

There are several main classes

Applicationcontextinitializer Creating Springaplication

Springapplicationrunlistener Start Run process

Applicationrunner

Commandlinerunner

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 Helloapplicationcontextinitializer Implements Applicationcontextinitializer<configurableapplicationcontext > {    @Override    publicvoid  Initialize (configurableapplicationcontext ApplicationContext) {        System. out. println ("applicationcontextinitializer...initialize ... "+ApplicationContext);}    }

Springapplicationrunlistener

 Public classHellospringapplicationrunlistener implements Springapplicationrunlistener {//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

Applicationrunner

@Component  Public class Helloapplicationrunner implements Applicationrunner {    @Override    publicvoid  Run (applicationarguments args) throws Exception {        System. out. println ("applicationrunner...run .... ") " );    }}

Commandlinerunner

@Component  Public class Hellocommandlinerunner implements Commandlinerunner {    @Override    publicvoid  Run (String ... args) throws Exception {        System. out. println ("commandlinerunner...run ... "+ arrays.aslist (args));}    }

Spring Boot Boot configuration principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.