我需要大脑! :P
我有一个IDAO接口< T,K>,T是实体类,K是主键类。
我开发了一个具体类GenericDAO< T,K>,提供CRUD操作和Criteria Queries自动提取(我创建了一个方面@Joinfetch,我在权利关系上使用)。如果你想要我可以显示代码,但它不是主题:)。
这里生产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;
}
}
有时我需要DAO的另一个实现(例如复杂的提取)。这些实现也通过IDAO外观公开它们的方法。我希望那些DAO的注入方式与GenericDAO相同。
以下是我想要的功能:
我需要注入IDAO的实现&lt;人,长&gt;某处。 如果存在,则类实现IDAO&lt;人,长&gt;那么我希望CDI选择这个实现。 否则,如果IDAO没有实现,则&lt;人,长&gt;我希望CDI选择GenericDAO&lt;人,长&gt;由工厂生产。
我很难用英语解释。我希望你能理解我的问题。
你有最好的做法吗?
谢谢!
答案 0 :(得分:1)
我需要两个@Qualifier
@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);
}
IDefaultDAO
public interface IDefaultDAO<T, PK> extends IDAO<T, PK> {
public void setEntityClass(final Class<T> entityClass);
}
摘要DAO
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;
}
}
}