【发布时间】:2021-08-30 20:30:06
【问题描述】:
有没有办法在 .properties 文件中添加一个属性而不调用它的 getter。
所以我期待的是:
application.properties
name=user1
email=user@xyzdomain.com
contact=123-456-0789
header=val1,val2,val3
.
.
.
现在,我该如何编写这样的方法:
public String getKey(String key) {
// any key passed, eg: name and I should get its value
// Do I have to create a switch statement to see which key it is and using the //mapping of its corresponding getter method, call that getter. Is there a better way?
...
}
所以,任何传递的键,例如:名称,我都应该得到它的值。我是否必须创建一个 switch 语句来查看它是哪个键并使用其相应 getter 方法的映射,调用该 getter。有没有更好的办法?
【问题讨论】:
-
你尝试注入
org.springframework.core.env.Environment吗?
标签: java spring-boot properties-file