依赖注入 – WELD-001408注入EntityManager时不满意的依赖关系

前端之家收集整理的这篇文章主要介绍了依赖注入 – WELD-001408注入EntityManager时不满意的依赖关系前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有@Stateless bean,它实现了两个接口(远程和本地).我还添加了@LocalBean anotation来访问带有无接口视图的bean.
@Stateless
@LocalBean
public class WeatherDataBean implements WeatherDataBeanRemote,WeatherDataBeanLocal {
    @Inject
    private EntityManager entityManager;

    public WeatherDataBean () {

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

我使用@Inject因为这个原因采取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.

以前我用过:

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

在EJB中,它没有任何问题.但是使用@Inject注释我得到这个错误

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

这是我如何定义类资源:

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

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

如果我尝试注入实体管理器,为什么会出现此错误

编辑:
根据@LightGuard的请求,我正在添加用于引用注释的包:

> WeatherDataBean有:

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.inject.Inject;

>资源有:

import javax.enterprise.inject.Produces;
import javax.faces.context.FacesContext;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
我只是遇到了同样的问题,我通过将这个类添加到我的项目中来解决这个问题
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();
   }

}
原文链接:https://www.f2er.com/javaschema/281533.html

猜你在找的设计模式相关文章