<> one Interview questions

<>1.1 Spring Boot Start process

??? interviewer : say something SpringBoot Start the process

?? I :

* First from main find run() method , In execution run() Before method new One SpringApplication object
* get into run() method , Create application listener SpringApplicationRunListeners Start listening
* Then load SpringBoot Configure environment (ConfigurableEnvironment), Then configure the environment (Environment) Add to the listening object
* Then load the application context (ConfigurableApplicationContext), treat as run Return object of method
* Last create Spring container ,refreshContext(context), realization starter Automated configuration and bean Instantiation of .
<>1.2 SpringBoot automatic assembly

??? interviewer : say something SpringBoot Principle of automatic configuration

?? I : adopt @EnableAutoConfiguration
Annotation in the META-INF/spring.factories Find all corresponding configuration classes in the file , Then load these autoconfiguration classes into spring In container .

<> two Detailed explanation of knowledge points

<>2.1 SpringBoot Core notes :

@SpringBootApplication Annotation is Spring Boot Core notes for , It is actually a composite annotation , Include three comments

: The annotated class is equal to Spring of XML In the configuration file (applicationContext.xml), Assemble all bean affair , Provides a Spring Context of

@EnableAutoConfiguration:SpringBoot Based on the dependencies declared by the application Spring Framework for automatic configuration , Two important notes are included :

: There is a @Import({Registrar.class}) annotation , among Registrar Class is used to scan and inject all sub package components under the package where the startup class is located into spring In container .

So that's why controller,service The reason why the package is placed in the same level directory of the startup class

: among AutoConfigurationImportSelector There is one in the class getCandidateConfigurations() method , This method passes SpringFactoriesLoader.loadFactoryNames() Find at META-INF/spring.factories File and load these classes .

: Component scan , Automatic scanning and assembly Bean, scanning SpringApplication of run In method ExammanagerApplication.class Files under the package path , So the class will be started (main) Put it under the follow-up path . It goes looking for
@Component Annotated class , And create bean.

<>2.2 Detailed startup process ( Combined source code )

First enter run() method ,run In method new Created a SpringApplication example
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) { return (new SpringApplication(primarySources)).run(args); }
stay SpringApplication The construction method of is SpringApplication Assign some initial values to the object .

After the construction method is executed , go back to run() method

The following key steps are implemented in this method :

* Created a listener for the application SpringApplicationRunListeners And start listening
load SpringBoot Configure environment (ConfigurableEnvironment), If yes web Container Publishing , Will load StandardEnvironment, It eventually inherited ConfigurableEnvironment
* Configure environment (Environment) Add to listener object (SpringApplicationRunListeners)
* establish run Return object of method :ConfigurableApplicationContext( Application configuration context )
go back to run Within method ,prepareContext Method will listeners,environment,applicationArguments,banner And other important components are associated with context objects
Next refreshContext(context) method ( The initialization method is as follows ) Will be implemented spring-boot-starter-*(mybatis,redis etc. ) Key to automated configuration , include spring.factories Loading of ,bean Core work such as instantiation of .

After configuration ,Springboot Did some basic finishing work , Application context returned . Review the overall process ,Springboot Start of , Mainly created the configuration environment (environment), event listeners (listeners), Application context (applicationContext), And based on the above conditions , Start instantiating what we need in the container Bean, thus , adopt SpringBoot The started program has been constructed .

©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?