javaspringspring-bean

when using @Value spring annotation, is it possible to leverage some sort of string interpolation?


Can I do something like this?

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

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

Essentially myVar key is getting used elsewhere so wanted to see if it can be moved to a constant?


Solution

  • Yes its possible:

      private static final String myVar = "health.xyz.throughput";
    
      @Value("${" + myVar + ":10}")
      private Integer healthThroughput;
    

    Also you can use:

      private static final String myVar = "${health.xyz.throughput:10}";
    
      @Value(myVar)
      private Integer healthThroughput;