使用JEE6将EJB注入JSF转换器 [英] Inject a EJB into a JSF converter with JEE6
问题描述
我需要一个JSF 2转换器中的bean来从String值参数中检索一个实体对象。我使用的是Glassfish V3的JEE6。
@EJB
注释不起作用并获得NPE,因为它是在面部上下文中,它没有访问EJB上下文。
我的问题是:
仍然可以使用 @Resource
或其他注释或JNDI查找,还是需要解决方法?
解决方案
一个这样的JNDI查找:
try {
ic = new InitialContext();
myejb =(MyEJB)ic
.lookup(java:global / xxxx / MyEJB);
} catch(NamingException e){
e.printStackTrace();
}
我从来没有使用JSF 2.0只有1.0),但规范的第5.4章说:
[...]允许容器注入
的引用容器管理的资源进入受管理的bean实例,使其可以被JSF应用程序访问。
只有 bean 声明为请求,
会话或应用程序范围为
才有资源注入。
但是到目前为止,我明白,JNDI查找应该做的伎俩。
I have a stateless EJB that acceses my database. I need this bean in a JSF 2 converter to retreive an entity object from the String value parameter. I'm using JEE6 with Glassfish V3.
@EJB
annotation does not work and gets a NPE, because it's in the faces context and it has not access to the EJB context.
My question is:
Is it still possible to Inject this bean with a @Resource
or other annotation, or a JNDI lookup, or do I need a workaround?
Solution
Do a JNDI lookup like this:
try {
ic = new InitialContext();
myejb= (MyEJB) ic
.lookup("java:global/xxxx/MyEJB");
} catch (NamingException e) {
e.printStackTrace();
}
I never used JSF 2.0 (only 1.0), but chapter 5.4 of the spec says:
[...] allow the container to inject references to container managed resources into a managed bean instance before it is made accessible to the JSF application. Only beans declared to be in request, session, or application scope are eligble for resource injection.
But so far I understand, a JNDI lookup should do the trick.
这篇关于使用JEE6将EJB注入JSF转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!