简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM