我实际上看到了this question,但不能从中得到很多,所以我将尽我所能.
我的多用户网络应用程序中有BaseDAO类,如下所示:
public abstract class BaseDAO<GenType>
{
private HibernateOperations hibernateTemplate;
protected BaseDAO() {}
protected HibernateOperations getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateOperations hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
protected void initialize(final Object proxy) throws DataAccessException {
hibernateTemplate.initialize(proxy);
}
public GenType merge(GenType entity) throws DataAccessException {
return (GenType)hibernateTemplate.merge(entity);
}
protected void persist(GenType entity) throws DataAccessException {
hibernateTemplate.persist(entity);
}
public void refresh(GenType entity) throws DataAccessException {
hibernateTemplate.refresh(entity);
}
public void save(GenType entity) throws DataAccessException {
hibernateTemplate.save(entity);
}
public void saveOrUpdate(GenType entity) throws DataAccessException {
hibernateTemplate.saveOrUpdate(entity);
}
public void update(GenType entity) throws DataAccessException {
hibernateTemplate.update(entity);
}
public void delete(GenType entity) throws DataAccessException {
hibernateTemplate.delete(entity);
}
protected void deleteAll(Collection<GenType> entities) throws DataAccessException {
hibernateTemplate.deleteAll(entities);
}
protected GenType get(Class<GenType> entityClass,Serializable id) throws DataAccessException {
return (GenType)hibernateTemplate.get(entityClass,id);
}
}
它基本上是HibernateTemplate的包装器.我所有其他DAO都继承了此类并实现了适当的接口,这些接口包含一些其他方法(如getBySomeAttribute()).因此,基本上,这些DAO只有方法.此外,我还有包装DAO的服务层.也就是说,一个服务类可以容纳多个DAO,并且通过spring-AOP截获来自服务层的方法调用以进行自动提交/回滚(事务划分).例如:
public class ModelDAO extends BaseDAO<Model> implements IModelDAO
{
@Override
public Model getNew() {
return new Model();
}
@Override
public List<Model> getBySomeAttr() {
DetachedCriteria criteria;
// define some criteria
return getHibernateTemplate().findByCriteria(criteria);
}
}
...
public class ModelService
{
private ModelDAO modelDAO;
private ElementDAO elementDAO;
// GET/SET for model and user DAO
public void doSomethingWithModel() {
modelDAO.doSomething();
elementDAO.doSomethingElse();
}
}
配置如下:
<bean id="hibernateTemplate2" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory2" />
<property name="maxResults" value="3000" />
</bean>
<!-- DAO -->
<bean id="baseDAO" abstract="true"
class="org.irvas.backend.dao_implement.BaseDAO" scope="session">
<property name="hibernateTemplate" ref="hibernateTemplate2" />
</bean>
<bean id="modelDAO"
class="org.irvas.backend.dao_implement.ModelDAOImplement"
parent="baseDAO" scope="session">
</bean>
<bean id="elementDAO"
class="org.irvas.backend.dao_implement.ElementDAOImplement"
parent="baseDAO" scope="session">
</bean>
<!-- Service -->
<bean id="modelService"
class="org.irvas.backend.service_implement.ModelServiceImplement" scope="session">
<property name="modelDAO" ref="modelDAO" />
<property name="elementDAO" ref="elementDAO" />
</bean>
<bean id="elementService"
class="org.irvas.backend.service_implement.ElementServiceImplement" scope="session">
<property name="elementDAO" ref="elementDAO" />
</bean>
因此,我想知道如何将DAO / Service bean的作用域用于多用户用途?我正在将服务bean注入GUI的原型控制器中.
当我使用此配置(使用scope =“ session”)时,出现如下错误:
Error creating bean with name ‘modelService’: java.lang.IllegalArgumentException setAttribute: Non-serializable attribute: modelDAO
我想说这是从Tomcat的StandardSession.setAttribute()中抛出的.由此我可以得出结论,我的DAO应该实现Serializable,而令我更困惑的是,我看到了完全相同的代码却没有实现Serializable …
如果有人可以启发我这里发生了什么,以及如何针对这种特殊情况确定DAO和服务的范围,我将不胜感激…
最佳答案
原文链接:https://www.f2er.com/spring/531698.html