如何在Spring中使用@ConfigurationProperties定义属性名称?



我有class:

@ConfigurationProperties(prefix = "user")
public class BarcodeConfig {
private String customName;
private Details customDetails;

和属性文件:

user.name=sampleName
user.details.address=sampleAddress
user.details.something=sampleSomething

如何映射"user.name";到字段"customname";和"user.details。*";";customeDetails"不更改字段名?

您可以使用@AliasFor("name")

您必须使用@Value注释提供配置文件中的属性路径。

例如,如果您想将user.name映射到customName:

...
@Value(${user.name})
private String customName;
...

Spring将自动获取user.name属性值并将其分配给customName变量属性。

最新更新