Originally based on this thread:
Spring IoC and Generic Interface Type
and this one
Write Less DAOs with Spring Hibernate using Annotations
I'm wondering how to approach implementation of the former's idea. Lets say I've got
@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);
}
}
and I am O.K. with having marker interfaces:
public interface CarDao extends GenericDao<Car> {}
public interface LeaseDao extends GenericDao<Lease> {}
But I want to funnel the implementation details through 1 GenericDaoImpl (something like above's GenericDaoImpl), to avoid writing duplicate Impl(s) for simple CRUD. (I would write custom Impl(s) for Entities which require more advanced DAO functionality.)
So then eventually in my controller I can do:
CarDao carDao = appContext.getBean("carDao");
LeaseDao leaseDao = appContext.getBean("leaseDao");
Is this possible? What would I need to do to achieve this?