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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With