原来在此基础上螺纹:的Spring IoC和泛型接口类型,实现
Spring IoC and Generic Interface Type
而这一次
Write Less DAOs with Spring Hibernate using Annotations
我不知道如何处理实施前的想法。可以说我有
@Repository
@Transactional
public class GenericDaoImpl<T> implements GenericDao<T> {
@Autowired
private SessionFactory factory;
private Class<T> type;
public void persist(T entity){
factory.getCurrentSession().persist(entity);
}
@SuppressWarnings("unchecked")
public T merge(T entity){
return (T) factory.getCurrentSession().merge(entity);
}
public void saveOrUpdate(T entity){
factory.getCurrentSession().merge(entity);
}
public void delete(T entity){
factory.getCurrentSession().delete(entity);
}
@SuppressWarnings("unchecked")
public T findById(long id){
return (T) factory.getCurrentSession().get(type, id);
}
}
而我是O.K.具有标记接口:
public interface CarDao extends GenericDao<Car> {}
public interface LeaseDao extends GenericDao<Lease> {}
但我想通过1个GenericDaoImpl漏斗实施细则(像上面的GenericDaoImpl),以避免书写复制默认地将Impl(个),简单的CRUD。 (我会写自定义默认地将Impl(个),这需要更先进的DAO功能实体。)
所以后来终于在我的控制,我可以这样做:
CarDao carDao = appContext.getBean("carDao");
LeaseDao leaseDao = appContext.getBean("leaseDao");
这可能吗?我需要做什么才能做到这一点?
难道你不能只创建两个类:'@Repository CarDaoImpl extends GenericDaoImpl'和'@Repository LeaseDaoImpl extends GenericDaoImpl '。因此所有的实现细节都在父类中,或者我错过了什么? –
matsev
不知道为什么我没有想到昨天。 – sloven