Current I have a seperate maven module for my database access, all my DAO classes inherit from:
public class GenericDaoImpl<T> extends JdbcDaoSupport implements GenericDao<T> {
}
My maven module has a spring dependancy:
org.springframework spring-orm
So a typical Dao class looks like:
public class UserDaoImpl extends GenericDaoImpl<User> implements UserDao {
@Override
public void insert(User user) {
getJdbcTemplate().update("insert into users(...)...");
}
}
My Dao’s get autowired with the dataSource bean.
Can I make this generic somehow so I can continue to use it in my spring MVC application, yet it will work if I need to use this library in a cron job service type environment? (without having to bring in spring’s application context into the picture).
I’d start to observe the following:
GenericDao<T>which means that you can have different implementations.GenericDaoImpl<T> extends JdbcDaoSupportwhich means that you cannot use in the other environment you described without any application context unless you prepare all the object in a manual fashion.So my suggestion is:
GenericDao<T> extends org.springframework.jdbc.core.JdbcOperationsAbstractGenericDao<T> implements GenericDao<T>to bring the abstract general functionality you need.MyEnvGenericDao<T> extends AbstractGenericDao<T>that is responsible to provide a data source and underlying implementation of different methods you need; could be using a direct Hibernate/OpenJPA implementation to directly executing the queries.SpringGenericDao<T> extends JdbcDaoSupport implements GenericDao<T>that comes is already with agetJdbcTemplate()to perform the operations you need and is delegated to Spring JDBC template. In this scenario, you need to delegate the operations toJdbcDaoSupport.getJdbcTemplate().Related to Maven, then you can actually have different modules for
MyEnvandSpringimplementations but both with one parent to have access toGenericDao<T>interface.