WELD-001408 Unsatisfied dependencies when injecting EntityManager

孤者浪人 提交于 2019-12-23 08:52:20

问题


I have @Stateless bean which implements two interfaces (remote and local). I have also added @LocalBean anotation for accessing bean with a no-interface view.

@Stateless
@LocalBean
public class WeatherDataBean implements WeatherDataBeanRemote, WeatherDataBeanLocal {
    @Inject
    private EntityManager entityManager;

    public WeatherDataBean () {

    }
    // ....attributes, getter & setter methods ....
}

I use @Inject for this reason taken from this example of JBoss AS7 quickstart:

We use the "resource producer" pattern, from CDI, to "alias" the old fashioned @PersistenceContext injection of the entity manager to a CDI style injection. This allows us to use a consistent injection style (@Inject) throughout the application.

Now previously I have used:

@PersistenceContext(unitName="WeatherStationJPA")
private EntityManager entityManager;

In EJB and it works without any problem. But with @Inject annotation I get this error:

WELD-001408 Unsatisfied dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[field] @Inject private ejb.WeatherDataBean.entityManager]

Here is how I have class Resources defined:

public class Resources {
     @SuppressWarnings("unused")
     @PersistenceContext(unitName="WeatherStationJPA")
     @Produces
     private EntityManager entityManager;

     @Produces
     FacesContext getFacesContext() {
         return FacesContext.getCurrentInstance();
     }
}

Why Is that I get this error if I try to inject entity manager?

EDIT: On request from @LightGuard I am adding packages that I am using to reference annotations:

  1. WeatherDataBean has:

    import javax.ejb.LocalBean;
    import javax.ejb.Stateless;
    import javax.inject.Inject;
    
  2. Resources has:

    import javax.enterprise.inject.Produces;
    import javax.faces.context.FacesContext;
    import javax.inject.Singleton;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    

回答1:


I just had the same problem, I fixed this by adding this class to my project

import java.util.logging.Logger;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * This class uses CDI to alias Java EE resources, such as the persistence context, to CDI beans
 * 
 * <p>
 * Example injection on a managed bean field:
 * </p>
 * 
 * <pre>
 * &#064;Inject
 * private EntityManager em;
 * </pre>
 */
public class Resources {
   // use @SuppressWarnings to tell IDE to ignore warnings about field not being referenced directly
   @SuppressWarnings("unused")
   @Produces
   @PersistenceContext
   private EntityManager em;

  // @Produces
  // public Logger produceLog(InjectionPoint injectionPoint) {
   //   return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
  // }

   @Produces
   @RequestScoped
   public FacesContext produceFacesContext() {
      return FacesContext.getCurrentInstance();
   }

}



回答2:


I have ran into a similar error, turned out I didn't put a beans.xml file in the WEB-INF folder. The beans.xml file can be an empty file sitting in the WEB-INF folder. JBossAS checks that file to start the CDI service.



来源:https://stackoverflow.com/questions/11748006/weld-001408-unsatisfied-dependencies-when-injecting-entitymanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!