spring boot 啓動類註解詳解

@SpringBootApplication: Spring Boot應用標註在某個類上說明這個類是SpringBoot的主配置類,SpringBoot

就應該運行這個類的main方法來啓動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}
)}
)

@SpringBootConfiguration:Spring boot的配置類,標註在某個類上,標誌着這是一個配置類;

@EnableAutoConfiguration 自動配置功能 以前我們需要配置的東西,Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啓自動配置功能;這樣自動配置才能生效;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自動配置包
@Import(AutoConfigurationPackages.Registrar.class)
Spring的底層註解@Import,給容器中導入一個組件;導入的組件由
AutoConfigurationPackages.Registrar.class
將主配置類(@SpringBootApplication標註的類)的所在包及下面所有子包裏面的所有組件掃描到Spring容器;
@Import(EnableAutoConfigurationImportSelector.class)
給容器中導入組件?
EnableAutoConfigurationImportSelector:導入哪些組件的選擇器;
將所有需要導入的組件以全類名的方式返回;這些組件就會被添加到容器中;
會給容器中導入非常多的自動配置類(
xxxAutoConfiguration);就是給容器中導入這個場景需要的所有組件,

並配置好這些組件;

有了自動配置類,免去了我們手動編寫配置注入功能組件等的工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)
Spring Boot在啓動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,將
這些值作爲自動配置類導入到容器中,自動配置類就生效,幫我們進行自動配置工作;
以前我們需要自己配置的東
西,自動配置類都幫我們;

J2EE的整體整合解決方案和自動配置都在spring-boot-autoconfigure-1.5.9.RELEASE.jar

List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes)
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());

Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章