spring小结二(二)

2014-11-24 11:07:22 · 作者: · 浏览: 1
http://www.springframework.org/schema/context/spring-context-3.0.xsd
*** http://www.springframework.org/schema/tx
*** http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
* 以注解方式配置(命名空间略)
[html]
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
//Dao层代码
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
//...
}
//service层代码
@Service("accountService")
@Transactional(readOnly=true)
public class AccountServiceImpl implements AccountService {
@Resource(name="accountDao")
private AccountDao accountDao;
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public void saveAccount(Account account) throws Exception {
//...
}
}
注意:方法的事务设置将被优先执行.也就是说方法的事务声明优先于类级别的事务设置.
2.Hibernate+Hibernate
(1)xml文件形式
[html]
classpath:hibernate.cfg.xml
(2)注解形式
* 在配置文件中引入spring的自动扫描机制
* 在配置文件中引入注解解析器
* 在service层通过@Transaction进行注解
6.struts2+spring+hibernate整合
(1)添加需要的jar包
(2)配置web.xml
[html]
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring/applicationContext.xml
OpenSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
OpenSessionInViewFilter
*.action
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
(3)OpenSessionInView
由于使用的是spring的声明式事务处理方式,所以在调用this.getHibernateTemplate().load方法时,
使用了hibernate的懒加载技术。当把一个实体Bean从 数据库中加载完以后,只能加载其ID值。
这个时候spring的声明式事务处理方式已经把session给关闭掉了。所以当值在页面输出时会产生异常