gpt4 book ai didi

Java注释: pass value of annotation attribute to another annotation

转载 作者:行者123 更新时间:2023-11-30 03:25:42 26 4
gpt4 key购买 nike

我有接口(interface)资源和几个实现它的类,例如音频视频...此外,我创建了自定义注释<带有类类型参数的strong>MyAnnotation:

 @MyAnnotation(type = Audio.class)
class Audio {
...
}

@MyAnnotation(type = Video.class)
class Video{
...
}

在代码中的其他地方,我必须使用接口(interface)资源作为返回类型:

public class Operations<T extends Resource> {
....
@OtherAnnotation(type = Audio.class (if audio), type = Video.class (if video) )
T getResource();
....
}

问题是如何根据返回的资源类型适本地注释注释@OtherAnnotation

最佳答案

您要求的是注释属性的动态值。

但是注释只能在编译时设置,这就是为什么它们的值只能是编译时常量的原因。您只能在运行时读取它们。

<小时/>

有一个类似的问题,有人试图生成注释值,it's answer 更详细地解释了为什么无法动态生成注释中使用的值。在该问题中,尝试使用通过静态方法生成的最终类变量。

<小时/>有一些注释处理器通过处理占位符提供了更多的灵 active 。但是我认为这不适合您的情况,因为您需要运行时的动态值。

This answer指的是spring使用expression language对于 Value 注释,其中占位符 (@Value("#{systemProperties.dbName})") 被定义的属性源之一的数据覆盖 ( example in spring boot )

无论如何,您都必须重新考虑一下您的架构。

关于Java注释: pass value of annotation attribute to another annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30300542/

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