欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Spring基础知识04

时间:2023-07-30

提示:这本文入门使用


文章目录

前言SpringAOP,事务

声明式事务和编程式事务 传播特性,隔离级别,超时时间,回滚设置


前言

提示:以下是本篇文章正文内容,下面案例可供参考

SpringAOP,事务

接上文,若是想只在xml中配置,如下。


before method=“start” pointcut"execution( Integer com.mashibing、service.MyCalculator.(…) " after method=“1ogFinally” pointcut="execution( Integer com.mashibing、service.MyCalculator.(.))‘X after-returning method"stop" pointcut="execution( Integer com.mashibing、service.MyCalculator.(…))" result=“e”
after-throwing method-"logException"pointcut-"execution( Integer com.mashibing、service.MyCalculator.
(…),exception=“e”
around method “around” pointcut="execution( Integer com.mashibing.service、MyCalculator.*(.) X/aop:arou’

与上文所言的效果一致,在实际公司项目中两者兼备,并无偏重。
也可以将切面抽象出来,作为每个切面内的私有引用。

@Test
public void test(){
JdbcTemplate jdbcTemplate = context、getBean( name:" jdbcTemplate",.class)
JdbcTemplate.String sql =“select * from emp where sal >?”;
List< Emp> result = jdbcTemplate、query(sql, new BeanPropertyRowMapper<>(Emp.class)
for (Emp emp : result){
System、out.println(emp);

声明式事务和编程式事务

在事务控制方面,主要有两个分类:
编程式事务∶在代码中直接加入处理事务的逻辑,可能需要在代码中显式调用beginTransaction()、 commit).rollback(等事务管理相关的方法
声明式事务:在方法的外部添加注解或者直接在配置文件中定义,将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。spring的AOP恰好可以完成此功能:事务管理代码的固定模式作为一种横切关注点,通过AOP方法模块化,进而实现声明式事务。
//导入事务管理模块

导入命名空间xmlns:tx= http://www.springframswork.orgs/schema/tx"http://www.sprinsframework..9ng.schema/txhttp://www.sprinsframework..9rg.schema/tx/spring-tx.xsd

@Transactional
public void buyBook(){
bookDao、getPrice( id: 1);
bookDao、updateBalance( userName:" zhangsan", price: 100);
int i= 1/0;
bookDao.updateStock( id: 1);
}

传播特性,隔离级别,超时时间,回滚设置

propagation:传播特性:
isolation:隔离级别:4种隔离级别,会引发不同的数据错乱问题
timeout:超时时间
readonly:只读事务,如果配置了只读事务,那么在事务运行期间,不允许对数据进行修改,否则抛出异常
//设置哪些异常不会回滚数据
noRollBackfor: noRollbackFor = {ArithmeticException.class}
设置哪些异常回滚
rollBackfor :
rollbackForClassName

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。