白话Spring(基础篇)---声明式事务(2)
来源:互联网 发布:影视飞飞cms 编辑:程序博客网 时间:2024/05/19 00:14
[一知半解,就是给自己挖坑]
在上一篇,我们演示了通过编程式的方式,进行事务管理。我们发现这种配置方式破坏了我们倡导的AOP切面变成,因此,我们来看一下如何通过声明的方式来实现Spring事务管理。来达到我们AOP切面的目的。本节内容使用到了SpringAOP的部分内容,如果读者对此有疑问,在参考前面我们的叙述及实例。再来阅读本文内容。
准备工作:
a.操作系统:win7 x64
b.开发工具:eclipse mars j2ee版本,maven3.3.2,Spring 4,junit4.12,mybatis
c.复制Spring10,重命名为Spring11。工程基本结构如下图:
------------------------------------------------------------------------------------------------------------------------------------------------------
正文开始:
1.修改BankServiceImpl.java文件,具体内容如下:
package com.java.ingo.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.java.ingo.dao.BankDao;import com.java.ingo.entity.Account;import com.java.ingo.service.IBankService;/** * @author 作者 E-mail:ingo * @version 创建时间:2016年3月6日下午1:06:05 类说明 */@Servicepublic class BankServiceImpl implements IBankService {@Autowiredprivate BankDao bankDao;public void setBankDao(BankDao bankDao) {this.bankDao = bankDao;}public void transferMoney(int countA, int countB, int money) {Account accountA = new Account(countA, money);Account accountB = new Account(countB, money);bankDao.updateOut(accountA);// System.out.println(1/0);bankDao.updateIn(accountB);}}2.修改beans.xml文件,具体内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:component-scan base-package="com.java" /><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/db_bank" /><property name="username" value="root" /><property name="password" value="1234" /></bean><!-- 配置mybatis的sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mappers.xml文件 --><property name="mapperLocations" value="classpath:mappers/*.xml"></property></bean><!-- DAO接口所在包名,Spring会自动查找其下的类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.java.ingo.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- jdbc事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><bean id="namedParameterJdbcTemplate"class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"><constructor-arg ref="dataSource"></constructor-arg></bean><bean id="bankService" class="com.java.ingo.service.impl.BankServiceImpl"></bean><!-- 配置事务通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="*"/></tx:attributes></tx:advice><!-- 配置事务切面 --><aop:config><aop:pointcut id="serviceMethod" expression="execution(* com.java.ingo.service.*.*(..))"/><!-- 配置事务通知 --><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/></aop:config></beans>解释如下:
a.增加事务通知advice命名空间。xmlns:tx="http://www.springframework.org/schema/tx
b.针对方法配置事务,<tx:method name="*"/>表示所有方法都作为事务处理
c.配置事务切面,包括切点,及通知属性。
3.测试方法:运行main方法,然后刷新数据库,观察数据库变化。
package com.java.ingo.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.transaction.annotation.Transactional;import com.java.ingo.dao.BankDao;import com.java.ingo.entity.Account;import com.java.ingo.service.IBankService;/** * @author 作者 E-mail:ingo * @version 创建时间:2016年3月6日下午1:06:05 类说明 */@Transactionalpublic class BankServiceImpl implements IBankService {@Autowiredprivate BankDao bankDao;public void setBankDao(BankDao bankDao) {this.bankDao = bankDao;}public void transferMoney(int countA, int countB, int money) {Account accountA = new Account(countA, money);Account accountB = new Account(countB, money);bankDao.updateOut(accountA);// System.out.println(1/0);bankDao.updateIn(accountB);}}
3.修改beans.xml文件,具体内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <context:component-scan base-package="com.java" /><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/db_bank" /><property name="username" value="root" /><property name="password" value="1234" /></bean><!-- 配置mybatis的sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mappers.xml文件 --><property name="mapperLocations" value="classpath:mappers/*.xml"></property></bean><!-- DAO接口所在包名,Spring会自动查找其下的类 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.java.ingo.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean><!-- jdbc事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><bean id="namedParameterJdbcTemplate"class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"><constructor-arg ref="dataSource"></constructor-arg></bean><bean id="bankService" class="com.java.ingo.service.impl.BankServiceImpl"></bean><!-- 织入事务管理器 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>
4.测试方法:运行main方法,然后刷新数据库,观察数据库变化。
参考资料:
Spring官网:http://spring.io/docs
Mybatis官方文档:官方文档- 白话Spring(基础篇)---声明式事务(2)
- 白话Spring(基础篇)---编程式事务(1)
- 白话Spring(基础篇)---事务传播行为与隔离级别
- 白话Spring(基础篇)---helloworld
- 白话Spring(基础篇)---IOC
- 白话Spring(基础篇)---依赖注入
- 白话Spring(基础篇)---参数注入
- 白话Spring(基础篇)---自动装配
- 【spring基础】spring声明式事务详解
- 白话Spring(基础篇)---AOP(实例篇-2)
- spring声明式事务(一)
- spring声明式事务(二)
- spring声明式事务(三)
- spring声明式事务(四)
- spring声明式事务(五)
- spring声明式事务(六)
- (十一) Spring 声明式事务
- Spring框架-声明式事务(1)
- 深入使用Spring(一)
- eclipse+SVN文件只显示版本号,不显示时间和作者解决办法
- MySQL存储引擎对比
- php 学习笔记 -- 日期(四)
- 一个不错的android学习网站
- 白话Spring(基础篇)---声明式事务(2)
- mysql-5.7.11-winx64.zip解压缩安装时root的默认密码
- android contentprovider limit限定
- hiho 1271 舰队游戏 状压dp 贪心 模拟
- C++中的RTTI机制详解
- UIKeyboardType
- Android Studio配置SVN服务器
- ffmpeg推流器
- sql批量替换字段里的字符串