LoginSignup
25
22

More than 3 years have passed since last update.

Spring BootのHot Swapping設定

Last updated at Posted at 2019-05-24

最近Spring Bootに入門しまして、Hot Swappingを設定しようとして調べていたら、記事によって若干情報が古かったり設定が足りなかったりして意外と苦戦したので、ここにまとめておきます。

環境情報

  • Mac
  • IntelliJ IDEA 2019.1.2 (Community Edition)
  • Spring Boot Gradle Plugin 2.1.4
  • Spring Boot Devtools 2.1.4

設定手順

PreferencesでBuild project automaticallyにチェックを入れます。

image.png

Shift+Command+Aでコマンド検索ウィンドウを表示し、Registry...を実行します。

image.png

compiler.automake.allow.when.app.runningにチェックを入れます。

image.png

build.gradleで以下の設定を追加します。

bootRun {
    sourceResources sourceSets.main
}

application.propertiesに以下を追加します(テンプレートエンジンにThymeleafを使用している場合)。

spring.thymeleaf.cache = false

リロードの動きについて

例えば、src/main/java/hello/GreetingController.javaのソースに変更を加えると、アプリケーションがリロードされます。
自分の環境ではリロードが走るにはソースファイルをCommand+Sで保存してあげる必要がありました。

...
サービスがシャットダウンされた
2019-05-24 16:18:26.310  INFO 37740 --- [      Thread-33] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

アプリが再起動した
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-05-24 16:18:31.397  INFO 37740 --- [  restartedMain] hello.Application                        : Starting Application on xxx.local with PID 37740 (/Users/takehiro/Documents/git/sprint-web-mvc/build/classes/java/main started by takehiro in /Users/user/Documents/git/sprint-web-mvc)
2019-05-24 16:18:31.398  INFO 37740 --- [  restartedMain] hello.Application                        : No active profile set, falling back to default profiles: default
2019-05-24 16:18:31.563  INFO 37740 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
...

また、静的ファイルであるsrc/main/resources/templates/greeting.htmlに変更を加えたときは、ブラウザをリロードすることで反映されました。
こちらの場合アプリケーションのリロードは発生しませんでした。

ライブリロード

ブラウザのLiveReloadエクステンションと組み合わせることで、いちいちブラウザのリロードボタンをクリックしなくても自動でリロードをさせることができるようになります。
これはSpring Boot DevtoolsにLiveReloadサーバーが組み込まれているためです。

エクステンションはこちらからインストールできます。

先にアプリケーションを起動しておき、ブラウザで画面を表示します。
LiveReloadエクステンションのアイコンをクリックし、ステータスがLiveReload is connected, click to disableになっていることを確認します。

image.png

これでライブリロードが実現されます。

livereload.gif

参考

25
22
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
25
22