一些 Spring 事务的核心配置与代码片段
核心配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
>
<!-- ============== 基本Bean ============== -->
<bean id="BankServiceImpl" class="com.project.service.impl.BankServiceImpl">
<property name="dao" ref="IBankDao"></property>
</bean>
<!-- ============== 整合Mybatis ============== -->
<!-- 配置连接数据库环境 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/mybaits"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<!-- 配置 mybatis 的 SqlSessionFactory 工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定环境 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定要加载的配置文件 -->
<property name="configLocation" value="classpath:config/mybatisConfig.xml"></property>
<!-- <property name="mapperLoactions" value="classpath:com/project/mapper/UserMapper.xml"></property> -->
</bean>
<!-- 创建数据映射器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.project.dao"></property>
</bean>
<!-- 创建事务管理器:针对jdbc或mybatis -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 指定环境 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用spring控制事务,使用动态代理实现 -->
<bean id="proxyTran" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 指定目标对象, 通过id指定 -->
<property name="target" ref="BankServiceImpl"></property>
<!-- 注入事务管理器, 通过id指定 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 设置事务属性 -->
<property name="transactionAttributes">
<props>
<!-- 指定方法,并设置传播行为与隔离级别 -->
<prop key="transferAccounts">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
</bean>
</beans>
目标对象
package com.project.service.impl;
import com.project.dao.IBankDao;
import com.project.exception.MyException;
import com.project.service.IBankService;
public class BankServiceImpl implements IBankService {
private IBankDao dao = null;
// 转账
@Override
public void transferAccounts (int destId, int srcId, int money) {
// 加钱
dao.updateMoneyAdd(destId, money);
// 模拟断电、死机情况引起的转账失败。
System.out.println(10 / 0);
// 减钱
dao.updateMoneyMinus(srcId, money);
}
public IBankDao getDao() {
return dao;
}
public void setDao(IBankDao dao) {
this.dao = dao;
}
}
测试
package com.project.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.project.exception.MyException;
import com.project.service.IBankService;
public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("config/springConfig.xml");
// 获取 BankService 的动态代理对象
IBankService service = (IBankService) context.getBean("proxyTran");
service.transferAccounts(2, 1, 1000);
}
}
小结:spring 的事务管理是通过动态代理实现的,所以我们应该获取BankService动态代理对象才会有事务,直接获取Bean是没有的。
用注解配置
xml 配置 片段
别的都跟上面一样
<!-- 创建事务管理器:针对jdbc或mybatis -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 指定环境 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解扫描 -->
<context:component-scan
base-package="com.project.service"></context:component-scan>
<!-- 注解方式实现事务: 指定注解方式实现事务 -->
<tx:annotation-driven
transaction-manager="transactionManager" />
注解
在要开启事务的方法上,添加注解就可以了,实现类,会把注解也继承过去。
public interface IBankService {
@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT)
public void transferAccounts(int destId, int srcId, int money);
}
网友评论