preface

        
SpringBoot Framework is a powerful tool in development , It simplifies spring of xml Configuration of , Followed ” Contract greater than configuration “ Principle of , Use annotations to make default configuration for common configurations , Reduce use xml Configuration mode .SpringBoot Encapsulates a large number of starter, such as spring-boot-starter-web Will integrate springmvc And embedded tomcat.SpringBoot Encapsulates the default configuration at the bottom , Modify configuration in application.yml Global profile . Now in pom.xml References in documents starter You can use this framework , use application.properties perhaps application.yml Global configuration of the project .

        So here comes the question... , In the project SpringBoot How to help us automatically configure ? So let's talk about what's next SpringBoot Automatic configuration principle of .

one ,@SpringBootApplication

        @SpringBootApplication Is the core annotation
, This annotation is the whole SpringBoot Entry notes for the project , yes SpringBoot Notes necessary for the project .SpringBoot Main configuration class loaded when the project is started , Main startup class . This annotation controls SpringBoot The lifeblood of the auto configuration principle .

     
  Look at the picture ,HomeworkApplication As an entry class , There is one in the entry class main method ,main Method SpringApplication.run To start the entire application .
@SpringBootApplication Is a compound annotation , Go in and have a look .

Explain the above notes a little :

@Target: express @SpringBootApplication Where can this annotation be marked , Mark on class or method attribute
Mark on class Element.Type. such as :@Autowired Annotations can be marked on Methods , It can also be marked on the parameter , It can also be written on the attribute .

@Retention: Sets how the annotated class is retained

@Document:java doc Generates annotation information

@Inheried: Indicates whether this class is inherited

Then there are the more important 3 Comments :
1,@SpringBootConfiguration:SpringBoot Configuration class for , Marking this annotation on a class indicates that this is a SpringBoot Configuration class for .
2,@EnableAutoConfiguration:
Enable automatic configuration , Things to be configured in the project ,SpringBoot Will help us automatically configure ;@EnableAutoConfiguration tell SpringBoot Enable automatic configuration . So that the automatic configuration will take effect .
3,@ComponentScan:
Enable package scanning , amount to spring in .xml In configuration <context:component-scan>, Not specified basepackage, If not specified ,spring The underlying layer will automatically scan the package of the current configuration class .
two , a key @EnableAutoConfiguration: Turn on automatic configuration       

        Click in @EnableAutoConfiguration annotation , It is found that this annotation is also a compound annotation . The most important of the direct @Import annotation ,  By importing
AutoConfigurationImportSelector
Medium selectImports() method . In method getCandidateConfigurations Medium pass
SpringFactoriesLoader.loadFactoryNames() Scan all with META-INF/spring.factories of jar package .
this jar The package contains all classes that need to be automatically configured .

selectImports() method

         adopt getCandidateConfigurations This method stores the configuration information in the form of a collection of return values .SpringFactoriesLoader.loadFactoryNames() scanning jar Bao Zai META-ING/spring.factory in .
 

stay META-INF There are spring.factory of jar package

  stay spring.factory There are many xxxAutoConfuguration Automatically configured for key, Stored in the form of key=value Add as to spring In container . The whole process is to "META-INF/spring.factories" All configured inside EnableAutoConfiguration Add the value of to the container .

  Summary :

        on the whole ,@EnableAutoConfiguration Annotation passed @SpringBootApplication Indirectly marked in Spring
Boot On the startup class of . stay SpringApplication.run(...) Will be executed internally selectImports() method , Find all JavaConfig The fully qualified name of the auto configuration class corresponds to the class, Then load all autoconfiguration classes into Spring In container .

        Elaborate ,@Import(EnableAutoConfigurationImportSelector.class), With the help of
EnableAutoConfigurationImportSelector,@EnableAutoConfiguration
Can help SpringBoot Apply all eligible @Configuration All configurations are loaded into the current SpringBoot Created and used IOC container : adopt
@Import(AutoConfigurationImportSelector.class) Imported configuration features ,
AutoConfigurationImportSelector Methods in getCandidateConfigurations
, Get the to be configured class Class name collection for , This collection is all the classes that need to be automatically configured , adopt META-INF/spring.factories
Of xxxAutoconfiguration Know which classes need to be automatically configured .

Validation of relevant automatic configuration :

         The effectiveness of automatic configuration classes is conditional , The condition is embodied in some form of annotation . The following are common annotation conditions :
@ConditionalOnBean: When there is a specified bean Under the condition of .
@ConditionalOnMissingBean: When the specified bean Under the condition of . @ConditionalOnClass: When there is a specified class under the classpath .
@ConditionalOnMissingClass: When the specified class does not exist in the classpath .
@ConditionalOnProperty: Whether the specified property has the specified value , such as @ConditionalOnProperties(prefix=”xxx.xxx”,
value=”enable”,
matchIfMissing=true), Representative when xxx.xxx by enable The Boolean value of the condition is true, If it is not set, it is also true.
     
    There are corresponding properties in the auto configuration class , adopt @xxxProperities To encapsulate the relevant attributes in the configuration file . Autoconfiguration class xxxAutoConfiguration The purpose is to add components, etc. to the container .

We can answer this in the interview :

SpringBoot There is a note @SpringBootApplication, Is a compound or derived annotation . When SpringBoot call run Method ,SpringBoot Will pass @EnableAutoConfiguration Annotation finding method
getCandidateConfigurations
Find all collections that require automatic configuration classes , with List Collection return , Auto configuration classes are stored in META-INF/spring.factories In the configuration file , File with AutoConfiguration Named ending direction spring Add components to container , By Properties Get the properties configured in the global configuration file from the end named class .XxxxProperties and @ConfigurationProperties The annotation is bound to the corresponding attribute in the global configuration file , So we have a global configuration file in the project application.yml Can be modified in server.port
:8081 wait .

This is it. SpringBoot Automatic configuration principle of .

Technology
©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 ?