1. 序章

Spring Bootを使用すると、サービスをすばやくセットアップして実行できます。

開発エクスペリエンスをさらに強化するために、SpringはSpringBoot-1.3の一部としてspring-boot-devtoolsツールをリリースしました。 この記事では、新しい機能を使用して達成できるメリットについて説明します。

次のトピックについて説明します。

  • プロパティのデフォルト
  • 自動再起動
  • ライブリロード
  • 全体設定
  • リモートアプリケーション

1.1. プロジェクトにSpring-Boot-Devtoolsを追加する

プロジェクトにspring-boot-devtoolsを追加するのは、他のspring-bootモジュールを追加するのと同じくらい簡単です。 既存のスプリングブートプロジェクトで、次の依存関係を追加します。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

プロジェクトのクリーンビルドを実行すると、spring-boot-devtoolsと統合されます。 最新バージョンはここから取得でき、すべてのバージョンはここにあります。

2. プロパティのデフォルト

Spring-bootは、パフォーマンスを向上させるためにデフォルトでキャッシュを有効にするなど、多くの自動構成を実行します。 そのような例の1つは、テンプレートエンジンで使用されるテンプレートのキャッシュです。 thymeleaf。 ただし、開発中は、変更をできるだけ早く確認することがより重要です。

application.propertiesファイルのプロパティspring.thymeleaf.cache= false を使用して、thymeleafのキャッシュのデフォルトの動作を無効にすることができます。 これを手動で行う必要はありません。このspring-boot-devtoolsを導入すると、これが自動的に行われます。

3. 自動再起動

通常のアプリケーション開発環境では、開発者はいくつかの変更を加え、プロジェクトをビルドし、アプリケーションをデプロイ/開始して新しい変更を有効にするか、JRebelなどを活用しようとします。

spring-boot-devtools、を使用すると、このプロセスも自動化されます。 クラスパスでファイルが変更されるたびに、spring-boot-devtoolsを使用するアプリケーションによってアプリケーションが再起動します。 この機能の利点は、加えられた変更が大幅に短縮されたことを確認するために必要な時間が短縮されることです。

19:45:44.804 ... - Included patterns for restart : []
19:45:44.809 ... - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
19:45:44.810 ... - Matching URLs for reloading : [file:/.../target/test-classes/, file:/.../target/classes/]

 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:45:45.174  ...: Starting Application on machine with PID 7724 (<some path>\target\classes started by user in <project name>)
2017-03-12 19:45:45.175  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:45:45.510  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy

ログに示されているように、アプリケーションを生成したスレッドは main ではなく、restartedMainスレッドです。 プロジェクトに加えられた変更は、Javaファイルの変更であっても、プロジェクトの自動再起動を引き起こします。

2017-03-12 19:53:46.204  ...: Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy
2017-03-12 19:53:46.208  ...: Unregistering JMX-exposed beans on shutdown


 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:53:46.587  ...: Starting Application on machine with PID 7724 (<project path>\target\classes started by user in <project name>)
2017-03-12 19:53:46.588  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:53:46.591  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@acaf4a1: startup date [Sun Mar 12 19:53:46 IST 2017]; root of context hierarchy

4. ライブリロード

spring-boot-devtools モジュールには、リソースが変更されたときにブラウザーの更新をトリガーするために使用される組み込みのLiveReloadサーバーが含まれています。

これをブラウザで行うには、LiveReloadプラグインをインストールする必要があります。そのような実装の1つは、Chrome用の Remote LiveReloadです。

5. 全体設定

spring-boot-devtools は、どのアプリケーションとも結合されていないグローバル設定を構成する方法を提供します。 このファイルの名前は.spring-boot-devtools.propertiesで、$HOMEにあります。

6. リモートアプリケーション

6.1. HTTPを介したリモートデバッグ(リモートデバッグトンネル)

spring-boot-devtools は、HTTPを介してすぐに使用できるリモートデバッグ機能を提供します。この機能を使用するには、spring-boot-devtoolsをアプリケーションの一部としてパッケージ化する必要があります。 これは、MavenのプラグインでexcludeDevtools構成を無効にすることで実現できます。

簡単なサンプルを次に示します。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>false</excludeDevtools>
            </configuration>
        </plugin>
    </plugins>
</build>

HTTPを介したリモートデバッグを機能させるには、次の手順を実行する必要があります。

  1. サーバーにデプロイして起動するアプリケーションは、リモートデバッグを有効にして起動する必要があります。
    -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n

    ご覧のとおり、リモートデバッグポートについてはここでは説明していません。 したがって、Javaはランダムなポートを選択します

  2. 同じプロジェクトで、起動構成を開き、次のオプションを選択します。メインクラスを選択します。 org.springframework.boot.devtools.RemoteSpringApplication プログラム引数に、アプリケーションのURLを追加します。 、例えば http:// localhost:8080
  3. spring-bootアプリケーションを介したデバッガーのデフォルトポートは8000であり、次の方法でオーバーライドできます。
    spring.devtools.remote.debug.local-port=8010
  4. 次に、リモートデバッグ構成を作成し、プロパティを介して構成された 8010 として、またはデフォルトに固執する場合は8000としてポートを設定します。

ログは次のようになります。

  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote ::  (v1.5.2.RELEASE)

2017-03-12 22:24:11.089  ...: Starting RemoteSpringApplication v1.5.2.RELEASE on machine with PID 10476 (..\org\springframework\boot\spring-boot-devtools\1.5.2.RELEASE\spring-boot-devtools-1.5.2.RELEASE.jar started by user in project)
2017-03-12 22:24:11.097  ...: No active profile set, falling back to default profiles: default
2017-03-12 22:24:11.357  ...: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Sun Mar 12 22:24:11 IST 2017]; root of context hierarchy
2017-03-12 22:24:11.869  ...: The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2017-03-12 22:24:11.949  ...: LiveReload server is running on port 35729
2017-03-12 22:24:11.983  ...: Started RemoteSpringApplication in 1.24 seconds (JVM running for 1.802)
2017-03-12 22:24:34.324  ...: Remote debug connection opened

6.2. リモートアップデート

リモートクライアントは、リモート再起動機能の場合と同様に、アプリケーションのクラスパスの変更を監視します。 クラスパスを変更すると、更新されたリソースがリモートアプリケーションにプッシュされ、再起動がトリガーされます。

変更されたファイルの監視はその場合にのみ可能であるため、リモートクライアントが稼働しているときに変更がプッシュされます。

ログでは次のようになります。

2017-03-12 22:33:11.613  INFO 1484 ...: Remote debug connection opened
2017-03-12 22:33:21.869  INFO 1484 ...: Uploaded 1 class resource

7. 結論

この簡単な記事では、 spring-boot-devtools モジュールを活用して、多くのアクティビティを自動化することにより、開発者のエクスペリエンスを向上させ、開発時間を短縮する方法を示しました。