当使用@Value spring注解时,是否可以利用某种字符串插值?

nx7onnlm  于 2023-04-28  发布在  Spring
关注(0)|答案(1)|浏览(103)

我能做这样的事吗?

private final static String myVar = "health.xyz.throughput";

@Value("${{myVar}:10}")   // doesn't work of course
private Integer healthThroughput;

本质上myVar键正在其他地方使用,所以想看看它是否可以移动到一个常量?

kxkpmulp

kxkpmulp1#

是的可能:

private static final String myVar = "health.xyz.throughput";

  @Value("${" + myVar + ":10}")
  private Integer healthThroughput;

您还可以用途:

private static final String myVar = "${health.xyz.throughput:10}";

  @Value(myVar)
  private Integer healthThroughput;

相关问题