we all know that automatic configuration is the most core SpringBoot framework is also the essence point when you begin to create the entire SpringBoot project main start class and start it when it automatically will be all you need to be configured to automatically configure, and import the corresponding components into the IOC container, eliminating the manual before we go to the XML file in a variety of complex configuration, greatly improve the efficiency of the environment to build our project, and if you are not satisfied with the framework to your configuration, you can through the global configuration file yml or propertise to modify its default configuration,
then springboot is how to realize the automatic configuration? So we need to start from its main class @ SpringBootApplication ,
Few words said directly above,
go in to look at the comments found a @ EnableAutoConfiguration notes it is unlock the function of automatic configuration of annotation
we have found one to go in the annotation @ Import the bottom note it into a called EnableAutoConfigurationImportSelector component of this component is to determine which configuration classes and components you need to import our project
we go in with this class
AutoConfigurationImportSelector
Found it calls a called selectImports method to choose which components and we need to import automatic configuration class. So the question becomes, where it is to choose our configuration class? We can see it inside the method body calling a getCandidateConfigurations method returns a collection of configurations and entire selectImports method finally returned to it so I guess the selection of source of automatic configuration file where the answers should be in the getCandidateConfigurations method (from English translation "capture candidate configuration" we can also easily daydream to)
few words said to check found that it is by the SpringFactoriesLoader. LoadFactoryNames () method to get the target object
we can see that it was introduced into a class class parameters found with in return is the EnableAutoConfiguration annotation class object
we return to the previous step to continue SpringFactoriesLoader. LoadFactoryNames () with in to find the answer to the question is to go to the class path before the meta-inf/spring. Factories file selected scans all the jar package classpath meta-inf/spring. Factories file
so we find it to scan the jar package classpath the meta-inf/spring. Factories file
Found that there are indeed EnableAutoConfiguration attributes of each such xxxAutoConfiguration class is a component in the container, through the condition judgment into the container; Use them to do automatic configuration;
every automatic configuration class for automatic configuration function;
We are looking for one of the examples, we find the HttpEncodingAutoConfiguration
found it has a @ EnableConfigurationProperties annotation inside a HttpEncodingProperties
The function of the annotation is to start the specified class ConfigurationProperties function; The corresponding values in the configuration file and HttpEncodingProperties binding; And the HttpEncodingProperties join the ioc container
We have found a follow-up HttpEncodingProperties objects we are familiar with annotations @ ConfigurationProperties
It is not the configuration file and the corresponding value in the HttpEncodingProperties bind? We in the yml or propertise through its configuration file for a specified prefix prefix can have prompt change its default configuration,
then we return to the previous step to HttpEncodingAutoConfiguration annotations on the configuration class sections explain
@ Configuration says this is a Configuration class, used to write Configuration files, also can add components to the container
@ EnableConfigurationProperties (HttpEncodingProperties. Class) start specified class ConfigurationProperties function; The corresponding values in the configuration file and HttpEncodingProperties binding; And the HttpEncodingProperties join the ioc container
@ ConditionalOnWebApplication Spring bottom @ Conditional annotations (Spring annotations), according to the different conditions, if
To meet the specified conditions, the entire configuration configuration will take effect in a class; To determine whether the current application of web applications, if it is, the current configuration class effect.
@ ConditionalOnClass (CharacterEncodingFilter. Class) //judge the current project have this class CharacterEncodingFilter; For the code to solve filter for SpringMVC;
@ ConditionalOnProperty (prefix="spring. HTTP. Encoding", value="https://bbs.csdn.net/topics/enabled", matchIfMissing=
True) to judge whether there is a configuration file configuration spring. HTTP. Encoding. Enabled; If there is no judgment is established
Even if we are in the configuration file is not configured pring. HTTP. Encoding. The enabled=true, is also the default effect;
If these conditions notes have effect, can add components to the container certain values in this component needs to be obtained from the properties
While the HttpEncodingProperties has joined the IOC container and binding, and configuration files
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull