Spring Boot Auto-Configuration principle Detailed

Source: Internet
Author: User

Recently learning Spring boot, feel spring automatic configuration is amazing, so handwritten this article, this article based on the Spring Boot 2.0.1 Source analysis of the automatic configuration principle, if there are errors, hope criticism points out.

First, @SpringBootApplication main program entry class annotation

1 @Target (Elementtype.type)2 @Retention (retentionpolicy.runtime)3 @Documented4 @Inherited5@SpringBootConfiguration//16@EnableAutoConfiguration//27@ComponentScan (excludefilters = {8@Filter (type = filtertype.custom, classes = Typeexcludefilter.class),9@Filter (type = filtertype.custom, classes = Autoconfigurationexcludefilter.class) })Ten  Public@InterfaceSpringbootapplication {}

1. @SpringBootConfiguration Configuration class annotations

Declares that the class is springboot a configuration class that can be replaced with the spring standard annotation @Configuration.

2. @EnableAutoConfiguration Automatic Configuration class annotations

Enables automatic configuration of the spring application context. By importing Autoconfigurationimportselector.class, all enableautoconfiguration configured within the Classpath meta-inf/spring.factories Values are added to the container.

 1   @Target (elementtype.type)  2   @Retention (retentionpolicy.runtime)  3   @Documented  4   @Inherited  5  @ Autoconfigurationpackage //  1  6  @Import (autoconfigurationimportselector. Class ) //  2  7  public  @ Interface  enableautoconfiguration {} 
    • 1. @AutoConfigurationPackage Automatic Configuration Package

      By importing the AutoConfigurationPackages.Registrar.class class, scan the package of the main configuration class (@SpringBootApplication labeled Class) and all the components in the following sub-packages to the spring container

    • 2, autoconfigurationimportselector automatic configuration Import selector class, the key method Selectimports

      All components that need to be imported are returned in a full-class name, and the returned component class is added to the container;

      1 @Override2  Publicstring[] Selectimports (Annotationmetadata annotationmetadata) {3    if(!isenabled (Annotationmetadata)) {//14       returnNo_imports;5    }6Autoconfigurationmetadata Autoconfigurationmetadata =Autoconfigurationmetadataloader7. Loadmetadata ( This. Beanclassloader);//28Annotationattributes attributes =getattributes (annotationmetadata);9list<string> configurations =getcandidateconfigurations (Annotationmetadata,Tenattributes);//3 OneConfigurations =removeduplicates (configurations); ASet<string> exclusions =getexclusions (annotationmetadata, attributes); - checkexcludedclasses (configurations, exclusions); - Configurations.removeall (exclusions); theConfigurations =Filter (configurations, autoconfigurationmetadata); - fireautoconfigurationimportevents (configurations, exclusions); -    returnStringutils.tostringarray (configurations); -}
        • 1, determine whether to turn on automatic configuration, if not open the configuration, return an empty array

        • 2, load meta-inf/spring-autoconfigure-metadata.properties in the automatic configuration meta-data

        • 3. Get the candidate configuration

          • Springfactoriesloader.loadfactorynames ()

          • Scan all jar package Classpath under Meta-inf/spring.factories

          • Wrap the contents of the scanned files into a Properties object

          • Get the values corresponding to the Enableautoconfiguration.class class (class name) from the properties and add them to the container

Spring Boot Auto-Configuration principle Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.