用CDI / Weld注入通用Bean
|
我只是来自很小的JavaSE / Guice世界,目前正在发现“由容器承载” -EE6的路径。在使用Glassfish3.1遇到一些麻烦之后,我刚切换到JBoss,现在面临的问题不应该是一个。
作为基础辅助课程,我试图为任何类型的实体创建通用的存储库/ DAO。以一种非常简单的方式,它可能看起来像这样。
public class Repository<E, K extends Serializable & Comparable<K>> {
private final Instance<EntityManager> entityManagerInstance;
protected final Class<E> getDomainObjectClass() {
return domainObjectClass;
}
private final Class<E> domainObjectClass;
protected final EntityManager getEntityManager() {
return entityManagerInstance.get();
}
@Inject
public Repository(Instance<EntityManager> entityManageryProvider, Provider<E> domainObjectProvider) {
//This is a dirty hack, sadly :(
domainObjectClass = (Class<E>)domainObjectProvider.get().getClass();
this.entityManagerInstance = entityManageryProvider;
}
public final void persist(E domainObject) {
final EntityManager em = getEntityManager();
em.persist(domainObject);
}
public final Collection<E> getAllEntities() {
final EntityManager em = getEntityManager();
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<E> query = cb.createQuery(getDomainObjectClass());
final List<E> result = em.createQuery(query).getResultList();
return Collections.unmodifiableList(result);
}
public final E find(K id) {
Preconditions.checkNotNull(id);
final EntityManager em = getEntityManager();
return em.find(getDomainObjectClass(), id);
}
// [...]
}
现在可能有一个不需要实体依赖查询功能的bean,而只是某种实体类型的存储库,例如(可能是测试用例):
public class DomainObjectARepositoryTest{
@Inject
Repository<DomainObjectA, PersistableUUID> domainObjectARepository;
@Test
public void testMitarbeitererstellung() {
for (DomainObjectA a : domainObjectARepository.getAllEntities()) {
// do cool stuff
}
}
}
不幸的是,Weld似乎不喜欢这种通用的注射。在部署时,出现以下错误:
state=Create: org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Repository<DomainObjectA , PersistableUUID>] with qualifiers [@Default] at injection point [[field] @Inject sompackage.DomainObjectARepositoryTest.domainObjectARepository]
我是否缺少某些东西,或者他们只是忘记了实现通用注入?据我了解,通用的东西,无论如何都要在编译后抹掉-到目前为止,即使它在guice3中也是如此。
亲切的问候,
avi
编辑:发现加文·金(Garvin King)的评论说,此行为在规范中,但未在焊缝中实现,(sta于2009年6月)
没有找到相关结果
已邀请:
1 个回复
膏焦凑
中删除
才能启动应用程序。 (b)注册一个扩展,并在部署过程中听
。那是我建议的方法,您可以在这里找到更多信息。 有了这个结果,您绝对应该能够确定是否有任何类型的Bean
徘徊:-) 如果您将结果报告回这里,并且还考虑在否定案例中提出Jira问题,那将很酷。