春のブート時に自動的に設定する原則

春ブートソース面分析:

1、春のブート起動クラス

@SpringBootApplication
 パブリック クラス MgmtApplicationは延びSpringBootServletInitializer { 
    @Overrideが
    保護SpringApplicationBuilderの設定(SpringApplicationBuilderビルダー){
         戻り builder.sources(MgmtApplication クラス)。
    } 

    パブリック 静的 ボイドメイン(文字列[]引数){ 
        SpringApplication.run(MgmtApplication。クラス、引数)。
    } 
}

私たちはインポートカテゴリ、輸入カテゴリとして、MgmtApplicationを参照してください、通常はアプリケーション全体を起動するmainメソッドSpringApplication.run()で使用される実際のJavaアプリケーションのための標準的な入力方法である主な方法は、あります。@SpringBootApplicationアノテーションを使用するための入り口クラスは、それがSpringBootコメントのコアであることを宣言していることは注目に値します。

2、@ SpringBootApplicationソース

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented 
@Inherited 
@SpringBootConfiguration 
@EnableAutoConfiguration 
@ComponentScan(
    excludeFilters = {@Filter(
    タイプ = FilterType.CUSTOM、
    クラスは = {TypeExcludeFilter。クラス} 
)、@Filter (
    タイプ = FilterType.CUSTOM、
    クラス = {AutoConfigurationExcludeFilter。クラス} 
)} 
パブリック @のインタフェースSpringBootApplication {
    // ... 
}

この注釈内の、最も重要なのは、ある@EnableAutoConfiguration

3、@ EnableAutoConfigurationソース

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented 
@Inherited 
@AutoConfigurationPackage 
@import({。AutoConfigurationImportSelector クラス})
 パブリック @のインタフェースEnableAutoConfiguration { 
    文字列ENABLED_OVERRIDE_PROPERTYは = "spring.boot.enableautoconfiguration" 

    クラス <?> []除外()デフォルト{}。

    文字列[] excludeName()デフォルト{}。
}

これは注釈構成インポート完了@import @EnableAutoConfigurationにノートに見ることができ、そしてMETA-INF / spring.factoriesファイルにEnableAutoConfigurationImportSelector SpringFactoriesLoader.loadFactoryNames方法ジャーパッケージスキャン内で使用されています。ここで2.1.8.RELEASE達成ソースは以下のとおりです。

公共のString [] selectImports(AnnotationMetadata annotationMetadata){
     場合(!この{.isEnabled(annotationMetadata))
         戻りNO_IMPORTS。
    } { 
        AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader.loadMetadata(この.beanClassLoader)。
        AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = この.getAutoConfigurationEntry(autoConfigurationMetadata、annotationMetadata)。
        返すStringUtils.toStringArray(autoConfigurationEntry.getConfigurationsを()); 
    } 
}

保護されたAutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata、AnnotationMetadata annotationMetadata){
     場合(!この.isEnabled(annotationMetadata)){
         返すEMPTY_ENTRYを。
    } { 
        AnnotationAttributes属性 = この.getAttributes(annotationMetadata)。
    //扫描具有META-INF / spring.factories文件的ジャー包  一覧
<文字列>構成= この.getCandidateConfigurations(annotationMetadata、属性); 構成 = この.removeDuplicates(構成)。 設定して <文字列>の除外= この.getExclusions(annotationMetadata、属性); この.checkExcludedClasses(構成、除外)。 configurations.removeAll(除外)。 構成 = この.filter(構成、autoConfigurationMetadata)。 この.fireAutoConfigurationImportEvents(構成、除外)。 返す 新しいAutoConfigurationImportSelector.AutoConfigurationEntry(構成、除外)。 } } //加载spring.factories实现
保護されたリストの<string> getCandidateConfigurations {(AnnotationMetadataメタデータ、属性AnnotationAttributes)
  リスト
の<string>構成= SpringFactoriesLoader.loadFactoryNames(この .getSpringFactoriesLoaderFactoryClass()、この.getBeanClassLoaderを())。
Assert.notEmpty(構成、
「META-INF / spring.factoriesが見つかりません自動設定クラスは、カスタムパッケージを使用している場合は、ファイルが正しいことを確認してください。。」);
  戻り値の設定;
}

下記のパッケージにspringboot-自動設定、およびspring.factoriesファイルを導入する任意のアプリケーションをSpringboot。spring.factories文書フォームキーである=値、値の初期設定、モニタ等に、このファイル情報に定義された間隔を置いた複数のは、実質的に自動的に設定実際のキーとなるようorg.springframework.boot.autoconfigure.EnableAutoConfiguration次のように:

#自動設定
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春ブート自動構成関連クラスであるクラス、複数の上方に配置され、起動時にコンフィギュレーション情報に対応するクラスを解析します。各クラスは、関連豆のConfiguation構成例を規定します。彼らは、Beanが条件を自動的に設定することができるものの下で、自動的に設定することができるかを示した、とこれらのBeanは、それをインスタンス化します。

おすすめ

転載: www.cnblogs.com/Z-share/p/11714082.html
おすすめ