5

I have a properties file that I read by spring annotation like this

    @Value("${platform}")
    private String platform;

after I get the platform parameter, I would like to read a second parameter depending on platform parameter value.

    @Value("${url." + platform + ."ws}")
    private String url;

but this gives error, "value for the annotation attribute must be constant expression". since there are lots of parameter changes depending on "platform" value, I am looking for a generic solution.

m_drinks_coffee
  • 436
  • 8
  • 24
ysnky
  • 438
  • 2
  • 6
  • 14

2 Answers2

3

You can't access platform directly in the @Value expression, but you can use Spring Expression Language to accomplish your end goal.

@Value("${platform}")
private String platform;

@Value("#{'Url.'.concat(${platform}).concat('.ws')}")
private String url;
LucasP
  • 1,665
  • 16
  • 24
2

The parameter is evaluated in compilation time. So it needs to be final or static final among others (ie Enum).

I don't know if the @Value annotation allows that. But you can always implement your own annotation. Extending is not possible in Java annotations.

ssedano
  • 8,322
  • 9
  • 60
  • 98