[英]Cast Bean<?> from BeanManager to class instance in Servlet
I have an EJB project, and have the following annotation: 我有一个EJB项目,并具有以下注释:
@Retention(RUNTIME)
@Target(TYPE)
@Qualifier
public @interface AfterComplete {
}
I have 2 classes that are decorated with this annotation. 我有2个用此注释修饰的类。 They are also
@Stateless
他们也是
@Stateless
@AfterComplete
@Stateless
public class AfterCompletePrinter implements IAfterComplete {
public void afterComplete(String someValue) {
System.out.println("After complete printer: " + someValue);
}
}
@AfterComplete
@Stateless
public class AfterCompleteErrPrinter implements IAfterComplete{
public void afterComplete(String someValue) {
System.err.println(someValue);
}
}
In a servlet, I have: 在servlet中,我有:
@WebServlet("/")
public class DemoServlet extends HttpServlet {
@Inject
BeanManager beanManager;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Set<Bean<?>> beans = beanManager
.getBeans(Object.class,new AnnotationLiteral<AfterComplete>() {});
for (Bean<?> bean : beans) {
response.getWriter().print(bean.getBeanClass());
// IAfterComplete iac = (IAfterComplete) bean;
}
}
}
My servlet prints: 我的servlet打印:
net.mikeski.ejb_annotations.AfterCompletePrinterclass net.mikeski.ejb_annotations.AfterCompleteErrPrinter
net.mikeski.ejb_annotations.AfterCompletePrinter类net.mikeski.ejb_annotations.AfterCompleteErrPrinter
However, this line throws an exception if it's not commented: 但是,如果不加注释,此行将引发异常:
IAfterComplete iac = (IAfterComplete) bean;
IAfterComplete iac =(IAfterComplete)bean;
The exception is: 例外是:
java.lang.ClassCastException: org.jboss.weld.bean.SessionBean cannot be cast to net.mikeski.ejb_annotations.IAfterComplete
java.lang.ClassCastException:org.jboss.weld.bean.SessionBean无法转换为net.mikeski.ejb_annotations.IAfterComplete
How can I do something with bean
to get the instance of the class that implements IAfterComplete
? 我如何使用
bean
来执行IAfterComplete
的类的实例? The bean.getName()
returns null. bean.getName()
返回null。
I was thinking if I can get its JNDI name I can do a lookup, but don't see how to do that. 我在想是否可以得到它的JNDI名称,所以可以进行查找,但是看不到该怎么做。
This is Wildfly 10 if it makes any difference. 如果有任何不同,则为Wildfly 10。
So, I found the answer that works, it's done like this: 因此,我找到了有效的答案,它是这样完成的:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Set<Bean<?>> beans = beanManager
.getBeans(Object.class, new AnnotationLiteral<AfterComplete>() {
});
for (Bean<?> bean : beans) {
response.getWriter().print(bean.getBeanClass());
IAfterComplete iac = (IAfterComplete) beanManager.getReference(bean, IAfterComplete.class, beanManager.createCreationalContext(bean));
iac.afterComplete("After complete called on " + iac);
}
}
I put the whole working project on github here: https://github.com/mikebski/ejb-custom-annotations 我将整个工作项目放在这里的github上: https : //github.com/mikebski/ejb-custom-annotations
Here's a quick blog post with a little more of an explanation: 这是一篇简短的博客文章,其中包含一些解释:
http://blog.mikeski.net/blog_post/487 http://blog.mikeski.net/blog_post/487
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.