gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 02:46:12 25 4
gpt4 key购买 nike

我正在使用 Java EE 开发一个简单的网络应用程序。我为 servlet 使用注释,我想用属性文件中的值填充注释的参数,但我不知道如何执行此操作。我想做这样的事情:

// My Servlet    
@WebServlet(urlPatterns="${key.value1}")
public class HomeServlet extends MyCustomServlet
{
...
}

# My properties files
key.value1=/home

这可能吗?如果是,解决方案是什么?

谢谢。

最佳答案

这不是直接可能的。您赋予注释属性的值必须是常量。代码编译后不能修改。来自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.

但是,如果您控制它们的处理方式(我不知道您会如何处理,因为它们是由 Servlet 容器处理的),您可以获得该值并进行一些占位符解析。

Here's an alternative for dynamic url patterns.

关于java - 使用属性文件中的值填充注释参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21483995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com