使用 JEE6 将 EJB 注入 JSF 转换器 [英] Inject a EJB into a JSF converter with JEE6

查看:20
本文介绍了使用 JEE6 将 EJB 注入 JSF 转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个访问我的数据库的无状态 EJB.我需要在 JSF 2 转换器中使用这个 bean 从字符串值参数中检索实体对象.我将 JEE6 与 Glassfish V3 结合使用.

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 注释不起作用并获得 NPE,因为它位于 faces 上下文中,并且无法访问 EJB 上下文.

@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.

我的问题是:是否仍然可以使用 @Resource 或其他注释或 JNDI 查找注入此 bean,或者我是否需要解决方法?

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?

解决方案

像这样执行 JNDI 查找:

Do a JNDI lookup like this:

  try {
   ic = new InitialContext();
   myejb= (MyEJB) ic
     .lookup("java:global/xxxx/MyEJB");   
  } catch (NamingException e) {
   e.printStackTrace();
  }

推荐答案

我从未使用过 JSF 2.0(只有 1.0),但是规范的第 5.4 章说:

I never used JSF 2.0 (only 1.0), but chapter 5.4 of the spec says:

[...] 允许容器注入对容器管理的资源在 JSF 应用程序可以访问之前放入一个托管 bean 实例中.只有 beans 声明为请求,会话或应用范围是符合资源注入条件.

[...] 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.

但就我所知,JNDI 查找应该可以解决问题.

But so far I understand, a JNDI lookup should do the trick.

这篇关于使用 JEE6 将 EJB 注入 JSF 转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆