【发布时间】:2011-09-19 22:35:49
【问题描述】:
我刚从我小小的 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 的 comment 表明此行为在规范中,但未在焊接中实现,(状态为 2009 年 6 月)
【问题讨论】:
-
很好的发现,关于评论:-)
-
ty @jan。好吧,既然这个评论还不到两年,我希望weld 可以一直实施到现在
-
嗨@avithan,你是如何在 Guice 中做到这一点的?尤其是
Provider<E> domainObjectProvider部分。您是否为每个X手动绑定Provider<X>? -
嗨@irreputable。如果我没记错的话,每种类型都有一个默认提供程序,它使用默认构造函数或使用
@Inject注释的构造函数。所以不需要绑定实体类型。
标签: java-ee-6 cdi jboss-weld genericdao