Springboot-在启动时验证“本机”属性

时间:2020-07-17 14:38:13

标签: spring spring-boot

上下文:在最近的springboot / spring迁移之后,我一直在想为什么现在将HAL URL作为HTTP(而不是HTTPS)返回。

经过调查,这与某些已弃用(并删除)的属性替换为另一个属性有关(好的,Intellij做得很好,并强调了这一点)

server.use-forward-headers=true
#replaced by
server.forward-headers-strategy=native

我知道您可以通过@ConfigurationProperties,其参数甚至您自己的JSR注释来验证自己的属性集,但是有一种方法可以验证“本机”属性(server。,spring。 / em>,...)在应用程序启动和/或构建时?

  • UC1:我想确保我没有使用任何未知的属性(例如,该属性已被完全删除或拼写错误)
  • UC2:我想确保我没有使用任何不赞成使用的属性

尝试的方法:

  • 搜索可以完成这项工作的现有财产
  • 覆盖ServerProperties(听起来像个坏主意)
  • 使用现有的FailureAnalyzer或定义自定义的

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您可以依靠您的IDE,但是它只能向您显示您正在管理的配置文件。这些属性可能在特定环境中或使用您无权访问的远程配置服务器设置为环境变量。

这是我们提供spring-boot-properties-migrator的原因之一。 release notes中引用了它。

以下是示例demo app,其产生了以下内容:

2020-07-18 19:19:54.168  INFO 69898 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on taurus.lan with PID 69898 (/Users/snicoll/Downloads/demo-properties-migrator/target/classes started by snicoll in /Users/snicoll/Downloads/demo-properties-migrator)
2020-07-18 19:19:54.170  INFO 69898 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-07-18 19:19:54.545  INFO 69898 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.571 seconds (JVM running for 0.924)
2020-07-18 19:19:54.551 ERROR 69898 --- [           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 'systemProperties':
    Key: server.connection-timeout
        Reason: Each server behaves differently.

Property source 'applicationConfig: [classpath:/application.properties]':
    Key: server.use-forward-headers
        Line: 1
        Reason: Replaced to support additional strategies.


Please refer to the release notes or reference guide for potential alternatives.

这将适用于已知属性(您的用例),但不适用于已设置且我们不知道的属性(即没有元数据的属性)。当然,该类型中的一个明显候选者是错别字,但这不再是升级问题。依靠您的IDE来确保您介绍的属性合法,这对我来说是合理的。

我们还想为在没有帮助的情况下添加属性的用户提供一些验证,有关更多详细信息,请参见this issue