Inject a stateless EJB with @Inject into CDI Weld ManagedBean (JSF 1.2 EJB Application on jboss 6 AS)

后端 未结 3 556
栀梦
栀梦 2020-12-15 12:28

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

相关标签:
3条回答
  • 2020-12-15 13:07

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

    0 讨论(0)
  • 2020-12-15 13:08

    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.

    0 讨论(0)
  • 2020-12-15 13:13

    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 {
    
    }
    
    0 讨论(0)
提交回复
热议问题