gpt4 book ai didi

spring-boot - 使用带有驼峰式前缀的 ConfigurationProperties 时,如何解决 "Prefix must be in canonical form"?

转载 作者:行者123 更新时间:2023-12-04 13:08:42 43 4
gpt4 key购买 nike

我的 yml 中有一个属性配置为

foobar:
baz: 7
和一个带有注释的配置类
@ConfigurationProperties(prefix = "foobar")
一切正常。
我所在组织的代码一般是驼峰式大小写,所以我将属性和前缀都重命名为 fooBar . IntelliJ 现在突出显示 prefix = "foobar"与错误“前缀必须采用规范形式”一致。在 yml 配置中保留camelCasing 的同时我能做什么?

最佳答案

Spring Boot 支持多种格式的属性名称,但鼓励您以规范的方式访问它们。
Property Binding in Spring Boot 2.0 :

It turns out the idea of relaxed property names is much easier to implement if you restrict it to one direction. You should always access properties in code using a canonical form, regardless of how they are represented in the underlying source.

The ConfigurationPropertyName class enforces these canonical naming rules, which basically boil down to “use lowercase kebab-case names”.

So, for example, you should refer to a property in code as person.first-name even if person.firstName or PERSON_FIRSTNAME is used in the underlying source.


您可以将配置 yml 保留在 Camel 案例中:
fooBar:
baz: 7
但是在配置类注解中更改访问以使用kebab-case:
@ConfigurationProperties(prefix = "foo-bar")

关于spring-boot - 使用带有驼峰式前缀的 ConfigurationProperties 时,如何解决 "Prefix must be in canonical form"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67994420/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com