Java 如何将注释作为参数传递?
CDI类有几种方法,它们采用类型为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)既
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上这个阶级本身?