简体   繁体   中英

Can't Inject DAO into EJB using CDI

I'm working on an application using EJB3, JPA and JSF and I'd like to inject my DAO into the EJB using CDI :

Here's my DAO's code :



    public class ZoneDao {

        @PersistenceContext(unitName = "wabApp3PU")
        private EntityManager em;

        /* Functions */

    }

and here's my EJB code :



    @Stateless
    public class ZoneFacade{

        @Inject
        private ZoneDao zoneDao;

        /* Functions*/

    }

Here's the error I get :

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [ZoneDao] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private fr.si.metier.ZoneFacade.zoneDao]

NB: I'm using the glassfish 4 application server

JAVA EE 6-CDI组件,给DAO @ApplicationScoped,然后它将允许注入持久性上下文

Add @Stateless to your ZoneDao Class or give this Bean a Scope eg @SessionScoped .

Make sure you have a beans.xml if using Java EE 6. With version 7 it is not required anymore.

ZoneDao is not marked as a managed bean and so it won't work like that using CDI. Annotate ZoneDao with @Named , and choose it's scope using @RequestScope, @SessionScop,...etc or what ever scope you need, Only then it will be a managed bean that can be injected using CDI.

  1. Which IDE are you using? If Netbeans, check if it has generated beans.xml in the META-INF/WEB-INF folder. If it has, check to ensure that default bean-discovery mode is not set to annotated, if it is, annotate the DAO with @Dependent.
  2. You cannot inject entitymanager into a CDI bean using @PersistenceContext.(Until the CDI guys, or the server vendors change this). Simple solution, as it seems you are learning is to inject the entitymanager in a stateless bean (and if you are using ejb in a war application, you can then inject the stateless EJB)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM