spring事务管理,xml配置aop事务和注解配置aop事务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了spring事务管理,xml配置aop事务和注解配置aop事务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7606字,纯文字阅读大概需要11分钟。
内容图文
xml配置和注解配合共同代码
AccountService.java
public interface AccountService { // 转账方法 void transfer(Integer from,Integer to,Double money); }
AccountServiceImpl.java
xml配置aop事务的AccountServiceImpl.java
public class AccountServiceImpl implements AccountService { private AccountDao ad ; @Overridepublicvoid transfer(final Integer from,final Integer to,final Double money) { //减钱 ad.decreaseMoney(from, money); int i = 1/0; //加钱 ad.increaseMoney(to, money); } publicvoid setAd(AccountDao ad) { this.ad = ad; } }
注解配置aop事务的AccountServiceImpl.java
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true) public class AccountServiceImpl implements AccountService { private AccountDao ad ; @Override @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) publicvoid transfer(final Integer from,final Integer to,final Double money) { //减钱 ad.decreaseMoney(from, money); int i = 1/0; //加钱 ad.increaseMoney(to, money); } publicvoid setAd(AccountDao ad) { this.ad = ad; } }
AccountDao.java
public interface AccountDao { // 加钱 void increaseMoney(Integer id,Double money); // 减钱 void decreaseMoney(Integer id,Double money); }
AccountDaoImpl.java
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { @Override publicvoid increaseMoney(Integer id, Double money) { //JdbcTemplate:spring整合jdbc操作数据库的类 getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id); } @Override publicvoid decreaseMoney(Integer id, Double money) { getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id); } }
xml配置aop事务的applicationContext.xml
<? xml version="1.0" encoding="UTF-8" ?> < beans xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://www.springframework.org/schema/beans" xmlns:context ="http://www.springframework.org/schema/context" xmlns:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd " > <!-- 指定spring读取db.properties配置 --> < context:property-placeholder location ="classpath:db.properties" /> <!-- 1.将连接池 --> < bean name ="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource" > < property name ="jdbcUrl" value ="${jdbc.jdbcUrl}" ></ property > < property name ="driverClass" value ="${jdbc.driverClass}" ></ property > < property name ="user" value ="${jdbc.user}" ></ property > < property name ="password" value ="${jdbc.password}" ></ property > </ bean > <!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 --> < bean name ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name ="dataSource" ref ="dataSource" ></ property > </ bean > <!-- 配置事务通知 --> < tx:advice id ="txAdvice" transaction-manager ="transactionManager" > < tx:attributes > <!-- 以方法为单位,指定方法应用什么事务属性 isolation:隔离级别 propagation:传播行为 read-only:是否只读 --> < tx:method name ="save*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="false" /> < tx:method name ="persist*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="false" /> < tx:method name ="update*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="false" /> < tx:method name ="modify*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="false" /> < tx:method name ="delete*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="false" /> < tx:method name ="remove*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="false" /> < tx:method name ="get*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="true" /> < tx:method name ="find*" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="true" /> < tx:method name ="transfer" isolation ="REPEATABLE_READ" propagation ="REQUIRED" read-only ="false" /> </ tx:attributes > </ tx:advice > <!-- 配置织入 --> < aop:config > <!-- 配置切点表达式 --> < aop:pointcut expression ="execution(* cn.itcast.service.*ServiceImpl.*(..))" id ="txPc" /> <!-- 配置切面 : 通知+切点 advice-ref:通知的名称 pointcut-ref:切点的名称 --> < aop:advisor advice-ref ="txAdvice" pointcut-ref ="txPc" /> </ aop:config > <!-- 2.Dao --> < bean name ="accountDao" class ="cn.itcast.dao.AccountDaoImpl" > < property name ="dataSource" ref ="dataSource" ></ property > </ bean > <!-- 3.Service --> < bean name ="accountService" class ="cn.itcast.service.AccountServiceImpl" > < property name ="ad" ref ="accountDao" ></ property > </ bean > </ beans >
注解配置aop事务的applicationContext.xml
<? xml version="1.0" encoding="UTF-8" ?> < beans xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://www.springframework.org/schema/beans" xmlns:context ="http://www.springframework.org/schema/context" xmlns:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd " > <!-- 指定spring读取db.properties配置 --> < context:property-placeholder location ="classpath:db.properties" /> <!-- 1.将连接池 --> < bean name ="dataSource" class ="com.mchange.v2.c3p0.ComboPooledDataSource" > < property name ="jdbcUrl" value ="${jdbc.jdbcUrl}" ></ property > < property name ="driverClass" value ="${jdbc.driverClass}" ></ property > < property name ="user" value ="${jdbc.user}" ></ property > < property name ="password" value ="${jdbc.password}" ></ property > </ bean > <!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 --> < bean name ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name ="dataSource" ref ="dataSource" ></ property > </ bean > <!-- 开启使用注解管理aop事务 --> <tx:annotation-driven /> <!-- 2.Dao --> < bean name ="accountDao" class ="cn.itcast.dao.AccountDaoImpl" > < property name ="dataSource" ref ="dataSource" ></ property > </ bean > <!-- 3.Service --> < bean name ="accountService" class ="cn.itcast.service.AccountServiceImpl" > < property name ="ad" ref ="accountDao" ></ property > </ bean > </ beans >
测试类Demo.java
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") publicclass Demo { @Resource(name="accountService") private AccountService as; @Test publicvoid fun1(){ as.transfer(1, 2, 100d); } }
原文:http://www.cnblogs.com/ms-grf/p/7449188.html
内容总结
以上是互联网集市为您收集整理的spring事务管理,xml配置aop事务和注解配置aop事务全部内容,希望文章能够帮你解决spring事务管理,xml配置aop事务和注解配置aop事务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。