我有以下类(class)。
public class MultipartFileWrapper {
@Extensions({".jpg",".png",".gif",".bmp",".mp4"})
MultipartFile multipartFile;
...
}
现在我想将格式提取到配置文件中。 但我不明白如何替换
@Extensions({".jpg",".png",".gif",".bmp",".mp4"})
我想重写它,如下所示:
public class MultipartFileWrapper {
@Extensions(readFormatsFromFile())
MultipartFile multipartFile;
...
}
格式应取自具有以下内容的外部文件:
.jpg,.png,.gif,.bmp,.mp4
java允许这样做吗?
最佳答案
属性值必须是常量。
来自Java Language Specification, section 9.7.1 ,强调我的:
Java does not allow for runtime annotation arguments: annotation parameters are stored at compile-time, and hence cannot be dynamic.
If T is a primitive type or String, and V is a constant expression (§15.28).
V is not null.
If T is Class, or an invocation of Class, and V is a class literal (§15.8.2).
If T is an enum type, and V is an enum constant.
就您而言,V
不是常量表达式。
除此之外,许多注释可能不会保留在编译的代码中,除非通过@Retention(RetentionPolicy.RUNTIME)
明确指定。 .
关于java - 如何将注释参数提取到外部文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29802014/