疑問
SpringBootにおけるpom.xml
やbuild.gradle
といったビルド設定ファイルを見て、「このstarter
ってついているのってなんでだっけ?」とふと疑問に思ったことはありませんか?
dependencies {
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
runtimeOnly("com.mysql:mysql-connector-j")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.4")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
調べてみた
困ったときは公式リファレンスを見ます。
(geminiに聞いてなんとく納得しても裏どりが出来ていないと不安なので最近は公式リファレンスを漁るようにしています。日本語ドキュメントがあっても日本語が崩壊していて意味が分からない時があるので英語で読んでみたりしますが、英語で読んでもネイティブではないので結局100%意味が理解できているかというと怪しい。)
以下のような記載がありました。
https://docs.spring.io/spring-boot/reference/using/build-systems.html#using.build-systems.starters
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.
The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.
大まかに分けると三つほどメリットがありそうです。
①設定の簡素化
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors.
つまり、starterは、必要なライブラリのセットをひとつのパッケージとして自動で追加してくれます。
必要なライブラリを一つひとつ手動で探して、dependencies
に追加する手間がなくなり、1行の記述で複数のライブラリをまとめて追加できるため、設定ファイルがシンプルになります。
②一貫性の確保
The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.
つまり、starterが(consistentでsupportedな)互換性のあるバージョンのライブラリをあらかじめ選んでくれるということです。
スターターは、このような互換性の問題を自動的に解決してくれるため、開発者が自分でライブラリのバージョンを管理する手間が省けます。
③開発の迅速化
For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.
つまり、すぐに開発を始められるということです。
通常、データベースアクセス機能を実装するには、複数のライブラリ(Hibernate、データベースドライバなど)を個別に設定する必要があります。しかし、スターターを使うことで、この一連の準備作業が不要になり、すぐにアプリケーションの開発に取り掛かることができます。
おわり
公式リファレンスを読むのは大変ですが、信頼できる情報源なので、少しずつ読み解いていきたいですね...
keycloakとSpringBootを連携した認可をひとまず作成できたので記事を書きたいのですが、(理解しきれていないのと)重くてまとめきれません。頑張ろう。