我有@Stateless
bean,它实现两个接口(远程和本地)。我还添加了@LocalBean
anotation来访问具有无界面视图的bean。WELD-001408注入EntityManager时不满意的依赖关系
@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;
一切都看起来正确。你确定你有正确的注释(检查包)吗?也可能取决于你如何包装东西。 – LightGuard 2012-08-01 17:56:31
请检查我的答案我已经添加了哪些软件包用于这些注释。我正在使用EAR项目来打包EJB项目,EJBClient项目,JPA项目和JSF项目。 – 2012-08-01 18:06:40
啊,好的,如果你使用的是EAR,东西会改变一点。您必须放置您正在进行注入的位置,类所在的库以及beans.xml文件所在的位置。 – LightGuard 2012-08-04 07:12:21