Aop事务管理

事务控制

导入jar包

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!--spring AOP的包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.1</version>
        </dependency>
    <!-- Spring 整合junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <!-- jdbcTemplate实现jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!-- 事务管理 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.3.1</version>
        </dependency>

    </dependencies>

注意:导入的jar包需要版本一致否则可能导致java.lang.NoSuchMethodError:org.springframework.core.KotlinDetector.isSuspendingFunction(Ljava/lang/reflect/Method;)Z 异常。

注解配置事务(使用JdbcTemplate)
<?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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.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">

    <!--导入资源文件-->
        <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
        <!-- 包扫描 -->
        <context:component-scan base-package="com.lqh"></context:component-scan>


        <!-- 开启基于Aop注解的功能-->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

		<!-- 配置数据源 -->
       <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
               <property name="driverClassName" value="${jdbc.driver}"></property>
               <property name="url" value="${jdbc.url}"></property>
               <property name="username" value="${jdbc.username}"></property>
               <property name="password" value="${jdbc.password}"></property>
       </bean>
		<!--jdbcTemplate 引入数据源 -->
        <bean class="org.springframework.jdbc.core.JdbcTemplate">
                <property name="dataSource" ref="dataSource"></property>
        </bean>

        <!-- 配置事务管理器 控制事务通过connection 也需要引入数据源-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
		<!-- 开启注解事务 -->
        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>



</beans>
事务xml配置

1、配置事务管理器,注入数据源
2、配置事务的通知,需要导入事务的约束
配置事务的通知标签:tx:advice,tx名称空间和约束,同时也需要aop的
使用tx:advice标签配置事务通知,属性
id:给事务通知起一个唯一标识
transaction-Manager:给事务通知提供一个事务管理器引用
3、配置aop中的通用切入点表达式
4、建立切入点表达式和事务通知的对应关系
5、配置事务的属性
在事务的通知tx:advice标签内部
isolation=“” :事务的隔离级别,默认值是DEFAULT,表示使用数据库的默认隔离级别
propagation=“” :事务的传播行为,默认值是REQUIRED,表示一定会有事务,增删改的选择,查询方法可以选择SUPPORTS
read-only=“” :事务是否只读,只有查询方法才能设置为true,默认值是false,表示读写
timeout=“”:事务的超时时间,默认值是-1,表示永不超时,如果指定了数值,以秒为单位
rollback-for=“” :指定一个异常,当该异常发生时,事务回滚,产生其他异常时,事务不回滚。没有默认值,表示任何异常都回滚
no-rollback-for=“” :与rollback-for=""相反,指定一个异常,当该异常发生时,事务不回滚,产生其他异常时,事务回滚,没有默认值,表示任何异常都回滚.

<?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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <!-- 包扫描 -->
    <context:component-scan base-package="com.lqh"></context:component-scan>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <bean class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置事务通知 -->
    <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="buyBook"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:advisor advice-ref="tx" pointcut="execution(* com.lqh.service.serviceimp.BookServiceImp.*(..))"></aop:advisor>
    </aop:config>

</beans>

两者配置区别:annotation 通过@Transactional 可以直接直接定位通知和切入点

​ xml需要通过<tx:advice> 设置通知 和<aop:advisor> 将通知带入切入点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值