In SpringBoot, one point we have to say is auto-assembly, which is the basis of starter and the core of SpringBoot. So what is auto-assembly? showTime

 

Automatic assembly in SpringBoot is implemented through the @enableAutoConfiguration annotation, which is declared in the composite annotation @SpringBootApplication

 

A few other notes before we start the meal

@springBootConfiguration is a declaration that the current class is a boot class, @target ({elementtype.type}) @Retention(RetentionPolicy.runtime) @Documented @Configuration Public @Interface SpringBootConfiguration { @AliasFor( annotation = Configuration.class ) boolean proxyBeanMethods() default true; }Copy the code

Dinner starts @enableAutoConfiguration, follow the source code click in below

Isn’t it obvious when you look at the @AutoConfigurationPackage annotation that this is why Spring scans all components in the current package and its subpackages that start the class

 

If you read the source code often, you will see the @import and @Conditional annotations. In this section, the @import annotation is encountered, and I will first explain what this annotation does: import classes.

@Import({AutoConfigurationImportSelector.class})

All comments @ import it into the configuration class is a class called AutoConfigurationImportSelector this a configuration, for now, of course, no matter what this class is, it must be clear is that it will realize the configuration class import, What is the difference between the import method and @Configuration

AutoConfigurationImportSelector this class finally achieved ImportSelector, ImportSelector is an interface, there is an abstract method that returns a String array, In this array you can specify the classes that need to be assembled into the IOC container. When its implementation Class is imported in @import, the Class names returned from that Class are loaded into the IOC container

Based on the previous analysis, we can guess that the core of automatic assembly is to parse the files in the directory of scanning conventions. After parsing, the obtained Configuration class is imported through ImportSelector to complete automatic assembly of beans. Then we carefully analysis the selectImport AutoConfigurationImportSelector method below

AutoConfigurationImportSelector has two main functions:

1. Load conditional metadata from meta-INF /spring-autoconfigure-metadata.properties

2. Collect all eligible configuration class antoConfigurationEntry. GetConfiguration (), to complete the assembly

 

The core loading process is about to begin: look at my tags, here’s how to do it

public String[] selectImports(AnnotationMetadata annotationMetadata) { if (! this.isEnabled(annotationMetadata)) { return NO_IMPORTS; } else { AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader); The most core process AutoConfigurationImportSelector. AutoConfigurationEntry AutoConfigurationEntry = this.getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata); return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations()); }}Copy the code

 

By the time you get to this point, you know the better part of it, and there’s only one Boss left for you to KO

———————————————————————————————————————— —————————————-

 

protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) { if (! this.isEnabled(annotationMetadata)) { return EMPTY_ENTRY; } else {// Get attributes in @enableAutoConfiguration exclude excludeName etc. AnnotationAttributes attributes = this.getAttributes(annotationMetadata); A List < String > configurations = / / get all automatic assembly configuration class enclosing getCandidateConfigurations (annotationMetadata, attributes); // Delete the duplicate configuration item configurations = this.removeDuplicates(configurations); // Exclude as configured in the @enableAutoConfiguration annotation Set<String> exclusions = this.getexclusions (annotationMetadata, attributes); this.checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = this.filter(configurations, autoConfigurationMetadata); / / this broadcast events. FireAutoConfigurationImportEvents (configurations, exclusions); return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions); }}Copy the code

 

Conclusion: It obtains all configuration classes and obtains the configuration classes that need to realize automatic assembly through operations such as de-duplication and exclude. And need to focus on here is getCandidateConfigurations he is the most the most core and core configuration class at the core

I said oh, the way four times

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }
Copy the code

This uses the SpringFactoriesLoader, which is an internal loading method provided by Spring, just like SPI. The spring.factories file is stored in key=value format, and the loader finds the value based on the key.

 

As a handsome boy, I prepared the interview answers for you

Through @ Import (AutoConfigurationImportSelector) to realize the configuration class Import, but not in the traditional sense of the single Import, but in bulk

AutoConfigurationImportSelector class implements the ImportSelector interface, rewrite the method selectImports, used to implement the batch assembly

Using the spirng Tigao de SpringFactoriesLoader mechanism, the meta-INF/Spring. factories in the classpath path are scanned to read the configuration classes that need to implement auto-assembly.

Finally, through the way of screening, put forward the inconsistent classes, and finally complete the automatic assembly of SpringBoot