Spring 使用xml配置事务

1 篇文章 0 订阅
1 篇文章 0 订阅
本文介绍了如何在 Spring 中使用 XML 配置进行事务管理。首先,创建 XML 文件并声明命名空间;其次,配置事务管理器的 bean;接着,设定事务生效规则,如对 get 方法的特殊处理;然后,通过 AOP 配置切入点,应用事务规则到指定包下的所有方法。最终,通过 aop:advisor 结合事务管理器和切入点完成配置。
摘要由CSDN通过智能技术生成

第一步,创建 xml 文件,并声明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: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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 在这里添加配置 -->
</beans>

第二步,配置 spring 事务管理器的 bean。

 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
 </bean>

第三步,我们已经有了事务管理器,接下来配置它的生效规则。

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="*"/>
        </tx:attributes>
</tx:advice>

对上面的配置作下说明:
id: 唯一标识。虽然配置了事务管理器对方法进行拦截的规则,但是还没有和切入点联系起来,比如要拦截哪个包下方法?哪个类下的方法?拥有哪些参数列表的方法?这些都要等着第四步的切入点配置完成后才能确定。

transaction-manager: 事务管理器。毋庸置疑,是我们第二步中配置的事务管理bean。

tx:method 中的 name: 对哪些方法进行拦截。

  • get* 表示所有名称以 get 开头的方法。对于这些方法,不用开启事务,并且是只读操作,不会修改数据。
    • 表示所有方法,propagation 不写会使用默认值 REQUIRED, 意思是开启事务。read-only 不写默认值是 false,表示会对数据进行写操作。

上面两个通配符,匹配范围小的优先级高。所有, 当匹配到 get* 时,优先使用 get* 的配置,也就是不开启事务。

第四步,配置 aop。事务管理器也是通过 aop 原理来工作的,这和其它的切面类并没有区别。既然其它的切面类是通过 aop来配置,它也不能例外。

<aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.zfy.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>

aop:pointcut 定义了切入点。切入点是以方法为最小单位的。其中的 expression 就定义了这个切点包含的方法。可以看到使用了通配符匹配了 com.zfy.service.impl 包下的所有方法。

aop:advisor 通过两个 ref 属性将事务管理器的相应规则应用到定义好的切点上。

这样,Spring 事务管理就配置成功了。至于其它的 bean 配置,比如 JdbcTemplate, DataSource,可以参考下面的完整配置:

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

    <bean id="accountService" class="com.zfy.service.impl.AccountServiceImpl">
        <property name="dao" ref="accountDao"/>
    </bean>
    <bean id="accountDao" class="com.zfy.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

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

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ioc"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>

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

    <!--    配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <!--    配置 aop-->
    <aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.zfy.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
    </aop:config>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值