【问题标题】:Can I pass variable in @Value annotation to read a property file, whose key is stored in a String variable?我可以在@Value 注释中传递变量来读取属性文件,其键存储在字符串变量中吗?
【发布时间】:2020-01-17 05:51:43
【问题描述】:

我可以从属性文件实体的键值存储在变量(contants 类)中的属性文件中获取值吗?

结构如下: 属性文件

x.y.z=123     

常量文件

public static final String abc = "x.y.z";

服务类

@Value(${"getConstant("abc").getValueOfProperty()})
public String resource;

【问题讨论】:

  • 如果它是一个充满静态常量的类,当然。
  • 这里我不想只从常量类中读取值。价值转移在这里发生了两次。
  • 只要String变量是final的,就可以在@Value注解中使用。
  • 请提供一些示例代码。
  • 结构类似于:属性文件--- x.y.z=123 常量文件== public static string abc = "x.y.z" 服务类 --- @Value(${"getConstant("abc") .getValueOfProperty()})

标签: java spring


【解决方案1】:

我认为,我们可以这样做,

private final String VALUE_STRING = "${external.value:default}";
@Value(VALUE_STRING)
private String value;

【讨论】:

    猜你喜欢
    • 2018-06-26
    • 2020-08-11
    • 1970-01-01
    • 2018-04-16
    • 2019-04-24
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多