エレガントなコンフィギュレーションモニタをSprinbootし、すべてのイベントが起動録音

ソースコードを読み始めるSpringboot、私が見つけたSpringbootリスナーが自動的にuopeizhi設定ファイルによって活性化され、用紙が自動的にSpringbootの方法リスナーを使用してロードされます。

プロジェクトの依存関係


<親>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>春・ブート・スターター・親</たartifactId>
<バージョン> 1.5.8.RELEASE </バージョン>
<relativePath /> <! -検索リポジトリから親- >
</親>

<依存性>
<依存>
<groupIdを> org.springframework.boot </ groupIdを>
<たartifactId>春・ブート・スターター・ウェブ</たartifactId>
</依存関係>

<依存性>
<のgroupId> org.springframework.boot </のgroupId>
<たartifactId>ばねブートスタータ試験</たartifactId>
</依存>

<依存性>
<のgroupId> JUnitの</のgroupId>
<たartifactId> JUnitの</たartifactId>
<バージョン> 3.8.1 </バージョン>
<スコープ>テスト</スコープ>
</依存>
</依存関係>
<構築>
<finalName >ブート</ finalName>
</ビルド>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
项目监听配置

次のようにリスナークラスの完全なパスを設定し、リソースディレクトリにMETA-INFフォルダを建て、そしてフォルダがspring.factoriesの下に作成されたファイルは、最初の行は、キーを見springbootをロードされ、その設定が可能

org.springframework.context.ApplicationListener = \
boot.Listener.MyListener、\
boot.Listener.DateListener
1
2
3
boot.Listener.MyListener

パッケージboot.Listener;

輸入org.springframework.context.ApplicationEvent。
輸入org.springframework.context.ApplicationListener。

パブリッククラスMyListenerが実装ApplicationListener <ApplicationEvent> {
@Override
公共ボイドonApplicationEvent(ApplicationEvent paramE){
するSystem.out.println( "--------" + paramE.getClass()のgetName()。)。
}
}

。1
2
3
4
5
6
7
8
9
10
11
12である
すべてのノートログを開始するためのイベントを開始します

-------- ** org.springframework.boot.context.event.ApplicationStartedEvent **
-------- ** org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent **

____ _ __ _ _
/ \\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
(()\ ___ | '_ |' _ | |「_ \ / _` | \ \ \ \
\\ / ___)| | _)| | | | | || (_ | |))))
'| ____ | .__ | _ | | _ | _ | | _ \ __、| / / / /
========= | _ | ============== | ___ / = / _ / _ / _ /
::春ブーツ::(V1。 5.8.RELEASE)

2019年5月21日14:36:56.423 INFO 4120 --- [メイン] boot.application:PID 4120でLIUJP上のアプリケーションを起動する(Eに管理者によって開始された:\プログラムファイル\ wowkSpace \ブーツ)
2019年5月21日14 :36:56.435 INFO 4120 --- [メイン] boot.application:いいえアクティブなプロファイル設定、デフォルトのプロファイルにフォールバック:デフォルト
-------- ** org.springframework.boot.context.event.ApplicationPreparedEvent * *
2019年5月21日14:36:56.718 INFO 4120 --- [メイン] ationConfigEmbeddedWebApplicationContext:さわやかorg.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21a947fe:起動日[火5月21日夜2時36分56秒CST 2019] ; コンテキスト階層のルート
2019年5月21日14:37:02.015 INFO 4120 --- [メイン] sbcetTomcatEmbeddedServletContainer:Tomcatがポート(複数可)で初期化:8080(HTTP)
2019年5月21日14:37:02.062 INFO 4120 --- [メイン] o.apache.catalina.core.StandardService:サービス開始[Tomcatの]
2019年5月21日14:37:02.078 INFO 4120 --- [メイン] org.apache.catalina.core.StandardEngine:サーブレットエンジンを始動:Apache Tomcatの/ 8.5.23
2019年5月21日14:37:02.624 INFO 4120 --- [OST-startStop-1] oaccC [Tomcatの] [ローカルホスト。 ] [/]:初期化スプリングWebApplicationContext埋め込み
2019年5月21日14:37:02.624 INFO 4120 --- [OST-startStop-1] osweb.context.ContextLoader:ルートWebApplicationContext:5922ミリ秒で完了し、初期化を
2019-05- 21 14:37:03.218 INFO 4120 --- [OST-startStop-1] osbwservlet.ServletRegistrationBean:マッピングサーブレット:[/]に'のDispatcherServlet'
2019年5月21日14:37:03.235 INFO 4120 --- [OST-startStop-1] osbwservlet.FilterRegistrationBean:マッピングフィルタ:に'characterEncodingFilter':/ *]
2019年5月21日14:37:03.251 4120 INFO --- [OST-startStop-1] osbwservlet.FilterRegistrationBean:マッピングフィルタ:に'hiddenHttpMethodFilter':/ *]
2019年5月21日14:37:03.251 INFO 4120 --- [OST-startStop-1] osbwservlet。 FilterRegistrationBean:マッピングフィルタ: 'httpPutFormContentFilter'へ:[/ *]
2019年5月21日14:37:03.251 INFO 4120 --- [OST-startStop-1] osbwservlet.FilterRegistrationBean:マッピングフィルタ: 'requestContextFilter'へ:[/ *]
2019年5月21日14:37:04.469 INFO 4120 --- [メイン] swsmmaRequestMappingHandlerAdapter:org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21a947fe:@ControllerAdvice探して起動日[火5月21日14時36分56秒CST 2019]; コンテキスト階層のルート
2019年5月21日14:37:04.725 INFO 4120 --- [メイン] swsmmaRequestMappingHandlerMapping:マッピングされた"{[/エラー]}"パブリックorg.springframework.http.ResponseEntity上<java.util.Map <ジャワ.lang.String、java.lang.Objectの>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(のjavax.servlet.http.HttpServletRequest)
2019年5月21日14:37:04.725 INFO 4120 --- [メイン] swsmmaRequestMappingHandlerMapping:マッピングされた公共org.springframework.web.servlet.ModelAndViewの組織に"{[/エラー]は、= [テキスト/ HTML]産生}"。 springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(するjavax.servlet.http.HttpServletRequest、javax.servlet.http.HttpServletResponseの)
2019年5月21日14:37:04.835 INFO 4120 --- [メイン] oswshandler.SimpleUrlHandlerMapping。タイプのハンドラにマッピングされたURLパス[/ webjars / **] [クラスorg.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019年5月21日14:37:04.850 INFO 4120 --- [メイン] oswshandler.SimpleUrlHandlerMapping:タイプのハンドラにマッピングされたURLパス[/ **] [クラスorg.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019年5月21日14:37:04.991 INFO 4120 --- [メイン] oswshandler.SimpleUrlHandlerMapping:マッピングされたURLのパス[/**/favicon.ico]種類のハンドラへの[クラスorg.springframework.web.servlet.resource。 ResourceHttpRequestHandler]
2019年5月21日14:37:05.491 INFO 4120 --- [メイン] osjeaAnnotationMBeanExporter:起動時にJMX露光用の豆を登録
-------- ** org.springframework.context.event.ContextRefreshedEvent **
2019年5月21日14:37:05.716 INFO 4120 --- [メイン] sbcetTomcatEmbeddedServletContainer:8080(HTTP):Tomcatが(S)ポートで起動
-------- ** org.springframework.boot.contextを。 embedded.EmbeddedServletContainerInitializedEvent **
-------- ** org.springframework.boot.context.event.ApplicationReadyEvent **
2019年5月21日14:37:05.732 INFO 4120 --- [メイン] boot.application:(11.845ための実行JVM)10.546秒で起動アプリケーション

。1
2
3
4
5
6
7
8
9
10
11
12である
13である
14
15
16
17
18である
。19
20
21であり、
22は
23である
24
25
26である
27
28
29
30
31であり、
32
33は
34である
35
36
37
38である
ことがSpringbootの起動時間が続いて見ることができる上に
org.springframeworkを.boot.context.event.ApplicationStartedEvent
org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent
org.springframework.boot.context.event.ApplicationPreparedEvent
org.springframework.context.event.ContextRefreshedEvent
org.springframework.boot.context.event.ApplicationReadyEvent

ソースモニター

SpringApplication.run(application.class、引数)。

SpringApplicationRunListenersリスナー=のgetRunListeners(引数)。

プライベートSpringApplicationRunListenersのgetRunListeners(文字列[]引数){
クラス[]タイプ=新しいクラス[] {SpringApplication.class、文字列[]クラス} <?> <?>。
新しいSpringApplicationRunListeners(ロガー、getSpringFactoriesInstances(戻る
SpringApplicationRunListener.class、種類、この、引数を));
}

プライベート<T>コレクション<?T> getSpringFactoriesInstances(クラス<T>型、延び
?クラス<> [] parameterTypesパラメータ、オブジェクト...引数){
たClassLoaderクラスローダ=にThread.currentThread()getContextClassLoader()。
//使用名と重複から保護するために独自の確保
を設定し、<文字列>名=新しいLinkedHashSetの<文字列>(
SpringFactoriesLoader.loadFactoryNames(タイプ、クラスローダ));
リスト<T>インスタンス= createSpringFactoriesInstances(タイプ、parameterTypesパラメータ、
クラスローダ、引数、名前);
AnnotationAwareOrderComparator.sort(インスタンス)。
インスタンスを返します。
}

public static final String型FACTORIES_RESOURCE_LOCATION = "META-INF / spring.factories";

公共の静的リストの<string> loadFactoryNames(クラスfactoryClass、のClassLoaderクラスローダ<?>){
文字列factoryClassName = factoryClass.getName()。
試す{
列挙<URL> URLを=(クラスローダ= nullのclassLoader.getResources(FACTORIES_RESOURCE_LOCATION):!?
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
一覧<文字列>結果=新しいのArrayList <文字列>();
一方、(urls.hasMoreElements()){
URLのURL = urls.nextElement()。
プロパティプロパティ=のPropertiesLoaderUtils.loadProperties(新しいUrlResource(URL));
ストリングfactoryClassNames = properties.getProperty(factoryClassName)。
result.addAll(は、Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)))。


}
キャッチ(のIOExceptionのEX){
新をスロー( "[+ factoryClass.getName()+できないロードする"
"]の位置から工場[" + FACTORIES_RESOURCE_LOCATION + "]"、EX)。
}
}

---------------------

おすすめ

転載: www.cnblogs.com/ly570/p/10930826.html