如何将注释参数提取到外部文件中?

How to extract annotation arguments into external file?

我关注 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).

明确指定,否则许多注释可能不会保留在编译后的代码中