将 EJB3.0 Bean 注入 JSF2.0 支持 Bean...不可能吗?

发布于 2024-11-14 06:17:42 字数 270 浏览 7 评论 0原文

我正在开发一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

停滞 2024-11-21 06:17:42

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 by web.xml, not for JSF. The blog describes also in detail a workaround using ServletContextListener 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).

夏末染殇 2024-11-21 06:17:42

事实证明,这是使用 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...

倾城月光淡如水﹏ 2024-11-21 06:17:42

要使其工作,您需要执行以下两个步骤:

  1. 将 jsf-2.0.war 部署为 LIBRARY,您可以找到它 /ORACLE_HOME/wlserver_10.3/common/deployable-libraries

在此处输入图像描述

  1. 在您的 Web 项目中,在 WEB-INF/weblogic.xml 中添加对 jsf-2.0.war 库的引用

     
    
    
        10.3.3
        your_context_app
    
        
          jsf
          2.0
          1.0.0.0_2-0-2
          true
                              
    
    
    

我已经在weblogic 10.3.3和10.3.5中成功测试了这一点。如果不知何故这不起作用,请尝试将应用程序部署为 EAR 文件的一部分。

To make it work you need to follow two steps:

  1. Deploy jsf-2.0.war as LIBRARY, you can find it /ORACLE_HOME/wlserver_10.3/common/deployable-libraries

enter image description here

  1. In your web project, add the reference to the jsf-2.0.war library in WEB-INF/weblogic.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.1/weblogic-web-app.xsd">
    
        <wls:weblogic-version>10.3.3</wls:weblogic-version>
        <wls:context-root>your_context_app</wls:context-root>
    
        <wls:library-ref>
          <wls:library-name>jsf</wls:library-name>
          <wls:specification-version>2.0</wls:specification-version>
          <wls:implementation-version>1.0.0.0_2-0-2</wls:implementation-version>
          <wls:exact-match>true</wls:exact-match>
        </wls:library-ref>                      
    
    </wls:weblogic-web-app>
    

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.

甜柠檬 2024-11-21 06:17:42

所以这是节拍!有一个简单的方法可以解决这个问题。

  1. 在 ...wlserver_10.3\common\deployable-libraries 下打开 jsf-2.0.war

  2. 导航到 WEB-INF/lib 并将 wls.jsf.di.jar JAR 保存在某处

  3. 放置 wls.jsf.di。将 wls.jsf.di.jar JAR 放在 WAR 应用程序的 lib 文件夹下。

  4. 部署

现在,只需将 @EJB 添加到 @ManagedBean 中的属性,即可全部

So here is the beat! There is a simple way to fix this.

  1. Open up jsf-2.0.war under ...wlserver_10.3\common\deployable-libraries

  2. Navigate to WEB-INF/lib and save wls.jsf.di.jar JAR somewhere

  3. Place wls.jsf.di.jar JAR under lib folder of your WAR application.

  4. Deploy

all should work now just by adding @EJB to property in your @ManagedBean.

你的他你的她 2024-11-21 06:17:42

@EJB 注释有一个替代方案,以便在 JSF ManagedBean Web 应用程序中访问本地 EJB bean。考虑到您将 EJB 类和 WAR 打包在同一个 EAR 文件中,请执行以下操作:

  1. 配置 ejb-jar.xml 以告诉 weblogic 将 EJB bean 公开给外部组件;

    ;
    <会话>
        MyEJBBean
        com.app.MyEJBBeanLocalInterface
        com.app.MyEJBBeanLocalImpl
        <会话类型>无状态
        <事务类型>容器
        
            ejb/MyEJBBeanLocal
            com.app.MyEJBBeanLocalInterface
        
    
     <企业-bean>
    
  2. 在 Web 应用程序的 web.xml 中插入通过 ejb 链接名称对 EJB 的引用。 ejb-ref-name 是 JSF 托管 bean 可见的名称。

    ;
      ejb/MyEJBBeanLocal
      会话
      com.app.MyEJBBeanLocalInterface
      MyEJBBean
    
    
  3. 在您的 JSF Managed Bean 中,通过 JNDI 查找调用 EJB Bean,如下所示:

    尝试{
        上下文上下文 = new InitialContext();
        MyEJBBeanLocalInterface myEJBBean = 
        context.lookup("java:comp/env/ejb/MyEJBBeanLocal");
    } catch (NamingException e) {
        e.printStackTrace();
    }

就我而言,我使用的是 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:

  1. configure your ejb-jar.xml to tell the weblogic expose the EJB beans to the external components;

    <enterprise-beans>
    <session>
        <ejb-name>MyEJBBean</ejb-name>
        <business-local>com.app.MyEJBBeanLocalInterface</business-local>
        <ejb-class>com.app.MyEJBBeanLocalImpl</ejb-class>
        <session-type>Stateless</session-type>
        <transaction-type>Container</transaction-type>
        <ejb-local-ref>
            <ejb-ref-name>ejb/MyEJBBeanLocal</ejb-ref-name>
            <local>com.app.MyEJBBeanLocalInterface</local>
        </ejb-local-ref>
    </session>
     <enterprise-beans>
    
  2. 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.

    <ejb-local-ref>
      <ejb-ref-name>ejb/MyEJBBeanLocal</ejb-ref-name>
      <ejb-ref-type>Session</ejb-ref-type>
      <local>com.app.MyEJBBeanLocalInterface</local>
      <ejb-link>MyEJBBean</ejb-link>
    </ejb-local-ref>
    
  3. In your JSF Managed Bean call the EJB Bean through JNDI lookup as the following:

    try {
        Context context = new InitialContext();
        MyEJBBeanLocalInterface myEJBBean = 
        context.lookup("java:comp/env/ejb/MyEJBBeanLocal");
    } catch (NamingException e) {
        e.printStackTrace();
    }

In my case I was using the Weblogic 10.3.6 (11g), JSF 2.0 and EJB 3.0 with JPA (Eclipselink)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文