Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill annotation parameter value with value from properties file

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.

like image 812
Patrick Avatar asked Jan 31 '14 15:01

Patrick


1 Answers

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.

like image 152
Sotirios Delimanolis Avatar answered Oct 29 '22 17:10

Sotirios Delimanolis