Reputation: 51
I am developing a simple web app in Java EE. I use annotations for the servlets and I would like to fill the parameters of the annotation with value from a properties file but I don't know how to do this. I would like to do something like this :
// My Servlet
@WebServlet(urlPatterns="${key.value1}")
public class HomeServlet extends MyCustomServlet
{
...
}
# My properties files
key.value1=/home
Is it possible ? If yes, what is the solution ?
Thanks.
Upvotes: 5
Views: 1783
Reputation: 279990
That's not directly possible. The values you give to annotation attributes must be constants. They cannot be modified after the code is compiled. From the Java Language Specification
It is a compile-time error if the return type of a method declared in an annotation type is not one of the following: a primitive type, String, Class, any parameterized invocation of Class, an enum type (§8.9), an annotation type, or an array type (§10) whose element type is one of the preceding types.
However, if you controlled how they were processed (which I don't see how you would since these are processed by the Servlet container), you could get the value and do some placeholder resolution.
Here's an alternative for dynamic url patterns.
Upvotes: 5