LoginSignup
7
8

More than 1 year has passed since last update.

Spring Boot 2.5 で spring.datasource.initialization-mode が非推奨となっている件

Last updated at Posted at 2021-05-30

Spring Bootの最新版 Spring Boot 2.5 が本年(2021年)5月21日にリリースされたとのことです。
これに伴い、標題のような警告が出たので、自分の備忘として書いておきます。

症状

Spring Boot のアプリケーションプロパティ(application.properties)に、以下のとおり、データベースに関する記述をしました。

application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/spring_test
spring.datasource.username=yama3
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
spring.datasource.sql-script-encoding=utf-8

すると、STSの画面上で警告(アラート)が表示されました(4つも!)。
2021-05-30 220931.png
5行目の spring.datasource.initialization-mode に関するアラートを見てみると次のとおりでした。
2021-05-30 212747.png
アラートの1行目を確認します。

Property 'spring.datasource.initialization-mode' is Deprecated: Use `spring.sql.init.enabled` instead.

日本語に訳せば、「プロパティ 'spring.datasource.initialization-mode' は非推奨です。代わりに 'spring.sql.init.enabled' を使用してください。」とのことです。
つまり、spring.sql.init.enabledを使いなさい、ということですね。
なお、現時点では非推奨のアラートが表示されるだけなので、特に修正しなくても動くのは動きます。

<参考記事>
spring.datasource.initialize is deprecated

修正後の記載内容

それぞれのアラートに従って、次のように修正しました。

application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/spring_test
spring.datasource.username=yama3
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.sql.init.enabled=true
spring.sql.init.schema-locations=classpath:schema.sql
spring.sql.init.data-locations=classpath:data.sql
spring.sql.init.encoding=utf-8

修正内容を一覧にすると、次のとおりです。

修正前の項目 修正後の項目 備考
spring.datasource.initialization-mode spring.sql.init.enabled DBの初期化の要否を設定(true=実行する、false=実行しない)
spring.datasource.schema spring.sql.init.schema-locations DBの初期スキーマを記述したファイルを指定
spring.datasource.data spring.sql.init.data-locations DBの初期データを記述したファイルを指定
spring.datasource.sql-script-encoding spring.sql.init.encoding 文字コードを指定

spring.sql.init.enabled は true(初期化を実行する) または false(初期化を実行しない)で指定します。
デフォルトが true なので、何も記載しなければ true の設定となります。

以上

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