[一知半解,就是给自己挖坑]
在上一篇,我们演示了通过编程式的方式,进行事务管理。我们发现这种配置方式破坏了我们倡导的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 类说明
*/
@Service
public class BankServiceImpl implements IBankService {
@Autowired
private 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 类说明
*/
@Transactional
public class BankServiceImpl implements IBankService {
@Autowired
private 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方法,然后刷新数据库,观察数据库变化。