淺析spring boot自動配置原理

spring boot 源碼刨析:

一、spring boot 啓動類php

@SpringBootApplication
public class MgmtApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MgmtApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MgmtApplication.class, args);
    }
}

咱們看到,MgmtApplication做爲入口類,入口類中有一個main方法,這個方法其實就是一個標準的Java應用的入口方法,通常在main方法中使用SpringApplication.run()來啓動整個應用。值得注意的是,這個入口類要使用@SpringBootApplication註解聲明,它是SpringBoot的核心註解。spring

二、@SpringBootApplication 源碼springboot

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
   //... 
}

這個註解裏面,最主要的就是@EnableAutoConfigurationide

三、@EnableAutoConfiguration的源碼spring-boot

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

能夠看到,在@EnableAutoConfiguration註解內使用到了@import註解來完成導入配置的功能,而EnableAutoConfigurationImportSelector內部則是使用了SpringFactoriesLoader.loadFactoryNames方法進行掃描具備META-INF/spring.factories文件的jar包。下面是2.1.8.RELEASE實現源碼:ui

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

protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) {
    if (!this.isEnabled(annotationMetadata)) {
        return EMPTY_ENTRY;
    } else {
        AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
    //掃描具備META-INF/spring.factories文件的jar包  List
<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes); configurations = this.removeDuplicates(configurations); Set<String> exclusions = this.getExclusions(annotationMetadata, attributes); this.checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = this.filter(configurations, autoConfigurationMetadata); this.fireAutoConfigurationImportEvents(configurations, exclusions); return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions); } }
//加載spring.factories實現
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;
}

任何一個springboot應用,都會引入spring-boot-autoconfigure,而spring.factories文件就在該包下面。spring.factories文件是Key=Value形式,多個Value時使用,隔開,該文件中定義了關於初始化,監聽器等信息,而真正使自動配置生效的key是org.springframework.boot.autoconfigure.EnableAutoConfiguration,以下所示:this

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
...

上面的EnableAutoConfiguration配置了多個類,這些都是Spring Boot中的自動配置相關類;在啓動過程當中會解析對應類配置信息。每一個Configuation類都定義了相關bean的實例化配置。都說明了哪些bean能夠被自動配置,什麼條件下能夠自動配置,並把這些bean實例化出來。spa

相關文章
相關標籤/搜索