SpringBoot source code analysis (a) @SpringBootApplication resolve

@SpringBootApplication解析

First, the three notes

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
      @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
      @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

二、@ComponentScan

1. xml configuration file corresponding to the context: Component-Scan ,

2. ComponentScan default scans all the current class plus the related notes identified under the category where the package is to IoC container

3. The main role is to identify the needs of the assembly under the category designated scan path, automatic assembly of the spring Ioc container. The main forms of identification are: @Component, @ Repository, @Service, @Controller such comments identifies the class.

4. Scan is this something to write our own Bean

三、@SpringBootConfiguration

Open source is essentially a @Configuration, configuration class is indicated. (Behind the run method will pass into their own, their own notes only other failure)

四、@EnableAutoConfiguration

It is formed from two notes:

@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)

Then open @AutoConfigurationPackage Source:

@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
}

Summary: @EnableAutoConfiguration equal

= @Import(AutoConfigurationPackages.Registrar.class)+@Import(AutoConfigurationImportSelector.class)

4.1 @Import

IOC introduced into a container class

4.2 AutoConfigurationPackages.Registrar

static class Registrar implements ImportBeanDefinitionRegistrar, DeterminableImports {

   @Override
   public void registerBeanDefinitions(AnnotationMetadata metadata,
         BeanDefinitionRegistry registry) {
      register(registry, new PackageImport(metadata).getPackageName());
   }

   @Override
   public Set<Object> determineImports(AnnotationMetadata metadata) {
      return Collections.singleton(new PackageImport(metadata));
   }

}

Which registerBeanDefinitions method is to register bean == AutoConfigurationPackages class, this class holds the package we scan path (com.xiaomi. This project name, start by sb Class class to get information), class comments said to be "used class storage provisioning packet for later use (such as a scanner for scanning the entity to the JPA developer entity class @Entity annotation by definition) "

4.3 AutoConfigurationImportSelector

Based ImportSelector to achieve dynamic load function is based on the bean. ImportSelector selectImports method returns an array interface (fully qualified class name of the class) will be incorporated into the spring container.

More important is to achieve self-DeferredImportSelector interface method selectImports

public String[] selectImports(AnnotationMetadata annotationMetadata) {
   if (!isEnabled(annotationMetadata)) {
      return NO_IMPORTS;
   }
   AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
         .loadMetadata(this.beanClassLoader);
   AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
         autoConfigurationMetadata, annotationMetadata);
   return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}

The main hit getAutoConfigurationEntry method, the step by step follow it will be found in class (work content SpringFactoriesLoader class) in the META-INF under load classpath path / spring.factories file, so that is scalable, everyone this file can add your own automatic assembly.

Rest of the code is determined whether spring.factories class file should be loaded (provided some conditions ConditionalOn), repeated removal, etc. getAutoConfigurationEntry conflict remaining codes are as follows:

configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);

4.4 SpringFactoriesLoader

spring.factories file:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

This is mybatis spring.factories springboot for automatic assembly, in this case, the document can be activated by the class MybatisAutoConfiguration mybatis the automatic assembling, without any further action

Guess you like

Origin www.cnblogs.com/chz-blogs/p/12565783.html