簡単に開始SpringBoot

まず、クラスのノートと開始SpringBootApplication

それは二重のコメントです。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@継承されました
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(タイプ = FilterType.CUSTOM、クラス= TypeExcludeFilter。クラス
@Filter(タイプ = FilterType.CUSTOM、
クラス = AutoConfigurationExcludeFilter。クラス)})

 

上記の注意事項では、我々は3つの音に注意を払う必要があります:

1.1 @SpringBootConfiguration:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
パブリック @ インタフェースSpringBootConfiguration {

}

 

@Configurationコメント

設定注釈機能は現在のクラスコンフィギュレーションクラス、現在のクラスおよび例@Bean注釈マーカーの方式としてマークされている
ばね容器に注入され、インスタンス名は、メソッドの名前です。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@成分
パブリック @ インタフェースの設定{

手段はまた、Beanとして登録されますこのコメントを参照してください@Component、それはまた、内部の依存性注入することができます。@Configurationタイプが一般的に使用さ
AnnotationConfigApplicationContextをまたはWeb案内AnnotationConfigWebApplicationContext変異体。
フォローアップの基礎となる操作機構春深さにもっと勉強し続けることができます......

1.2 @EnableAutoConfiguration

@EnableAutoConfigurationコメントは、自動設定を有効にするには、SpringBootアプリケーションがすべてを遵守することができます
@Configuration設定は、現在のIoCコンテナにロードされた条件。AutoConfigurationImportSelectorので@EnableAutoConfiguration
助け、後者は構成electImports()メソッドを実装することによって導出されます。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@継承されました
@AutoConfigurationPackage
@import(AutoConfigurationImportSelector。クラスパブリック @ インタフェース EnableAutoConfiguration {

ソース分析:

@Override
 パブリック文字列[] selectImports(AnnotationMetadata annotationMetadata){
 場合(!ISENABLED(annotationMetadata)){
 戻りNO_IMPORTS。
}
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(この.beanClassLoader)。
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(
autoConfigurationMetadata、annotationMetadata)。
リターンStringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}

保護されたAutoConfigurationEntry getAutoConfigurationEntry(
AutoConfigurationMetadata autoConfigurationMetadata、
AnnotationMetadata annotationMetadata){
もし(!ISENABLED(annotationMetadata)){
 戻りEMPTY_ENTRY。
}
AnnotationAttributes属性 = のgetAttributes(annotationMetadata)。
一覧 <文字列>構成= getCandidateConfigurations(annotationMetadata、
属性);
構成 = removeDuplicates(構成)。
セット <文字列>の除外= getExclusions(annotationMetadata、属性);
checkExcludedClasses(構成、除外)。
configurations.removeAll(除外)。
構成 = フィルタ(構成、autoConfigurationMetadata)。
fireAutoConfigurationImportEvents(構成、除外)。
返す 新しいAutoConfigurationEntry(構成、除外)。
}

バッグSpringFactoriesLoader内部のスプリングコアを呼び出すことによってselectImports AutoConfigurationImportSelectorクラス()メソッド

loadFactoryNamesクラス()メソッド

保護されたリストの<文字列> getCandidateConfigurations(AnnotationMetadataメタデータ、
AnnotationAttributes属性){
一覧 <文字列>構成= SpringFactoriesLoader.loadFactoryNames(
getSpringFactoriesLoaderFactoryClass()、getBeanClassLoader())。
Assert.notEmpty(構成、
「いいえ自動設定クラスは、META-INF / spring.factoriesで見つかりませんでした。あなたの場合は」
+「カスタムパッケージを使用している、ファイルが正しいことを確認してください。」);
戻り値の設定;
}

クラスパスEnableAutoConfigurationとspring.factoriesファイルからのすべてのMETA-INF / spring.factoriesプロファイルスキャン
マーク@Configuration IoCコンテナのコンフィギュレーションクラスの形で反射機構によってインスタンスに対応する構成要素に対応し、その後移植IoCコンテナ。

1.3 @ComponentScan

実際には、組み立てスキャン、およびアセンブリ自動的に豆を発見し、機能が自動的にスキャンし、ロードは、アセンブリ修飾またはBean定義は、最終的には豆プラス定義する
IoCコンテナにロードします。指定されていない、デフォルトの場合の範囲は、basePackages自動スキャンすることによって、このようなカスタム@ComponentScanなどきめ細かい属性することができ
春のフレームワークの実装は、クラス宣言@ComponentScanパッケージからスキャンされます。SpringApplicationの方法は、スキャンの実行をデフォルトで
それがルートクラスパスに次のパッケージを起動するのが最善の方法ですので、パケットファイルパスBooter.class。
この注釈にも等basePackages属性による自動アノテーションの走査範囲の、よりきめ細かく制御することができます。

@ComponentScan(basePackages = { "cn.shu.controller"、 "cn.shu.entity" })

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans。クラスパブリック @ インタフェース ComponentScan {

二、SpringApplicationクラス

また、オブジェクトのrun()メソッドを呼び出した後、SpringApplicationクラスのオブジェクトを作成する必要があります。そして、残りは建設SpringApplicationの話で
これを理解するために、フロー・プロセスとそのrun()メソッドは、SpringBootアプリケーションが稼働している方法を見つけ出すだろう!

構成パラメータの2.1 SpringApplicationオブジェクトクラス名の例。

ソースコードを見てください:

@SuppressWarnings({ "未チェック"、 "rawtypes" })
 パブリック SpringApplication(ResourceLoader resourceLoader、クラス<?> ... primarySources){
 この .resourceLoader = resourceLoader。
Assert.notNull(primarySources、 "PrimarySourcesであってはならないことはnull" );
 この .primarySources = 新しい新しい LinkedHashSetの<> は、Arrays.asList(primarySources));
 // 型推論用途:創造ですREACTIVEアプリケーション、サーブレットアプリケーション、NONE 3中の種の特定の種類
この .webApplicationType = WebApplicationType.deduceFromClasspath()。
setInitializers((コレクション)getSpringFactoriesInstances(
ApplicationContextInitializer。クラス));
setListeners((コレクション)getSpringFactoriesInstances(ApplicationListener。クラスは));
 // 開始クラスなどの表面クラスのメソッドを取得します。
この .mainApplicationClass = deduceMainApplicationClass()。
}

2.2どのようにアプリケーションカテゴリとコンテキストの違い:REACTIVE、SERVLET、NONE

SERVLET:{ "のjavax.servlet.Servlet"  "org.springframework.web.context.ConfigurableWebApplicationContext" }。
プライベート 静的な 最終文字列WEBMVC_INDICATOR_CLASS =「org.springframework。」
+ "web.servlet.DispatcherServlet" プライベート 静的な 最終文字列JERSEY_INDICATOR_CLASS =「org.glassfish.jersey.servlet.ServletContainer」
反応性:     プライベート 静的な 最終文字列WEBFLUX_INDICATOR_CLASS =「ORG。」
+ "springframework.web.reactive.DispatcherHandler" 
なし:その他。

静的WebApplicationType deduceFromApplicationContext(
クラス <?> applicationContextClass){
 場合(isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS、applicationContextClass)){
 戻りWebApplicationType.SERVLET。
}
もし(isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS、applicationContextClass)){
 戻りWebApplicationType.REACTIVEと、
}
リターンWebApplicationType.NONE;
}

 

初期化容器に入れすべてApplicationContextInitializer.class 2.3を得る例です。

setInitializers((コレクション)getSpringFactoriesInstances(
ApplicationContextInitializer。クラス));

UampServletInitializerは拡張 SpringBootServletInitializer 実装WebApplicationInitializer

パブリック クラス ServletContextApplicationContextInitializer 実装
ApplicationContextInitializer <ConfigurableWebApplicationContext> 順序

パブリック インターフェース ApplicationContextInitializer <Cが延び ConfigurableApplicationContext>
 パブリック インターフェイス ConfigurableApplicationContextが延びApplicationContextの、ライフサイクル、閉鎖可能に

パブリック インターフェイスのApplicationContextは拡張し、EnvironmentCapable、ListableBeanFactory、HierarchicalBeanFactoryを
MessageSource、ApplicationEventPublisher、ResourcePatternResolver {

 

2.4 SpringFactoriesLoaderは、利用可能なすべてのApplicationListenerクラスパスの次のファイルを検索し、ロードします。

setListeners((コレクション)getSpringFactoriesInstances(ApplicationListener.class));

第三に、コアSpringApplicationメソッドの実行の簡潔な分析()

 

3.1ソースコードは以下の通りであります:

公共ConfigurableApplicationContext RUN(文字列...引数){
 // スプリングフレームワークは、パッケージ開始時の、すなわち一組のJavaクラスの記録動作の終了時、ストップウォッチクラスは、同様のタスクの実行時間制御を行うことができ提供 
ストップウォッチストップウォッチ= 新しい新しいストップウォッチ();
stopWatch.start(); //タイミングタスクを開始 
= ConfigurableApplicationContextコンテキストをヌル
コレクション <SpringBootExceptionReporter> exceptionReporters = 新しい ArrayListを<> ();
configureHeadlessProperty();
SpringApplicationRunListenersリスナー = getRunListeners(引数)。
listeners.starting();
してみてください{
ApplicationArguments applicationArguments = 新しいDefaultApplicationArguments(
引数);
ConfigurableEnvironment環境 = prepareEnvironment(リスナー、
applicationArguments);
// それが空の場合、セットするSystem.setPropertyのトゥーレ( "spring.beaninfo.ignore"、ignore.toString());.ある
ConfigureIgnoreBeanInfo(環境);
 // 印刷バナー 
バナーprintedBanner = printBanner(環境)。
コンテキスト = createApplicationContext()。
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter。クラス新しいクラス[] {ConfigurableApplicationContext。クラス}、コンテキスト)。
prepareContext(コンテキスト、環境、リスナー、applicationArguments、
printedBanner);
refreshContext(コンテキスト)。
afterRefresh(コンテキスト、applicationArguments)。
stopWatch.stop();
もしこの.logStartupInfo){
 新しい StartupInfoLogger(この.mainApplicationClass)
.logStarted(getApplicationLog()、ストップウォッチ)。
}
(コンテキスト)listeners.started。
callRunners(コンテキスト、applicationArguments)。
}
キャッチ(ThrowableのEX){
handleRunFailure(コンテキスト、EX、exceptionReporters、リスナー)。
スロー 新しいIllegalStateExceptionを(EX)を、
}

してみてください{
listeners.running(コンテキスト)。
}
キャッチ(ThrowableのEX){
handleRunFailure(コンテキスト、EX、exceptionReporters、NULL );
スロー 新しいIllegalStateExceptionを(EX)を、
}
戻り値のコンテキスト。
}

3.2フローチャート:

 

 

次のようにプロセスを簡単に全体で3.3に要約しました:

  • 通过this.getSpringFactoriesInstances(SpringApplicationRunListener.class、種類、この、引数)
  • SpringApplicationRunListenerオブジェクトを取得して作成します。
  • メッセージを開始すると、その後SpringApplicationRunListenerに送信されます
  • パラメータを作成し、環境で使用される現在のSpringBootアプリケーションを構成します
  • 終了したら、まだSpringApplicationRunListener environmentPreparedからメッセージを送信します
  • 初期のApplicationContext、および環境を設定し、負荷の設定など
  • SpringApplicationRunListenerによってSpringBootアプリケーションを伝える問題へcontextPreparedメッセージは、使用のApplicationContext OKの準備ができています
  • 豆は、種々のApplicationContextにロードされ、メッセージはアプリケーションに通知するために使用されるcontextLoaded SpringApplicationRunListenerを、送信され続ける充填のApplicationContext OKをSpringBoot
  • 利用できるIoCコンテナの最後のステップを完了するために、ApplicationContextのリフレッシュ
  • メッセージを発するように始めSpringApplicationRunListener
  • 最終的なプログラムの完了を開始
  • プログラムが起動され、実行していることを実行しているメッセージを発行するSpringApplicationRunListener

おすすめ

転載: www.cnblogs.com/Lambquan/p/12024494.html