为了更加明确,可能贴的代码有点长,请见谅。
这里主要是打算应用泛型来做一些通常基本的CRUD操作,但是注入的时候出了点点问题。
我把基类部分注释掉以后可以正常工作,但是有一个问题,我每个具体的action类又必须重写BaseAction中的方法,因为manager没有得到注入,这样失去了使用泛型的意义。我把注释去掉以后在BaseAction中的“manager”无法按期望的方式的初始化。因为manager中的属性“persistentClass”永远都是在manager bean配置时设置的值,而不能变化,泛型就是需要变,我期望的是manager中该属性根据BusinessFlowDAO的构造注入时的参数而改变,该怎么做呢?
附:我参考的一篇文章 http://blog.csdn.net/brodie/archive/2007/08/14/1743275.aspx
下边是我的代码:
1、接口genericDao.java
public interface GenericDAO {
T findById(ID id, boolean lock);
List findAll();
}
2、实现GenericHibernateDAO.java
public class GenericHibernateDAO extends
HibernateDaoSupport implements GenericDAO {
private Class persistentClass;
public GenericHibernateDAO() {
}
public GenericHibernateDAO(Class persistentClass) {
logger.debug("base class constructor " + persistentClass);
this.persistentClass = persistentClass;
}
public Class getPersistentClass() {
return persistentClass;
}
public void setPersistentClass(Class persistentClass) {
this.persistentClass = persistentClass;
logger.debug("persistentClass " + this.persistentClass);
}
@SuppressWarnings("unchecked")
public T findById(ID id, boolean lock) {
T entity;
if (lock)
// this.getHibernateTemplate().load(this.getPersistentClass(), id);
entity = (T) getSession().get(getPersistentClass(), id,
LockMode.UPGRADE);
else
entity = (T) getSession().get(getPersistentClass(), id);
return entity;
}
@SuppressWarnings("unchecked")
public List findAll() {
return findByCriteria();
}
3、service接口Manager.java
public interface Manager {
public T findById(ID id, boolean lock);
public List findAll();
}
4、service实现ManagerImpl.java
public class ManagerImpl implements Manager {
protected final Logger logger = Logger.getLogger(ManagerImpl.class);
private GenericDAO genericDAO;
@SuppressWarnings("unchecked")
public T findById(ID id, boolean lock) {
return (T) this.genericDAO.findById(id, lock);
}
@SuppressWarnings("unchecked")
public List findAll() {
return (List) this.genericDAO.findAll();
}
}
5、扩展的业务Dao、service
public interface BusinessFlowDAO extends GenericDAO{
//TODO: add other method for business logic related to BusinessFlow
}
public class BusinessFlowDAOHibernate extends
GenericHibernateDAO implements BusinessFlowDAO {
public BusinessFlowDAOHibernate(Class persistentClass) {
super(persistentClass);
logger.debug("child class constructor " + persistentClass);
}
}
public interface BusinessFlowManager extends Manager{
}
public class BusinessFlowManagerImpl extends ManagerImpl
implements BusinessFlowManager {
private BusinessFlowDAO businessFlowDAO;
public BusinessFlowDAO getBusinessFlowDAO() {
return businessFlowDAO;
}
public void setBusinessFlowDAO(BusinessFlowDAO businessFlowDAO) {
this.businessFlowDAO = businessFlowDAO;
super.setGenericDAO(businessFlowDAO);
}
}
baseAction如下:
public class BaseAction, TForm, ID extends Serializable>
extends DispatchAction {
protected org.apache.log4j.Logger logger = org.apache.log4j.Logger
.getLogger("action");
public T instance;
public TForm tForm;
protected Manager manager;
@SuppressWarnings("unchecked")
public ActionForward create(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
TForm tf = (TForm) form;
try {
instance = (T) ConvertUtil.convert(tf);
this.manager.makePersistent(instance);
} catch (Exception e) {
e.printStackTrace();
logger.debug("create object exception " + e.toString());
}
return null;
}
......
}
6、配置注入的代码 applicationContext.xml
com.wonders.persistence.config.BusinessFlow
。。。。。。