Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将注释作为参数传递?_Java_Parameters_Annotations_Cdi_Bean Manager - Fatal编程技术网

Java 如何将注释作为参数传递?

Java 如何将注释作为参数传递?,java,parameters,annotations,cdi,bean-manager,Java,Parameters,Annotations,Cdi,Bean Manager,CDI类有几种方法,它们采用类型为Annotation或Annotation…的参数。比如说 我想知道如何将注释作为参数传递给这些方法 我尝试了BeanManager.getBeans(MyBean.class,MyAnnotation.class),但它不是那样工作的。我见过Class.isAnnotation(),但是没有什么能像Class.asAnnotation()那样将其作为注释类型检索 BeanManager.getBeans(MyBean.class,@MyAnnotation)既

CDI类有几种方法,它们采用类型为
Annotation
Annotation…
的参数。比如说

我想知道如何将注释作为参数传递给这些方法

我尝试了
BeanManager.getBeans(MyBean.class,MyAnnotation.class)
,但它不是那样工作的。我见过
Class.isAnnotation()
,但是没有什么能像
Class.asAnnotation()
那样将其作为
注释
类型检索

BeanManager.getBeans(MyBean.class,@MyAnnotation)
既不起作用,也没有
BeanManager.getBeans(MyBean.class,(Annotation)MyAnnotation.class)

如何将注释类检索为类型?

您需要使用

getAnnotation(类annotationClass)返回此元素的 指定类型的注释(如果存在此类注释), 否则为空

或循环通过

getAnnotations()返回此元素上存在的所有批注

获取注释

object.getClass().getAnnotations()


文档中有一个示例:

beanManager.getBeans(Object.class, new AnnotationLiteral<Any>() {});

beanManager.getBeans(Object.class,new AnnotationLiteral

不清楚您要做什么。beanManager.getBeans(…)返回指定类型的bean实例。如果您试图读取注释本身,则需要使用@Hirak所述的反射。这是一种奇怪的解决方案。我认为标准Java API将提供更方便的功能。我不确定这些方法的名称。在annota上这个阶级本身?