Spring has an internal beanfactorypostprocessor.
ID:org.springframework.context.annotation.internalConfigurationAnnotationProcessor;
Type: beandefinitionregistrypostprocessor;
Implement class Configurationclasspostprocessor;
Responsible for parsing all @configuration label classes and registering the bean definition with the beanfactory.
How did the Configurationclasspostprocessor deal with the class that hit the @configuration tag?
The first parsing process is performed in Applicationcontext-refresh-invokebeanfactorypostprocessor.
The main class and the internally defined class are already loaded in the beanfactory. Internally defined classes are internal, not the configuration class, which the parser ignores, and finally only sampleapplication will parse the configuration. The bean definition in beanfactory is shown in the attached 1.
the parsing process of configurationclasspostprocessor:
Configurationclasspostprocessor The actual parsing operation through Configurationclassparser.
Configurationclassparser will perform the following parsing:
Handling nested Memberclass
Handles @propertysource tags, which are used to parse the properties file and set to environment.
Process @componentscan tags, scan all classes under the package and iterate.
Process the @import label.
Process the @importresource label.
Process the @bean label.
Handles the @bean label on all inherited interface.
Handle superclass.
Handles the Deferredimport in the label.
Deferredimport and Spring-boot-autoconfig are closely related.
There are @enableautoconfiguration in the @springbootapplication, and there are @import in the @EnableAutoConfiguration ( Autoconfigurationimportselector.class), Autoconfigurationimportselector is Deferredimportselector, will be treated as a deferredimport.
What did the Deferredimportselector do?
1. Load the spring-autoconfigure-metadata.properties configuration file under the Spring-boot-autoconfig package to get all the information that supports automatic configuration.
2. Get all the component information that supports enableautoconfiguration, this information is configured under the Spring-boot-autoconfig package under Spring.factories, can see support 127 third-party component configuration.
3, the existing components of the sequencing, de-weight and other processing.
4, then use Autoconfigurationimportfilter to filter, the way to filter is basically to determine whether the existing system has introduced a component, (whether the system uses which component is defined at the time of the POM definition), if any, then the relevant configuration. such as Servletwebserverfactoryautoconfiguration, will be in servletrequest.class and other conditions exist in the case of configuration, and Embeddedtomcat will Servlet.class, Create the Tomcatservletwebserverfactory if Tomcat.class exists.
The last valid deferredimport see attached 2, a total of 22 configuration classes.
And in the Deferredimport after the beanfactory in the beans, there are 112, inside of our custom sampleapplication, Samplecontroller, As well as the dispatcherservlet of the component customization. As a whole, the beans needed to start the Web project are basically all.
Attached 1:
Name:org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
Class:rg.springframework.context.annotation.ConfigurationClassPostProcessor
Name:org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
Class:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
Name:org.springframework.context.annotation.internalRequiredAnnotationProcessor,
Class:org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
Name:org.springframework.context.annotation.internalCommonAnnotationProcessor,
Class:org.springframework.context.annotation.CommonAnnotationBeanPostProcessor
Name:org.springframework.context.event.internalEventListenerProcessor,
Class:org.springframework.context.event.EventListenerMethodProcessor
Org.springframework.context.event.internalEventListenerFactory,
Class:org.springframework.context.event.DefaultEventListenerFactory
Name:sampleapplication,
Class:com.travelsky.ibeplus.sample.helloworld.SampleApplication
Name:org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
class:org.springframework.boot.autoconfigure.sharedmetadatareaderfactorycontextinitializer$ Sharedmetadatareaderfactorybean
Attached 2:
Attached 3:
3.1.1_spring How to load and parse @configuration tags