Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
788 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 {

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...