将 EJB3.0 Bean 注入 JSF2.0 支持 Bean...不可能吗?
我正在开发一个基于 Weblogic 11g 的 JSF 项目,我们的初始设计是调用 JSF Backing Beans 来调用 EJB3.0 bean 来执行业务逻辑和数据访问调用。当我尝试将 EJB 引用注入到支持 bean 时,@EJB 注释似乎在我的项目中不起作用。每当我访问正在测试的类时,我的 EJB 的构造函数都不会被调用,并且最终会得到一个 NPE。是否可以将 EJB3.0 bean 注入 JSF 支持 bean 中?是否还有其他方法可以通过 JSF Backing bean 调用 EJB?最佳实践是什么?
I'm working on a JSF project on Weblogic 11g, and our initial design is calling for JSF Backing Beans to invoke EJB3.0 beans to perform business logic and data access calls. The @EJB annotation doesn't seem to work in my project when I try to inject the EJB reference to the backing bean. Whenever I hit the class that I am testing, the constructor for my EJB is never called and I end up with a NPE. Is it possible to inject an EJB3.0 bean into a JSF backing bean? Is there another way I should be invoking an EJB through the JSF Backing bean? What is the best practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我google了一下,这确实似乎是一个已知的Weblogic 的问题。许多类似的话题都没有得到解答。
我发现 此博客,确认 Weblogic 中的
@EJB
仅适用于web.xml
定义的资源,不适合 JSF。该博客还详细描述了使用 ServletContextListener 的解决方法,在我看来,它并不比使用 JNDI 好多少。我还发现了 此 OTN 主题,它证实了
@EJB<当 EJB 模块不包含在子目录中时,Weblogic 中的 /code> 开始工作(请参阅底部发布的答案,2011 年 2 月 15 日 5:44下午)。
I googled somewhat and this indeed seems to be a known issue with Weblogic. Lot of similar topics are kept unanswered.
I found this blog which confirms that
@EJB
in Weblogic only works for resources definied byweb.xml
, not for JSF. The blog describes also in detail a workaround usingServletContextListener
which is IMO not much better than using JNDI.I also found this OTN topic which confirms that
@EJB
in Weblogic started to work when EJB modules are not included in subdirectories (see the answer posted at the bottom, Feb 15, 2011 5:44 PM).事实证明,这是使用 JSF 和 EJB 部署任何内容时的 Weblogic 特定问题。我在 Oracle 论坛上找到了这篇文章,解释了如何使用 Weblogic 11g 在 JSF Managed Beans 中使用 @EJB 注入:
EJB3.0 注入 JSF 托管 bean
更新:
在旋转了太久之后,我不得不放弃尝试注入将 EJB 转换为 Weblogic 11g 上的 JSF ManagedBean。看来在 Tomcat 中工作得很好。也许EJB3和JSF的实现在12G中会更好......
It turns out that it is a Weblogic specific issue when deploying anything using JSF and EJB. I found this post on the Oracle forums that explains how to get the @EJB injection working in JSF Managed Beans using Weblogic 11g:
EJB3.0 Injection into JSF Managed beans
UPDATE:
After spinning my wheels for too long, I have to give up trying to inject an EJB into a JSF ManagedBean on Weblogic 11g. Seems to work fine in Tomcat. Maybe the EJB3 and JSF implementation will be better in 12G...
要使其工作,您需要执行以下两个步骤:
在您的 Web 项目中,在 WEB-INF/weblogic.xml 中添加对 jsf-2.0.war 库的引用
我已经在weblogic 10.3.3和10.3.5中成功测试了这一点。如果不知何故这不起作用,请尝试将应用程序部署为 EAR 文件的一部分。
To make it work you need to follow two steps:
In your web project, add the reference to the jsf-2.0.war library in WEB-INF/weblogic.xml
I have successfully tested this in weblogic 10.3.3 and 10.3.5. If somehow this does not work, try to deploy the application as part of EAR file.
所以这是节拍!有一个简单的方法可以解决这个问题。
在 ...wlserver_10.3\common\deployable-libraries 下打开 jsf-2.0.war
导航到 WEB-INF/lib 并将 wls.jsf.di.jar JAR 保存在某处
放置 wls.jsf.di。将 wls.jsf.di.jar JAR 放在 WAR 应用程序的 lib 文件夹下。
部署
现在,只需将 @EJB 添加到 @ManagedBean 中的属性,即可全部
So here is the beat! There is a simple way to fix this.
Open up jsf-2.0.war under ...wlserver_10.3\common\deployable-libraries
Navigate to WEB-INF/lib and save wls.jsf.di.jar JAR somewhere
Place wls.jsf.di.jar JAR under lib folder of your WAR application.
Deploy
all should work now just by adding @EJB to property in your @ManagedBean.
@EJB 注释有一个替代方案,以便在 JSF ManagedBean Web 应用程序中访问本地 EJB bean。考虑到您将 EJB 类和 WAR 打包在同一个 EAR 文件中,请执行以下操作:
配置 ejb-jar.xml 以告诉 weblogic 将 EJB bean 公开给外部组件;
在 Web 应用程序的 web.xml 中插入通过 ejb 链接名称对 EJB 的引用。 ejb-ref-name 是 JSF 托管 bean 可见的名称。
在您的 JSF Managed Bean 中,通过 JNDI 查找调用 EJB Bean,如下所示:
就我而言,我使用的是 Weblogic 10.3.6 (11g)、JSF 2.0 和 EJB 3.0 以及 JPA (Eclipselink)
There is an alternative for the @EJB annotation in order to get your local EJB bean accessible in your JSF ManagedBean web application. Considering that you have your EJB classes and your WAR packaged in the same EAR file, do the following:
configure your ejb-jar.xml to tell the weblogic expose the EJB beans to the external components;
Insert in the web.xml of your web application a reference to the EJB throught the ejb-link name. The ejb-ref-name is name visible for the JSF managed beans.
In your JSF Managed Bean call the EJB Bean through JNDI lookup as the following:
In my case I was using the Weblogic 10.3.6 (11g), JSF 2.0 and EJB 3.0 with JPA (Eclipselink)