Lets say I have a custom Annotation like below:
public @interface myLimits {
int MIN_LIMIT = 400;
int stockLimit() default MIN_LIMIT;
}
Instead of hard-coding the value of MIN_LIMIT in the annotation definition, can we externalize it to a properties file?
Something like below – Assuming “min.limit” is defined in a properties file.
public @interface myLimits {
@value("${min.limit}")
int MIN_LIMIT;
int stockLimit() default MIN_LIMIT;
}
How do I externalize MIN_LIMIT instead of hard-coding?
Strictly speaking: you can’t. Annotations are processed in a separate compile round before the rest of the code, which means that everything that appears anywhere in an annotation must be a compile-time constant.
Here’s what the JLS has to say about it: