gpt4 book ai didi

java - 默认情况下将注释配置为运行时保留(没有单独的@Retention)

转载 作者:行者123 更新时间:2023-12-02 10:34:08 26 4
gpt4 key购买 nike

是否可以有一个配置,其中给定名称或类型的所有注释自动RetentionPolicy.RUNTIME

我遇到了一个问题,我需要在运行时使用反射来搜索某些注释。但这些是分散在应用程序各处的常见注释,例如 @Service@Transactional ,而且我也不能一一加上@RetentionPolicy对于每个人来说,这都太长了。

最佳答案

Java 语言规范,第 9.6.4.2. @Retention 部分状态:

If T does not have a (meta-)annotation m that corresponds to java.lang.annotation.Retention, then a Java compiler must treat T as if it does have such a meta-annotation m with an element whose value is java.lang.annotation.RetentionPolicy.CLASS.

如您所见,缺少注释与@Retention(RetentionPolicy.CLASS)相同是严格要求。

如果您需要@Retention(RetentionPolicy.RUNTIME),那么您必须明确指定。

关于java - 默认情况下将注释配置为运行时保留(没有单独的@Retention),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416981/

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