简体   繁体   English

使用属性文件中的值填充注释参数值

[英]Fill annotation parameter value with value from properties file

I am developing a simple web app in Java EE. 我正在使用Java EE开发一个简单的Web应用程序。 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. 我为servlet使用注释,我想用属性文件中的值填充注释的参数,但我不知道如何做到这一点。 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 来自Java语言规范

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. 如果在注释类型中声明的方法的返回类型不是以下之一,则是编译时错误:基本类型,字符串,类,类的任何参数化调用,枚举类型(第8.9节),注释类型或数组类型(第10节),其元素类型是前面的类型之一。

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. 但是,如果您控制了它们的处理方式(我不知道您将如何处理它们,因为它们由Servlet容器处理),您可以获取值并执行一些占位符解析。

Here's an alternative for dynamic url patterns. 这是动态网址格式的替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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