Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

value for the annotation attribute must be constant expression

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.

like image 617
ysnky Avatar asked Oct 28 '11 07:10

ysnky


People also ask

What is constant expression in Java?

A constant expression is an expression that always produces the same result. More precisely, a constant expression is an expression that produces a pure value of a primitive data type and is only composed of the following: Literals of primitive data types. String literals.

What is the annotation used to define attributes for an element?

We can also explicitly specify the attributes in a @Test annotation. Test attributes are the test specific, and they are specified at the right next to the @Test annotation.

How do you use an array constant in annotation?

if the compiler expects an "Array Initializer" to be passed to the Annotation, declaring a compile-time constant like private static final String[] AB = { ... }; should do. it's understood that Annotation processing happens before the actual compilation, but then the error message is not accurate.

What is the use of @interface annotation?

Java Custom annotations or Java User-defined annotations are easy to create and use. The @interface element is used to declare an annotation. For example: @interface MyAnnotation{}


1 Answers

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;
like image 138
LucasP Avatar answered Sep 19 '22 11:09

LucasP