使用 @Inject 将无状态 EJB 注入 CDI Weld ManagedBean(jboss 6 AS 上的 JSF 1.2 EJB 应用程序)

发布于 2024-10-18 07:38:27 字数 940 浏览 8 评论 0原文

目前,我正在尝试将无状态 EJB 注入 Jboss 6 AS Final 上的 CDI 托管控制器中。控制器在可从 JSF 页面访问的上下文中进行管理。如果我用 @EJB 注入无状态 bean,它就可以工作。如果我使用 @Inject 注入无状态 EJB,则会出现以下异常:

我的控制器:

@Named("TestController")
public class TestController {   
    @Inject
    private TestManagerLocal myTestManager;
        ...
    }
}

我的无状态 bean:

@SuppressWarnings("unchecked")
@Stateless
public class TestManagerBean implements TestManagerLocal {

    @PersistenceContext
    private EntityManager em;
        ...
}

Bean 的接口用 @Local 进行注释。

如果我尝试调用 myTestManager 我会收到以下异常:

WELD-000079 在 JNDI 中找不到 EJB:类 de.crud.org$jboss$weld$bean-jboss$classloader:id="vfs:$$$usr$local$jboss$server$default$deploy$test$ear"-SessionBean-TestManagerBean_$$_WeldProxy

非常感谢。

Currently I am trying to inject a stateless EJB into a CDI managed controller on Jboss 6 AS Final. The controller is managed in the context an accessible from the JSF pages. If I inject the stateless bean with @EJB it is working. If I inject the stateless EJB with @Inject I get the following Exception:

My controller:

@Named("TestController")
public class TestController {   
    @Inject
    private TestManagerLocal myTestManager;
        ...
    }
}

My stateless bean:

@SuppressWarnings("unchecked")
@Stateless
public class TestManagerBean implements TestManagerLocal {

    @PersistenceContext
    private EntityManager em;
        ...
}

The Interface of the Bean is annotated with @Local.

If I try to call myTestManager I get the following exception:

WELD-000079 Could not find the EJB in JNDI: class
de.crud.org$jboss$weld$bean-jboss$classloader:id="vfs:$$$usr$local$jboss$server$default$deploy$test$ear"-SessionBean-TestManagerBean_$$_WeldProxy

THX a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

想念有你 2024-10-25 07:38:27

对于那些没有能力改变对战争的态度的人,我找到了以下解决方法:

  • 在战争中创建一个 EJB
  • 使用 EJB 模块中的 EJB 注入该 EJB
  • 添加 CDI 生产者方法
  • Qualify @Inject 以及那些生产者方法的限定符:

代码:

// This bean is defined in the WEB module
@Stateless
public class EJBFactory {

    @EJB
    protected UserDAO userDAO;

    // ~X other EJBs injected here


    @Produces @EJBBean
    public UserDAO getUserDAO() {
        return userDAO;
    }

    // ~X other producer methods here
}

现在,来自 EAR 中任何位置的 EJB 都可以注入:

// This bean is also defined in the web module
@RequestScoped
public class MyBean {

    @Inject @EJBBean
    private UserDAO userDAO; // injection works

    public void test() {
        userDao.getByID(...); // works
    }

}

EJBBean 是一个简单的标准限定符注释。为了完整起见,这里是:

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface EJBBean {

}

For those not having the luxury to change an ear to a war, I've found the following workaround:

  • Create an EJB in the war
  • Inject that EJB with the EJBs from the EJB module
  • Add CDI producer methods
  • Qualify @Inject with the qualifier for those producer methods:

Code:

// This bean is defined in the WEB module
@Stateless
public class EJBFactory {

    @EJB
    protected UserDAO userDAO;

    // ~X other EJBs injected here


    @Produces @EJBBean
    public UserDAO getUserDAO() {
        return userDAO;
    }

    // ~X other producer methods here
}

Now EJBs from anywhere in the EAR can be injected with:

// This bean is also defined in the web module
@RequestScoped
public class MyBean {

    @Inject @EJBBean
    private UserDAO userDAO; // injection works

    public void test() {
        userDao.getByID(...); // works
    }

}

EJBBean is a simple standard qualifier annotation. For completeness, here it is:

@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface EJBBean {

}
我是有多爱你 2024-10-25 07:38:27

问题是,我构建并部署了我的应用程序作为耳朵。当我将应用程序部署为战争(包括所有 EJB)时,Weld 正在工作。

The problem was, that I built and deployed my application as an ear. Weld is working when I deploy my application as an war including all EJBs.

懷念過去 2024-10-25 07:38:27

目前,由于 EAR 部署中的 WAR 不共享相同的类加载器,因此出现了各种问题。请参阅 https://issues.jboss.org/browse/JBAS-8683 了解正在进行的JBoss-AS JIRA 中的讨论(并投票支持:-) )

更新 我在 如何禁用单独的类加载器,选项 1 对我有用,但要非常小心。类加载器的分离并不是无缘无故地被引入的,所以显然前面的道路上出现了新的问题......

Currently there are various problems arising from the fact that WARs in EAR-Deployments don't share the same classloader. See https://issues.jboss.org/browse/JBAS-8683 for the ongoing discussion in the JBoss-AS JIRA (and vote it up :-) )

UPDATE I found this information on how to disable separate classloaders, option 1 worked for me, but be extremely careful with this. The separation of classloaders hasn't been introduced for no reason, so apparently there are new problems on the road ahead...

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