实战《Spring Boot 2.1.5》-属性迁移工具Migrator

本文介绍了Spring Boot 2.1.5版本升级时的属性迁移工具Migrator,它能帮助分析应用环境并提供诊断。通过添加特定依赖,Migrator可以在运行时迁移已移除或重命名的属性。文章详细讲解了Migrator的功能,包括配置文件的处理,以及如何实现属性的迁移和日志提示。在完成迁移后,需从项目依赖中移除Migrator模块,以避免影响应用启动效率。
摘要由CSDN通过智能技术生成

migrator介绍

以下内容为Spring Boot Reference Guide v2.1.5的内容

When upgrading to a new feature release, some properties may have been renamed or removed. Spring Boot provides a way to analyze your application’s environment and print diagnostics at startup, but also temporarily migrate properties at runtime for you. To enable that feature, add the following dependency to your project:

当升级到新功能版本时,一些配置可能会重命名或者被移除。SpringBoot提供一种方式去分析你应用的环境和在启动时打印诊断内容,还可以在运行时为你临时迁移属性。要启用该特性,添加下方的依赖到你的工程中:

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

Properties that are added late to the environment, such as when using @PropertySource, will not be taken into account.

晚添加到环境中的属性,例如使用@PropertySource时,将不被考虑。

Once you’re done with the migration, please make sure to remove this module from your project’s dependencies.

一旦你完成迁移,请确保在你的项目依赖中移除该模块。

功能介绍

配置文件:application.properties

server.port=8080

# 该属性已经被重命名了
# 参考spring-boot-autoconfigure-2.1.5.RELEASE.jar下META-INF的additional-spring-configuration-metadata.json文件

# {
#  "name": "security.user.name",
#  "type": "java.lang.String",
#  "description": "Default user name.",
#  "defaultValue": "user",
#  "deprecation": {
#    "replacement": "spring.security.user.name",
#    "level": "error"
#  }
# }
    
security.user.name=test

# 该属性已经被重命名了
# 参考spring-boot-2.1.5.RELEASE.jar下META-INF的spring-configuration-metadata.json文件

# {
#   "name": "banner.image.height",
#   "type": "java.lang.Integer",
#   "description": "Banner image height (in chars).",
#   "deprecated": true,
#   "deprecation": {
#     "level": "error",
#     "replacement": "spring.banner.image.height"
#   }
# }
banner.image.height=1

提示内容

当你的配置文件中存在被识别的已经移除的属性时,日志打印以下内容:

2020-02-12 22:06:01.321  WARN 22900 --- [           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.height
		Line: 3
		Replacement: spring.banner.image.height
	Key: security.user.name
		Line: 2
		Replacement: spring.security.user.name


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.

注意:并不是配置中所有的属性重命名之后都会被提示,必须是被识别的,并且是已经移除属性。参考实现原理内容。

实现原理

类PropertiesMigrationListener实现ApplicationListener。对三类事件进行操作,分别是ApplicationPreparedEvent,ApplicationReadyEvent,ApplicationFailedEvent。优先执行事件优先执行

  • ApplicationPreparedEvent

核心代码如下:

private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
   
    
    // 步骤1:加载数据到内存
    ConfigurationMetadataRepository repository = this.loadRepository();
    PropertiesMigrationReporter reporter = new PropertiesMigrationReporter(repository, event.getApplicationContext().getEnvironment());
    
    // 下面那个方法
    this.report = reporter.getReport();
}


// this.report = reporter.getReport();的方法
public PropertiesMigrationReport getReport() {
   
    PropertiesMigrationReport report = new PropertiesMigrationReport();
    
    // 步骤2和步骤3,匹配数据
    Map<String, List<PropertyMigration>> properties = this
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑾析编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值