1. 序章

このチュートリアルでは、Spring Bootのアップグレードを容易にするためにSpringが提供するサポートシステムについて説明します。 特に、 spring-boot-properties-migratorモジュールについて説明します。 アプリケーションのプロパティを移行するのに役立ちます。

Spring Bootのバージョンをアップグレードするたびに、非推奨としてマークされているか、サポートが終了しているか、新しく導入されたプロパティが存在する可能性があります。 Springは、アップグレードごとに包括的な変更ログを公開しています。 ただし、これらの変更ログを確認するのは少し面倒です。 そこで、 spring-boot-properties-migratorモジュールが役に立ちます。 それは私たちのセットアップのために私たちに個人化された情報を提供することによってそうします。

これを実際に見てみましょう!

2. デモアプリケーション

Spring Bootアプリケーションをバージョン2.3.0から2.6.3にアップグレードしましょう。

2.1. プロパティ

デモアプリケーションには、2つのプロパティファイルがあります。 デフォルトのプロパティファイルapplication.propertiesに、以下を追加しましょう。

spring.resources.cache.period=31536000
spring.resources.chain.compressed=false
spring.resources.chain.html-application-cache=false

devプロファイルYAMLファイルapplication-dev.yamlの場合:

spring:
  resources:
    cache:
      period: 31536000
    chain:
      compressed: true
      html-application-cache: true

プロパティファイルには、SpringBoot2.3.0と2.6.3の間で置き換えまたは削除されたいくつかのプロパティが含まれています。 さらに、より良いデモンストレーションのために、.propertiesファイルと.yamlファイルの両方もあります。

2.2. 依存関係の追加

まず、モジュールの依存関係として spring-boot-properties-migratorを追加しましょう。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-properties-migrator</artifactId>
    <scope>runtime</scope>
</dependency>

Gradleを使用している場合は、次を追加できます。

runtime("org.springframework.boot:spring-boot-properties-migrator")

依存関係のスコープはruntimeである必要があります。

3. スキャンの実行

次に、アプリケーションをパッケージ化して実行しましょう。 Mavenを使用してビルドとパッケージ化を行います。

mvn clean package

最後に、それを実行しましょう:

java -jar target/spring-boot-properties-migrator-demo-1.0-SNAPSHOT.jar

その結果、 spring-boot-properties-migrator モジュールは、アプリケーションのプロパティファイルをスキャンし、その魔法を働かせます。 これについてはもう少し詳しく説明します。

4. スキャン出力を理解する

ログを調べて、スキャンの推奨事項を理解しましょう。

4.1. 交換可能なプロパティ

既知の置換があるプロパティについては、PropertiesMigrationListenerクラスからの警告ログが表示されます。

WARN 34777 --- [           main] o.s.b.c.p.m.PropertiesMigrationListener  : 
The use of configuration keys that have been renamed was found in the environment:

Property source 'Config resource 'class path resource [application.properties]' via location 'optional:classpath:/'':
	Key: spring.resources.cache.period
		Line: 2
		Replacement: spring.web.resources.cache.period
	Key: spring.resources.chain.compressed
		Line: 3
		Replacement: spring.web.resources.chain.compressed

Property source 'Config resource 'class path resource [application-dev.yaml]' via location 'optional:classpath:/'':
	Key: spring.resources.cache.period
		Line: 5
		Replacement: spring.web.resources.cache.period
	Key: spring.resources.chain.compressed
		Line: 7
		Replacement: spring.web.resources.chain.compressed


Each configuration key has been temporarily mapped to its replacement for your convenience. To silence this warning, please update your configuration to use the new keys.

ログには、 各エントリに関連するプロパティファイル、キー、行番号、置換キーなどのすべてのキー情報が表示されます。 これにより、このようなすべてのプロパティを簡単に識別して置き換えることができます。 さらに、モジュールはこれらのプロパティを実行時に利用可能な置換に置き換えます。これにより、変更を加えることなくアプリケーションを実行できるようになります。

4.2. サポートされていないプロパティ

既知の置換がないプロパティについては、PropertiesMigrationListenerクラスからのエラーログが表示されます。

ERROR 34777 --- [           main] o.s.b.c.p.m.PropertiesMigrationListener  : 
The use of configuration keys that are no longer supported was found in the environment:

Property source 'Config resource 'class path resource [application.properties]' via location 'optional:classpath:/'':
	Key: spring.resources.chain.html-application-cache
		Line: 4
		Reason: none

Property source 'Config resource 'class path resource [application-dev.yaml]' via location 'optional:classpath:/'':
	Key: spring.resources.chain.html-application-cache
		Line: 8
		Reason: none

前のシナリオと同様に、 問題のあるプロパティファイル、キー、プロパティファイルの行番号、およびキーが削除された理由が表示されます。 ただし、前のシナリオとは異なり、問題のプロパティによっては、アプリケーションの起動が失敗する場合があります。 これらのプロパティは自動的に移行できなかったため、ランタイムの問題が発生する可能性もあります。

5. 構成プロパティの更新

これで、スキャンによって提供された重要な情報により、プロパティをアップグレードするためのより良い状態になりました。 移動するプロパティファイル、行番号、および提案された置換で置き換えるキー、または置換されていない特定のキーのリリースノートを参照してください。

プロパティファイルを修正しましょう。 デフォルトのプロパティファイルapplication.propertiesで、推奨事項に従ってプロパティを置き換えましょう。

spring.web.resources.cache.period=31536000
spring.web.resources.chain.compressed=false

同様に、devプロファイルYAMLファイルapplication-dev.yamlを更新しましょう。

spring:
  web:
    resources:
      cache:
        period: 31536000
      chain:
        compressed: false

要約すると、プロパティ spring.resources.cache.periodspring.web.resources.cache.periodおよびspringに置き換えました。 resources.chain.compressedspring.web.resources.chain.compressed spring.resources.chain.html-application-cache キーは、新しいバージョンではサポートされなくなりました。 したがって、この場合は削除しました。

もう一度スキャンを実行してみましょう。 まず、アプリケーションを作成しましょう。

mvn clean package

次に、それを実行しましょう:

java -jar target/spring-boot-properties-migrator-demo-1.0-SNAPSHOT.jar

これで、 PropertiesMigrationListener クラスから以前に表示されたすべての情報ログが消え、プロパティの移行が成功したことが示されます。

6. これはどのように機能しますか? フードの下のピーク

Spring Boot Module JARには、META-INFフォルダーにspring-configuration-metadata.jsonファイルが含まれています。 これらのJSONファイルは、spring-boot-properties-migratorモジュールの情報のソースです。 プロパティファイルをスキャンするときに、これらのJSONファイルから関連するプロパティのメタデータ情報を取得してスキャンレポートを作成します。

ファイルの例は、生成されたレポートに表示されるさまざまな情報を示しています。

spring -autoconfigure:2.6.3.jar META-INF / spring -configuration-metadata.json に、springのエントリがあります。 ] .resources.cache.period

{
    "name": "spring.resources.cache.period",
    "type": "java.time.Duration",
    "deprecated": true,
    "deprecation": {
        "level": "error",
        "replacement": "spring.web.resources.cache.period"
    }
}

同様に、 spring -boot:2.0.0.RELEASE.jar META-INF / spring -configuration-metadata.json には、のエントリがあります。 X132X] vendor.image.location :

{
    "defaultValue": "banner.gif",
    "deprecated": true,
    "name": "banner.image.location",
    "description": "Banner image file location (jpg\/png can also be used).",
    "type": "org.springframework.core.io.Resource",
    "deprecation": {
        "level": "error",
        "replacement": "spring.banner.image.location"
    }
}

7. 警告

この記事を締めくくる前に、 spring-boot-properties-migratorでいくつかの警告を確認しましょう。

7.1. 本番環境でこの依存関係を維持しないでください

このモジュールは、開発環境でのアップグレード中にのみ使用することを目的としています。 更新または削除するプロパティを特定して修正したら、依存関係からモジュールを削除できます。 最終的には、このモジュールをより高い環境に含めるべきではありません。 それに関連する特定のコストのため、推奨されません

7.2. 歴史的財産

モジュールは、はるかに古いバージョンで廃止された本当に古いプロパティを検出できない可能性があるため、アップグレード中にバージョンをジャンプしないでください。 たとえば、 vendor.image.locationapplication.propertiesファイルに追加しましょう:

banner.image.location="myBanner.txt"

このプロパティは、Spring Boot2.0非推奨になりました。 Spring Bootバージョン2.6.3でアプリケーションを直接実行しようとしても、警告やエラーメッセージは表示されません。 このプロパティを検出できるようにするには、Spring Boot2.0でスキャンを実行する必要があります。

WARN 25015 --- [           main] o.s.b.c.p.m.PropertiesMigrationListener  : 
The use of configuration keys that have been renamed was found in the environment:

Property source 'applicationConfig: [classpath:/application.properties]':
    Key: banner.image.location
	Line: 5
	Replacement: spring.banner.image.location


Each configuration key has been temporarily mapped to its replacement for your convenience. To silence this warning, please update your configuration to use the new keys.

8. 結論

この記事では、spring-boot-properties-migratorについて説明しました。 これは、プロパティファイルをスキャンし、簡単に実行できるスキャンレポートを提供する便利なツールです。 また、モジュールがどのようにその偉業を達成するかについての概要も調べました。 最後に、このツールを最大限に活用するために、いくつかの注意事項を確認しました。

いつものように、コードはGitHubから入手できます。