LoginSignup
38
45

More than 5 years have passed since last update.

Spring Boot 1.5.10 → Spring Boot 2.0.0 にしたときの覚書

Last updated at Posted at 2018-03-16

はじめに

製作中のWEBアプリがSpringBoot 1.5.10だったのですが、2.0.0がリリースされたので新しくしちゃおう。
そんな時にやったこととかの覚書です。
注意)製作中のWEBアプリで必要になったことだけ記載しています。

  • Spring Web MVC
  • Spring Security
  • Thymeleaf
  • Gradle

(4/18 追記)2.0.0で利用しているSpring Frameworkのバージョン5.0.4に脆弱性が見つかったため対応バージョンの5.0.5になる2.0.1にアップしています。
詳しくは( https://www.jpcert.or.jp/at/2018/at180014.html

ざっくりとした変更

Spring4 → Spring5

Spring5の変更内容は下記記事がとても参考になりました。
参考)Spring Framework 5.0 主な変更点の概要

Java8以上必須

Javaはもともと8だったので影響がありませんでした。

Thymeleaf2 → Thymeleaf3

私の作っていたアプリで一番影響があったところかもしれません。

具体的にやったことについて

とりあえず、gradleのSpringBootのバージョンを2.0.0にして、何も修正せずワイルド:smirk:にビルドしてみた。
速攻でエラーが出たので、ちゃんと手順を見て順番に解決していく。

よい子の皆さんは、SpringBoot2.0のマイグレートガイドをよく読んで、部屋を明るくして、離れてみてください。

1. gradlewのバージョンアップ

4.x以上が必要になりました。
なぜか、2.xのgradlewになっていたので当然そこでコケます。(犯人は私)

wrapperタスクのバージョンを最新にします。

build.gradle

task wrapper(type: Wrapper) {
    gradleVersion = '4.6' //バージョンを4.x以上に設定
}

build.gradleにwrapperタスクを追加し、コマンドgradle wrapperを実行して、バージョン4.x以上のgradlewにしておきます。
これで、gradlewをつかって、SpringBoot2.0.0のアプリケーションがビルドできるようになります。

参考)Spring-Boot-2.0-Release-Notes#gradle-plugin

2. spring-boot-gradle-pluginの仕様変更

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management' // <-- add this to your build.gradle

バージョンアップに伴って、依存関係管理プラグインが自動で適用されなくなったようです。
明示的に読み込む必要があるので、指示に従って1行追加します。

参考)Spring-Boot-2.0-Migration-Guide#spring-boot-gradle-plugin

3. application.yml(propeties)の仕様変更

spring-boot-properties-migratorというチェックツールがあるようす。

一時的にbuild.gradleの依存関係に追加。

gradle buildして、SpringBootApplicationを起動する。
すると、コンソール上に見慣れないメッセージが出現。
名称が変わったプロパティや、なくなってしまったものなどを教えてくれます。

参考)Spring-Boot-2.0-Migration-Guide#before-you-start

4. hibernate validator

hibernate validatorのNotEmptyアノテーションが非推奨になっていました。
使っていなかったので影響なかったですがNotEmpty以外に、NotBlankやEmail、ModCheckなどのアノテーションも非推奨になっている様子。

use the standard NotEmpty constraint instead

とあるので、そのように修正しました。

参考)Hibernate Validator - Deprecated API

5. SpringMvcのJavaConfig

WebMvcConfigurerAdapterが非推奨になっていました。

参考)Spring-javadoc WebMvcConfigurerAdapter.java

as of 5.0 WebMvcConfigurer has default methods (made possible by a Java 8 baseline) and can be implemented directly without the need for this adapter.

WebMvcConfigurerインターフェースがdefault methodを持つようになったのでadapterはもういらないよと書かれています。

変更前のWebMvcConfig.java

変更前のWebMvcConfig.java
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter
{
    // 略
}

変更後のWebMvcConfig.java

変更後のWebMvcConfig.java
@Configuration
public class WebMvcConfig implements WebMvcConfigurer
{
    // 略
}

6. SpringSecurity

参考)https://docs.spring.io/spring-security/site/docs/5.0.3.RELEASE/reference/htmlsingle/
参考)https://spring.io/blog/2017/09/15/security-changes-in-spring-boot-2-0-m4
TODO: 内容まとめる…かもしれない。

7. Thymeleafの色々

JavaConfig

作ってなかったので影響なしでしたが作っている方は変更があるようなのでご注意ください。

下記マイグレーションガイドの2.Configuration changesの項に詳しく記載があります。
参考)Thymeleaf 3 ten-minute migration guide

Dialect

ここはがっつり変更されていて古いものはエラーざんまい:open_hands_tone1:でした。
改行コードを<br/>タグにするみたいなDialectを作っていたので下記を参考に作り直ししました。

参考)Say Hello! Extending Thymeleaf in 5 minutes
参考)Say Hello Again! Extending Thymeleaf even more in another 5 minutes

TemplateMode

Thymeleaf2のときは、パーサーがXMLベースでチェックが非常にきびしぃため、TemplateMode.LEGACYHTML5の設定にしたうえで、HTMLパーサーとしてnekoHTMLを利用していましたが、Thymeleaf3ではTemplateMode.HTMLを利用することが推奨されているようなので変更しました。(TemplateMode.LEGACYHTML5は非推奨になっていました。)
バイバイ:cat:

The second difference is that the template mode has a value of TemplateMode.HTML. Template modes are not strings anymore and the possible values are a bit different from Thymeleaf 2. We will discuss it in a minute.

参考)Thymeleaf 3 ten-minute migration guide

th:inline構文の削除

エラーになるわけではありませんが、マイグレーションガイドに下記の通り記載がありました。
今回th:inlineは利用していなかったので修正対象ではありませんでしたが、うっかり使わないようにきをつける。

The only change we recommend doing to your templates is removing any th:inline="text" attributes you might have, because they are not needed anymore in order to have output inlined expressions in HTML or XML templates. And it’s just a recommendation — templates will work anyway. But you will benefit from some extra processing performance if you remove those.

修正が推奨されています。

Thymeleaf2

<tr th:inline="text" th:each="user : ${users}">
    <td>[[$(user.id)]]</td>
    <td>[[$(user.name)]]</td>
</tr>

参考)Thymeleaf2 - 12.インライン処理

Thymeleaf3

<tr th:each="user : ${users}">
    <td>[[$(user.id)]]</td>
    <td>[[$(user.name)]]</td>
</tr>

参考)Thymeleaf3 - 12.Inlining
※ 日本語ドキュメントが2系の説明のままっぽかったので、英語ドキュメントにリンクしました。

8. コネクションプール(追記)

デフォルトのコネクションプールがTomcat JDBC Connection PoolからHikariCPになっていました。
実装の違いによって影響を受けるソースが移行した時点ではなかったので漏れていましたが追記します。

Migration-GuideにはこれまでHikariCPを使うために依存関係に追記をしていた人はその記述が不要になったよと書かれていました。

参考)Spring-Boot-2.0-Migration-Guide - working-with-sql-databases

SpringBootのドキュメントにも、このように書かれておりました。

Production database connections can also be auto-configured by using a pooling DataSource. Spring Boot uses the following algorithm for choosing a specific implementation:
1.We prefer HikariCP for its performance and concurrency. If HikariCP is available, we always choose it.
2.Otherwise, if the Tomcat pooling DataSource is available, we use it.
3.If neither HikariCP nor the Tomcat pooling datasource are available and if Commons DBCP2 is available, we use it.

参考)Spring-Boot Documents - boot-features-connect-to-production-database

spring.datasource.tomcat.max-wait=10000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.test-on-borrow=true

こんな感じのプレフィックス付きプロパティの定義をしていた場合は影響があるのかな。
HikariCPの設定はspring.datasource.hikari.* だそうで。

おわり

38
45
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
38
45