在web.xml中添加了OpenSessionInViewFilter依然有延迟加载的错误

web.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

OpenSessionInViewFilter
org.springframework.orm.hibernate.support.OpenSessionInViewFilter


OpenSessionInViewFilter
/*


contextConfigLocation
classpath:applicationContext.xml


org.springframework.web.context.ContextLoaderListener


index.jsp


struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



struts2
/*

struts.xml


<!-- 交给spring来配置 -->


DAO层:
@Repository
public class CommonDaoImpl implements ICommonDao
{
private HibernateTemplate hibernateTemplate;
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public List queryAll(String hql)
{
return this.hibernateTemplate.find(hql);
}
Biz层:
@Service
public class CrmBizImpl implements ICrmBiz
{
private ICommonDao sysRightDao;
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public List queryAllRight()
{
return sysRightDao.queryAll("Form SysRight");
}
@Resource
public void setSysRightDao(ICommonDao sysRightDao)
{
this.sysRightDao = sysRightDao;
}

}
Test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class JTest extends AbstractJUnit4SpringContextTests
{
@Resource
private CrmAction crmAction;
@Test
public void test()
{
........
}
在我用spring test+junit进行测试的时候是 no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize

这个应该不是你的配置问题而是你的代码问题;而且用OpenSessionInViewFilter也不是最好的选择;当你页面量很大而且网站访问量很大时,一打开渲染页面
就让session开着,这对数据库造成很大的压力的;通常的做法是在你的事务类中强行的初始化页面,也就是在这个类里写一句废话让代理立即执行就行了;