Sprign声明式事务控制(xml配置)2


首先要明白什么是切点,这里的切点就是业务层的方法,所以就要在配置文件中配置业务层的对象。
什么是通知?就是事务的增强也是需要在配置文件中配置的。
第三步就是把切点和切面完成aop的配置。

1.配置切点

 <!--目标对象  内部的方法就是切点-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean

2.配置增强

事务增强中有一个transactionManager属性,这个属性方法需要一个指定一个平台事务管理器,也就是需要一个DataSourceTransactionManager,需要Connection对象也就是封装的jdbc模板。

    <!--配置平台事务管理器-->
    <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="*"/>
        </tx:attributes>
    </tx:advice>

3.配置切面

  <!--配置事务的aop织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" expression="execution(* com.itheima.service.impl.*.*(..))"/>
    </aop:config>

完整代码

<?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"
       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
">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

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

    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

    <!--目标对象  内部的方法就是切点-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </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="*"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务的aop织入-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" expression="execution(* com.itheima.service.impl.*.*(..))"/>
    </aop:config>

</beans>

效果就是当你执行业务方法出现错误时,进行了事务控制。就是out方法和in方法要同时成功或者失败。


tx:method里的method就是被增强的方法也就是切点。我们之前讲的编程式声明事务有三个对象,platformtransactionmanage和transactiondefinition和transactionstatus,分别是行为,属性,状态。里面的行为和属性是需要配置的。

*代表通配符。

第一个就是平台事务管理器配置,就是告诉spring我用的是哪个平台的api进行配置。

然后就是事务的增强,需要引用事务管理器

第三个就是织入,当只要一个通知时,不用aop:aspect而是advisor。

切点表达式也是可以抽取的,直接用poingtcut-ref引用切点的id即可。

posted @ 2022-04-17 23:09  长情c  阅读(25)  评论(0)    收藏  举报