【发布时间】:2020-05-19 13:52:00
【问题描述】:
如何在下面的代码中使用前缀?
属性:
height.customer.feet=10
height.customer.eu.timezone=UTC
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "height.customer")
public class Customer {
private final int age;
private final String timezone;
public Customer(int age, String timezone){
this.age = age;
this.timezone = timezone;
}
}
在这里,我想为年龄和时区设置默认值。默认值从 application.properties 文件中读取。有人可以帮帮我吗?
我可以像下面这样使用。
@Value("${height.customer.age}")
private final int age;
@Value("${height.customer.eu.timezone}")
private final String timezone;
但如果我这样使用,我可能无法使用构造函数注入
【问题讨论】:
-
为什么需要构造函数注入?值年龄和时区将正常使用“@ConfigurationProperties”或“@Value”填充。
标签: spring spring-boot spring-mvc spring-data