Spring的AOP中的声明式事务

本文介绍Spring框架中的三种事务管理配置方法:XML配置、注解配置及纯注解配置(NoXML)。涵盖数据源配置、事务管理器声明、AOP配置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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: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
    http://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:component-scan base-package="com.xx"/>

    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/xxx"/>
        <property name="username" value="root"/>
        <property name="password" value="xxxx"/>
    </bean>

    <!--1、新建数据源事务管理器对象,指定引用为transactionManager,添加完成数据源属性,然后存于spring容器中-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="ds"/>
    </bean>
    <!--2、在spring容器中,利用id引入上面添加的数据源事务管理器的对象(transactionManager为此处默认接收值,可不写),
    新建id为txAdvice的事务,并设置内部属性-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--内部属性主要是针对不同的方法,进行事务属性参数设定-->
        <tx:attributes>
            <!--利用通配符,设定只要以select为前缀的方法的事务,均为只读,且事务传递方式为supports(外部有事务就用,没有就不用)-->
            <tx:method name="select*" read-only="true" propagation="SUPPORTS"/>
            <!--设定其余方法的事务(由于范围大于上面的方法,所以优先级小于上面的优先级),默认不是只读,且事务传递方式为required(外部有事务就利用,没有就自己新建一个事务)-->
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <!--3、开辟aop代码区域-->
    <aop:config>
        <!--4、定义切入点,包含被代理方法,下面的AOP表达式,表示返回值为*(任意),且切入点为com包下fh包下service包下impl包下的*(所有类)的*(所有方法)且该方法可接受..(任意参数)-->
        <aop:pointcut id="pcr" expression="execution(* com.xx.service.impl.*.*(..))"/>
        <!--5、将上面的事务和切入点完成织入-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pcr"/>
    </aop:config>


</beans>

二、注解配置方法(最简单、最常用):
xml文档中写入:

<!--开启事务的注解配置驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

在service层中的待配置方法上加上注解“@Transactional”,表示该方法被设定为一个事务,只读默认为false,事务传递方式默认为required(有事务就加,无事务自己创建,用于增删改),可设定service类上加上“@Transactional”则对该类下全部方法生效。而对于其中的特例——查寻,可以单独配置“@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)”。(supports为有事务就用,无事务就不用,用于查寻)

三、纯注解配置(no xml):
配置类如下:

@Configuration
@ComponentScan("com.xx")
@EnableTransactionManagement//开启事务的注解支持
public class SpringConfig {
    @Bean("ds")
    public DataSource getDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/XXX");
        dataSource.setUsername("root");
        dataSource.setPassword("xxxx");
        return dataSource;
    }
    @Bean("transactionManager")
    public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("ds") DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值