0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

あえてSpringBootを最小構成で使ってみる

Posted at

SpringBootあるある

勝手にいろんなことやってくれるので、よくわからんまま使いがち

前提

SpringBootはgradleなどで
spring-boot-starter-xxx みたいな便利パッケージを使うのが基本だが
「これどのパッケージの機能? spring-boot-starter-web? spring-boot-starter-mvc?」など役割分担がよくわからない

→最小構成で何ができるのか調べてみる

  • SpringBoot3.4.4
  • SpringInitializerから dependencies を何も選ばずにプロジェクトを作成

こうなった

build.gradle
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

すくなーい!
spring-boot-starterは単体で使うことはほぼない。通常は spring-boot-starter-web や spring-boot-starter-data-jpa のような 機能特化型スターターを使うのが一般的

ホントの最小構成はimplementation 'org.springframework.boot:spring-boot-starter'だけのはずだけど、SpringInitializerにとりあえず従っておく

@SpringBootApplication
public class PureApplication {
	public static void main(String[] args) {
		SpringApplication.run(PureApplication.class, args);
	}
}

いつもの
普通にデバッグ実行動きますね
依存が少ないから起動が爆速!!

AutoConfiguration

@SpringBootApplicationを付けると、Spring Bootは大量の@Configurationクラスを条件付きで読み込む
本来手動で仕込まなきゃいけない設定を自動でやってくれるイメージ

Spring側で作成済みのJavaConfigクラスの中から、必要なものだけを選別して読み込んでくれる

  • 必要なクラスがクラスパス上にあるか
  • プロパティが指定されているか
    などから判断

以下の設定からデバッグ実行でAutoConfigurationの判定が見れる

applicaiton.yml
debug: true

起動ログ

   JmxAutoConfiguration matched:
      Matched:
         - @ConditionalOnClass found required class 'org.springframework.jmx.export.MBeanExporter' (OnClassCondition)
         - ...

   WebMvcAutoConfiguration did not match:
      - @ConditionalOnClass did not find class 'javax.servlet.Servlet' (OnClassCondition)
      - ...

matched:この構成クラスは有効になった(→Bean登録される)
did not match:この構成クラスは無効(→今回のアプリには不要と判断された)

依存ライブラリを調べる

gradleのライブラリ

./gradlew dependencies
compileClasspath - Compile classpath for source set 'main'.
\--- org.springframework.boot:spring-boot-starter -> 3.4.4
     +--- org.springframework.boot:spring-boot:3.4.4
     |    +--- org.springframework:spring-core:6.2.5
     |    |    \--- org.springframework:spring-jcl:6.2.5
     |    \--- org.springframework:spring-context:6.2.5
     |         +--- org.springframework:spring-aop:6.2.5
     |         |    +--- org.springframework:spring-beans:6.2.5
     |         |    |    \--- org.springframework:spring-core:6.2.5 (*)
     |         |    \--- org.springframework:spring-core:6.2.5 (*)
     |         +--- org.springframework:spring-beans:6.2.5 (*)
     |         +--- org.springframework:spring-core:6.2.5 (*)
     |         +--- org.springframework:spring-expression:6.2.5
     |         |    \--- org.springframework:spring-core:6.2.5 (*)
     |         \--- io.micrometer:micrometer-observation:1.14.5
     |              \--- io.micrometer:micrometer-commons:1.14.5
     +--- org.springframework.boot:spring-boot-autoconfigure:3.4.4
     |    \--- org.springframework.boot:spring-boot:3.4.4 (*)
     +--- org.springframework.boot:spring-boot-starter-logging:3.4.4
     |    +--- ch.qos.logback:logback-classic:1.5.18
     |    |    +--- ch.qos.logback:logback-core:1.5.18
     |    |    \--- org.slf4j:slf4j-api:2.0.17
     |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.24.3
     |    |    +--- org.apache.logging.log4j:log4j-api:2.24.3
     |    |    \--- org.slf4j:slf4j-api:2.0.16 -> 2.0.17
     |    \--- org.slf4j:jul-to-slf4j:2.0.17
     |         \--- org.slf4j:slf4j-api:2.0.17
     +--- jakarta.annotation:jakarta.annotation-api:2.1.1
     +--- org.springframework:spring-core:6.2.5 (*)
     \--- org.yaml:snakeyaml:2.3

compileClasspathだけでこの量
最小構成とはいえいっぱいですね

この状態でできること

  • DIコンテナの利用
  • propertiesの読み込み
  • ログ出力
  • プロファイルの切り替え

Springのコア!って感じの機能は使える

この状態でできないこと

  • Webアプリ(MVC)、REST API
    • spring-boot-starter-webが必要
  • DB接続
    • spring-boot-starter-jdbc などが必要
  • Thymeleaf テンプレート
    • spring-boot-starter-thymeleaf

アプリ作るぞー!ってことはできない

参考文献

プロになるためのSpring入門 ――ゼロからの開発力養成講座

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?