配置事务管理器
编程式事务管理: 要修改原来的代码,加入事务管理代码 (侵入性 )— 不推荐,不使用
声明式事务管理:底层就是AOP的环绕通知, — 推荐
用XML配置方式添加事务管理(tx、aop约束)
第一步: 引入aop和tx 的名称空间,导入aop和tx 的jar
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
</beans>
第二步: 配置 transactionManager(spring提供 Around 通知 )
根据不同的持久层框架,配置不同的事务管理器,事务管理器需要依赖数据源信息
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入驱动-->
<property name="dataSource" ref="dataSource"></property>
</bean>
第三步: 配置切入点和切面
1、在spring的配置文件中,使用tx:advice配置通知信息,其实就是哪些需要方法需要增强,事务管理也是aop的应用
<tx:advice>
<tx:attributes>
<tx:method name="具体的方法名"/>
</tx:attributes>
</tx:advice>
2、使用aop:config来配置切入点和切面信息
<!-- 2、配置切入点,你要对哪一个方法进行增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="updateMoney"/>
</tx:attributes>
</tx:advice>
<!-- 3、配置切面-->
<aop:config>
<!-- 配置切入点表达式-->
<aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>
<!-- 切面配置,切入点和通知进行一个结合-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCat"></aop:advisor>
</aop:config>
用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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 配置注解扫描包:扫描com.sky包下的所有组件-->
<context:component-scan base-package="com.sky"></context:component-scan>
<!-- 核心思想:将Mybatis的所有对象全部都交给Spring管理-->
<!-- 引入数据源信息-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- location值就需要加上classpath, classpath = src-->
<property name="location" value="classpath:db.properties"></property>
</bean>
<!-- 配置数据源,数据库链接池,dbcp,c3p0-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- sqlSessionFactory:加载工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation:加载 Mybatis 的配置文件-->
<property name="configLocation" value="classpath:MybatisConfig.xml"></property>
<!-- 扫描加载所有的mapper.xml文件-->
<property name="mapperLocations" value="classpath:com/sky/mapperxml/*.xml"></property>
</bean>
<!-- sqlSession-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 配置接口的扫描包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sky.mapper"></property>
</bean>
<!-- *****-->
<!-- *****-->
<!-- 1、配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入驱动-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2、配置切入点,你要对哪一个方法进行增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="updateMoney"/>
</tx:attributes>
</tx:advice>
<!-- 3、配置切面-->
<aop:config>
<!-- 配置切入点表达式-->
<aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>
<!-- 切面配置,切入点和通知进行一个结合-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCat"></aop:advisor>
</aop:config>
</beans>
注解配合方式添加事务管理 @Transactional
第一步: 配置注解驱动事务管理
<!-- 1、配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入驱动-->
<property name="dataSource" ref="dataSource"></property>
</bean>
第二步: 在需要管理事务的方法或者类上面 添加@Transactional 注解
<!-- 2、事务管理器的注解驱动配置,加了这个配置就能识别 Transactional 注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
用注解配合方式添加事务管理代码
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 配置注解扫描包:扫描com.sky包下的所有组件-->
<context:component-scan base-package="com.sky"></context:component-scan>
<!-- 核心思想:将Mybatis的所有对象全部都交给Spring管理-->
<!-- 引入数据源信息-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- location值就需要加上classpath, classpath = src-->
<property name="location" value="classpath:db.properties"></property>
</bean>
<!-- 配置数据源,数据库链接池,dbcp,c3p0-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- sqlSessionFactory:加载工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- configLocation:加载 Mybatis 的配置文件-->
<property name="configLocation" value="classpath:MybatisConfig.xml"></property>
<!-- 扫描加载所有的mapper.xml文件-->
<property name="mapperLocations" value="classpath:com/sky/mapperxml/*.xml"></property>
</bean>
<!-- sqlSession-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 配置接口的扫描包-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sky.mapper"></property>
</bean>
<!-- *****-->
<!-- *****-->
<!-- 1、配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入驱动-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2、事务管理器的注解驱动配置,加了这个配置就能识别 Transactional 注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--<!– 配置切入点,你要对哪一个方法进行增强–>-->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!-- <tx:attributes>-->
<!-- <tx:method name="updateMoney"/>-->
<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!--<!– 配置切面–>-->
<!-- <aop:config>-->
<!--<!– 配置切入点表达式–>-->
<!-- <aop:pointcut id="pointCat" expression="execution(* com.sky.service.*.*(..))"/>-->
<!--<!– 切面配置,切入点和通知进行一个结合–>-->
<!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCat"></aop:advisor>-->
<!-- </aop:config>-->
</beans>
事务管理器增强的方法
package com.sky.service;
import com.sky.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountService {
@Autowired
private AccountMapper accountMapper;
@Transactional // 添加事务管理
public void updateMoney(String numberOut,String numberIn,double money){
accountMapper.getMoneyOut(numberOut,money);
System.out.println(1/0); // 这行代码有异常
accountMapper.getMoneyIn(numberIn,money);
}
}
问题: XML配置方式和注解配置方式 进行事务管理 哪种用的多?
XML方式 和注解都在使用:
使用@Transactional注解进行事务管理,方便,不过配置太分散, 使用XML进行事务管理 属性集中配置,便于管理和维护
<tx:advice id="txadvice">
<tx:attributes>
<!-- 例:增强所有add开头的方法-->
<tx:method name="add*"/>
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
</tx:attributes>
</tx:advice>

1402

被折叠的 条评论
为什么被折叠?



