Http://docs.spring.io/spring-boot/docs/current/api/overview-summary.htmlhttp://docs.spring.io/spring-boot/docs /current/reference/htmlsingle/#auto-configuration-classesPrerequisites1, generally speaking, xxxaware interface, all provide a setxxx method, so that its implementation class will xxx into its own xxx field, so as to operate. For example, Applicationcontextaware provides a
void setapplicationcontext
(
APPL Icationcontext
applicationcontext
)
throws
beansexception
; , allowing its implementation to operate directly applicationContext . 2. Beanfactory in spring, which is the root interface of the access Bean container. Objects that implement classes typically have a series of bean definitions and are distinguished by different string names. However, the official documentation does not recommend that you get the bean directly in this way, but rather it is recommended that you get the bean in an injected way (
@Autowire)。 --This, can not use initialization injection in the prophase?
Body
The automatic configuration provided by Spring Boot is located under Package org.springframework.boot.autoconfigure .
Note that ① is provided here by Spring Boot, not by a third party (such as Mybatis-spring-boot-starter). ② does not contain the Spring Boot Actuator section.
Spring Boot's automatic configuration class can be viewed through the AutoConfig report and requires--debugOr-debug。 Or, view the AutoConfig endpoint of the actuator project.Start learning from the web first
Because spring-boot Web starter integrates spring MVC, not others, the web is actually provided with automatic configuration of spring MVC. Spring MVC is automatically configured under Package Org.springframework.boot.autoconfigure.web, and the contents of the package are as follows:
From there, you can see clearly what the Spring boot provides for spring MVC's automatic configuration:
- dispatcherservletautoconfiguration . Class
- embeddedservletcontainerautoconfiguration . Class
- Errormvcautoconfiguration . Class
- Gsonhttpmessageconvertersconfiguration . Class
- Httpencodingautoconfiguration . Class
- Httpmessageconvertersautoconfiguration . Class
- Jacksonhttpmessageconvertersconfiguration . Class
- Multipartautoconfiguration . Class
- Serverpropertiesautoconfiguration . Class
- Webclientautoconfiguration . Class
- webmvcautoconfiguration . Class
Next, we have targeted to be smoothed over.
First,Check it out first.embeddedservletcontainerautoconfiguration, which is the automatic configuration of the built-in servlet container-The default is the Pivotal-tc-server (custom tomcat, commercial). The declaration of this class is as follows:
1@AutoConfigureOrder (ordered.highest_precedence)//the highest priority within the auto-configuration2 @Configuration3@ConditionalOnWebApplication//Web Apps only4@Import (Beanpostprocessorsregistrar.class)//Import settings for built-in containers5 Public classembeddedservletcontainerautoconfiguration {6 @Configuration7@ConditionalOnClass ({Servlet.classTomcat.class })8@ConditionalOnMissingBean (value = embeddedservletcontainerfactory.class, search =searchstrategy.current)9 Public Static classEmbeddedtomcat {Ten // ... One } A - @Configuration -@ConditionalOnClass ({Servlet.class, Server.class, Loader.class,Webappcontext.class }) the@ConditionalOnMissingBean (value = embeddedservletcontainerfactory.class, search =searchstrategy.current) - Public Static classEmbeddedjetty { - // ... - } + - @Configuration +@ConditionalOnClass ({Servlet.class, Undertow.class, Sslclientauthmode.class }) A@ConditionalOnMissingBean (value = embeddedservletcontainerfactory.class, search =searchstrategy.current) at Public Static classEmbeddedundertow { - // ... - } - - Public Static classBeanpostprocessorsregistrarImplementsImportbeandefinitionregistrar, Beanfactoryaware { - // ... in } -}
@ConditionalOnWebApplicationis based on application context whether the Web application context is judged.
@AutoConfigureOrder
(
ordered
Span style= "color: #008080;" >.
highest_precedence
) , where
Ordered
.
highest_precedence is actually an int type , which represents the highest priority.
@Import
(
beanpostprocessorsregistrar
.
class
) , the operation in Beanpostprocessorsregistrar here is to register a bean, Embeddedservletcontainercustomizerbeanpostprocessor . is the early registration through Importbeandefinitionregistrar. In this configuration class, there are 4 internal static internal classes: 3 Container classes (Tomcat, jetty, Undertow), and 1 bean registration classes.
3个容器类是基于classpath中的class文件来判断使用哪个容器,默认使用内置容器。
1个bean注册类则是注册了一个 BeanPostProcessor bean,用于对容器进行定制。
The following highlights the correlation of this bean registration class.1.embeddedservletcontainercustomizer
beanpostprocessor
Public class Implements Beanpostprocessor, Applicationcontextaware { // ...}
2. Beanpostprocessor interface, the interface content is as follows:
1 Public Interface beanpostprocessor {2 throws beansexception; 3 throws beansexception; 4 }
View Code
Depending on the name of the method, you know that this is the way to operate before and after the bean is initialized.
The Javadoc of this interface are as follows:
BeanPos Tprocessor is a factory hook (factory hook) for customizing new beans instances, such as checking their markup interfaces or wrapping them with proxies.
applicationcontexts can automatically detect beanpostproces Sor beans and apply them to any beans that are subsequently created.
Simple bean Factory allows a coded registration post-processors and applies the registered post-processors to all beans created by the factory.
postprocessbeforeinitialization ; When you use a proxy wrapper beans postprocessafterinitialization .
The embeddedservletcontainercustomizer
beanpostprocessor provides
the implementation of the postprocessbeforeinitialization (more operations on the bean, rather than directly returning the Bean), has been customized for the built-in container. (In a word, the custom operation here is done through the ApplicationContext embeddedservletcontainer
customizer beans, So we need to get ApplicationContext (applicationcontext
Aware credit) and get it from ApplicationContext. Embeddedservletcontainer
customizer beans) 3.Go back and look again.beanpostprocessors
Registrar
1 Public Static class Implements Importbeandefinitionregistrar, Beanfactoryaware {2 // ... 3 }
View Code
This is a static inner class that implements two interfaces.
4. Interfaceimportbeandefinition
Registrar
Public Interface Importbeandefinitionregistrar { publicvoid registerbeandefinitions ( Annotationmetadata Importingclassmetadata, beandefinitionregistry Registry);}
This interface is used to register more beans when the system processes @Configuration class. Is the operation of the Bean-defined level, not the @bean method/instance level.
This method should be called by spring when the bean is loaded in spring. Org.springframework.context.annotation.Configurationclassbeandefinitionreader
Not to be continued.
Spring Boot Auto Configuration class