Spring Boot 原理<1>

玉獅子發表於2019-04-16

在公司使用SSM做了若干個專案,新的專案基本上就是Ctrl+c原來配置。後來公司“緊跟潮流”後面專案改用Spring Boot,由於封裝嚴重,有時出了問題網上都找不到解解決方案,還是乖乖自己捋一捋Spring Boot吧!

Spring Boot是什麼

官網解釋如下

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".

We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

Spring Boot的核心是基於Spring構建的,官方對Spring Boot的定位是Build Anything,旨在快速、高效,使用最少的配置構建Java應用。

  • Spring Cloud—————————Coordinate Anything (協調任何東西)
  • Spring Cloud Data Flow—————Connect Everything (連線任何東西)

Spring Boot原理

啟動類如下

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
複製程式碼

SpringApplication是什麼類?@SpringBootApplication又是什麼?點選到原始碼:

@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}
)}
)
複製程式碼
  • @Target(ElementType.TYPE) // 註解的適用範圍
  • @Retention(RetentionPolicy.RUNTIME) // 註解的生命週期
  • @Documented // 表明這個註解應該被javadoc記錄
  • @Inherited // 子類可以繼承該註解
  • @SpringBootConfiguration //表明某個類是springboot的配置類 底層是 @Configration ,本身其實也是一個IoC容器的配置類
  • @ComponentScan //自動掃描並載入符合條件的元件 ,比如@Component和@Repository載入到IoC容器中。
  • @EnableAutoConfiguration開啟自動配置功能。

@EnableAutoConfiguration

@EnableAutoConfiguration對啟動有至關重要的作用,也是一個複合註解。原始碼如下:

@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 {};
}
複製程式碼

核心兩項

  • @AutoConfigurationPackage:自動配置包
  • Import(AutoConfigurationImportSelector.class)註解

通過原始碼發現:@EnableAutoConfiguration也是藉助@Import將所有符合自動配置條件的bean定義載入到IoC容器。

@AutoConfigurationPackage也是一個複合式註解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
複製程式碼

AutoConfigurationImportSelector核心方法為selectImports:s

@Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        if (!isEnabled(annotationMetadata)) {
            return NO_IMPORTS;
        }
        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
                .loadMetadata(this.beanClassLoader);
        AnnotationAttributes attributes = getAttributes(annotationMetadata);
        List<String> configurations = getCandidateConfigurations(annotationMetadata,
                attributes);
        configurations = removeDuplicates(configurations);
        Set<String> exclusions = getExclusions(annotationMetadata, attributes);
        checkExcludedClasses(configurations, exclusions);
        configurations.removeAll(exclusions);
        configurations = filter(configurations, autoConfigurationMetadata);
        fireAutoConfigurationImportEvents(configurations, exclusions);
        return StringUtils.toStringArray(configurations);
    }
複製程式碼

藉助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration可以將所有符合條件的配置都載入到當前IoC容器

相關文章