如何将Annotation作为参数传递?

noo*_*one 8 java parameters annotations cdi bean-manager

CDI类BeanManager有几种采用类型Annotation或参数的方法Annotation....例如BeanManager.getBeans(...).

我想知道我应该如何将我的注释作为参数传递给那些方法.

我试过了BeanManager.getBeans(MyBean.class, MyAnnotation.class),但是不行.我见过Class.isAnnotation(),但没有什么比Class.asAnnotation()把它作为一种Annotation类型来检索它了.

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

如何将我的注释类检索为类型Annotation

pal*_*int 9

文档中有一个例子:

beanManager.getBeans(Object.class, new AnnotationLiteral<Any>() {});
Run Code Online (Sandbox Code Playgroud)

资料来源:16.6.Bean界面

  • 这就是诀窍.不过,这是一种奇怪的解决方案.我认为标准的Java API会提供更方便的东西. (2认同)