我需要大脑!:p
我有一个接口IDAO< T,K >,T是实体类,K是主键类。
我已经开发了一个具体的类Joinfetch,K >,它提供了带有自动抓取的CRUD操作和标准查询(我已经创建了一个方面@ GenericDAO<,我用在实体关系上)。如果您愿意,我可以显示代码,但它不是主题:)。
这里是生成GenericDAO的工厂:
public class GenericDAOProducer {
@PersistenceContext
EntityManager em;
@Produces
@Default
public <T, K> IDAO<T, K> producesGenericDAO(
final InjectionPoint injectionPoint) {
final ParameterizedType type = (ParameterizedType) injectionPoint
.getType();
// Because of the new, i got to inject the class and the entity manager
// manualy
final IDAO<T,K> dao = new GenericDAO<T, K>(this.em,
(Class) type.getActualTypeArguments()[0]);
dao.init(); //cool stuff here to prepare some criteria queries
return dao;
}
}有时我需要DAOs的另一个实现(例如复杂的抓取)。这些实现也通过IDAO外观公开了它们的方法。我希望这些DAO以与GenericDAO相同的方式注入。
下面是我在示例中想要的特性:
我需要一个注射的实现IDAO<的人,Long>某处。如果存在实现CDI,Long>的类,那么我希望IDAO<选择这个实现。否则,如果IDAO< Person没有实现,我希望Long>选择GenericDAO< Person,Long > produced by the Factory。
这对我来说很难用英语来解释。我希望你能理解我的问题。
你有这样做的最佳实践吗?
谢谢!
发布于 2015-03-14 09:26:26
我需要两个@限定符
@Qualifier
@Retention(RUNTIME)
@Target({ METHOD, FIELD, PARAMETER, TYPE })
public @interface Generic {}
@Qualifier
@Retention(RUNTIME)
@Target({ METHOD, FIELD, PARAMETER, TYPE })
public @interface Specific {
Class<?> classe();
}IDAO接口
public interface IDAO<T, PK> extends Serializable {
public abstract T create(T entity) throws DAOException;
public abstract T edit(T entity) throws DAOException;
public abstract void remove(T entity) throws DAOException;
public abstract T find(PK id) throws DAOException;
public abstract T findWithFetch(PK id) throws DAOException;
public abstract List<T> findAll();
public abstract T getRef(PK id);
}The IDefaultDAO
public interface IDefaultDAO<T, PK> extends IDAO<T, PK> {
public void setEntityClass(final Class<T> entityClass);
}抽象的道
public abstract class AbstractDAO<T, PK> implements IDAO<T, PK> {
@PersistenceContext
protected EntityManager em;
@Inject
protected transient Logger logger;
protected Class<T> entityClass;
protected final Class<T> getEntityClass() {
if (this.entityClass == null) {
this.entityClass = ((Class) ((ParameterizedType) this.getClass()
.getGenericSuperclass()).getActualTypeArguments()[0]);
}
return this.entityClass;
}
//methods implementations
}默认DAO
@Generic
public class DefaultDAO<T, PK> extends AbstractDAO<T, PK> implements
IDefaultDAO<T, PK> {
@Override
public void setEntityClass(final Class<T> entityClass) {
this.entityClass = entityClass;
}
}一个特定的DAO
@Specific(classe=Delegation.class)
public class DelegationDAO extends AbstractDAO<Delegation, Integer>
implements IDAO<Delegation, Integer> {
// do things differently
}DAO制作人
public class GenericDAOProducer {
@Inject
private transient Logger logger;
@Produces
public <T, PK> IDAO<T, PK> producesDAO(final InjectionPoint injectionPoint,
@Generic final IDefaultDAO<T, PK> genericDAO,
@Any Instance<IDAO<T, PK>> specDAOInstance) {
// JPA Class (T)
final ParameterizedType type = (ParameterizedType) injectionPoint
.getType();
final Class<T> entityClass = (Class) type.getActualTypeArguments()[0];
this.logger.info("Search DAO " + entityClass);
// Search specific DAO
specDAOInstance = specDAOInstance.select(new SpecificLiteral(
entityClass));
if ((specDAOInstance != null) && !specDAOInstance.isAmbiguous()
&& !specDAOInstance.isUnsatisfied()) {
this.logger.info("Implementation found! ");
return specDAOInstance.get();
} else {
this.logger
.info("Implementation not found! Return generic DAO."
+ entityClass);
genericDAO.setEntityClass(entityClass);
return genericDAO;
}
}
}https://stackoverflow.com/questions/29029827
复制相似问题