WELD-001408注入EntityManager时不满足的依赖关系 [英] WELD-001408 Unsatisfied dependencies when injecting EntityManager

查看:291
本文介绍了WELD-001408注入EntityManager时不满足的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有@Stateless bean,它实现了两个接口(远程和本地).我还添加了@LocalBean注释,用于使用无接口视图访问Bean.

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

出于这个原因,我使用@Inject 来自JBoss AS7快速入门示例:

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

我们使用CDI的资源生产者"模式,将实体管理器的老式@PersistenceContext注入别名"为CDI样式注入.这使我们可以在整个应用程序中使用一致的注入样式(@Inject).

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注释时,我会收到此错误:

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

WELD-001408类型[EntityManager]的依赖关系不令人满意 注入点[[field]处的限定词[@Default] @Inject private ejb.WeatherDataBean.entityManager]

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?

应@LightGuard的要求,我正在添加用于参考批注的软件包:

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

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

  • 推荐答案

    我只是遇到了同样的问题,我通过将此类添加到我的项目中来解决了这个问题

    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();
       }
    
    }
    

    这篇关于WELD-001408注入EntityManager时不满足的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆