5
4

More than 3 years have passed since last update.

【初心者向け】Gradleを使ってSpring BootでWebアプリを作るならとりあえず追加したほうがよい依存関係の設定

Last updated at Posted at 2021-08-27

はじめに

Spring初心者がGradleを使ってWebアプリを作ろうとしたとき、
依存関係のエラーがなかなか解決せず試行錯誤した結果、
結局のところGradleのビルド定義ファイル(build.gradle)の依存関係定義の設定漏れだった、ということがよくあります。(体験談)
(プロジェクトの新規作成時に必要な依存関係を追加していればよいのですが、初心者はすっ飛ばしてしまいます。その場合、後から手動で依存関係を追加します。)

そこで、初心者がSpring Bootアプリを作るならまずは追加しておくべき
Gradleのビルド定義ファイルの依存関係をまとめました。

何も考えずにプロジェクトを作成してしまった・・・

何も考えずにSpring スタータープロジェクトで新規作成した場合、
build.gradleは以下のようになっています。

plugins {
    id 'org.springframework.boot' version '2.5.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

この状態で色々なサイトで参考にした情報をもとにソースコードを書いていくと、
依存関係のエラーになることが多々あります。

build.gradleに依存関係の定義を追加する

ここで、以下のように依存関係の定義を追加します。

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-validation')
    implementation('org.springframework.boot:spring-boot-starter-thymeleaf')
    implementation('org.springframework.boot:spring-boot-starter-jdbc')
    implementation('com.h2database:h2')
    implementation('org.springframework.boot:spring-boot-devtools')

}

これで最低限、Spring BootでthymeleafやDBを使ったWebアプリを作ることができます。

各設定の補足説明

spring-boot-starter-web

Spring BootでWebアプリケーションを作るのに必須。

spring-boot-starter-validation

Spring Bootで@Size@NotNullなどのアノテーションを用いたバリデーション機能を使うのに必要。

参考:https://qiita.com/tatetsujitomorrow/items/a397c311a95d66e4f955

spring-boot-starter-thymeleaf

「Thymeleaf」はSpring Bootで標準的に使われるテンプレートエンジンの1つ。
普通のHTMLファイルのタグに記述を追加していくだけで使える。
→学習コストが低い、デザイナーとの協業が容易といったメリットがある。

公式サイト:https://www.thymeleaf.org/
引用記事:https://it-kyujin.jp/article/detail/169/

spring-boot-starter-jdbc

Spring BootでJDBCを使ってRDBにアクセスするために必要。
ライブラリが持つメソッドに引数としてSQL文を渡すことで、DBの検索や更新を行うことが可能。
Spring は、JdbcTemplate と呼ばれるテンプレートクラスを提供し、
SQL リレーショナルデータベースと JDBC を簡単に操作できるようにします。

参考:https://spring.pleiades.io/guides/gs/relational-data-access/
   https://spring.pleiades.io/spring-framework/docs/current/reference/html/data-access.html#jdbc-JdbcTemplate

com.h2database:h2

Spring Bootで「H2 Database Engine」を使うために必要。
H2 Database Engineは、Javaで実装されたRDBの1つ。
インストールの必要がないので、手軽に使える。
DBが必要なWebアプリを作るときにDB周りのテストをしたいときに便利。

参考:https://zenn.dev/developma/articles/548b9652b01131

spring-boot-devtools

Spring Bootで「Spring Boot DevTools」を使うために必要。
プロジェクトを停止→再起動することなく、ソースコードを反映させることができるため、
ソースコードの修正と動作確認を繰り返し行いながら開発を進めたいときに便利。

参考:https://qiita.com/IsaoTakahashi/items/f99d5f761d1d4190860d

5
4
1

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
5
4