1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring Bootで作ったアプリの起動時イベント等が書けるらしい

Last updated at Posted at 2020-01-11

Spring Bootでもアプリが起動した時とかにイベントかませられるらしい
ただ公式リファレンスにもあんま使い道ないやろ的なことが書いてあったし
僕も何に使うかわからん・・・
ただ、こういうのもできるよっていうメモ

環境

Spring Boot 2.2.2.RELEASE - 2.2.4.RELEASE
Kotlin 1.3.61
Gradle

環境は以下の記事のまんま

エントリポイントで指定する

SplingBootExampleApplication.kt
@SpringBootApplication
class SpringBootExampleApplication

fun main(args: Array<String>) {

	println("start main method")

	val application = SpringApplication(SpringBootExampleApplication::class.java)

	// 各種イベント追加
	application.addListeners( MyApplicationStartingEventListener() )
	application.addListeners( MyApplicationEnvironmentPreparedEventListener() )
	application.addListeners( MyApplicationContextInitializedEventListener() )
	application.addListeners( MyApplicationPreparedEventListener() )
	application.addListeners( MyApplicationStartedEventListener() )
	application.addListeners( MyApplicationReadyEventListener() )

	application.run(*args)

	// Spring Initializrでの生成時に記載されてたアプリ起動メソッド
	// runApplication<SpringBootExampleApplication>(*args)

}
各Listenerクラス.kt
class MyApplicationContextInitializedEventListener : ApplicationListener<ApplicationContextInitializedEvent> {
    override fun onApplicationEvent(event: ApplicationContextInitializedEvent) = println("Application Context Initialized Event")
}

課題

  • 使い道
  • イベント処理は別クラスに移せたけどaddListenersも別クラスにかけないかなぁ いい感じにやってくれる機能があると嬉しい
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?