利用spring的aop实现事务管理步骤

来源:互联网 发布:ubuntu运行loic.exe 编辑:程序博客网 时间:2024/05/01 03:18

在spring-context.xml中配置即可(这只贴了关键信息):



<!--事物配置 -->
    <!--配置aop切面-->
    <aop:config proxy-target-class="true">
        <!--切面配置:表达式:控制到方法上:修饰符 返回值 方法(参数) ===》返回值*:表示任何返回值,参数:(..):表示任意个参数
注意:在定位参数的时候,com.wb.service.impl.*.*.*(..) 
表示com.wb.service.impl.user.xxxximpl.xx方法(任意个方法参数);当然,也可以表示com.wb.service.impl.xxxximpl.xx方法(任意个方法参数),就是少了user这一层
          ,就是说(..) 前面的第一个*表示的是方法,注意在写这个表述式后,创建service的实现的时候,路径一定要按照这个规则来,不然不能被事务控制
        -->
        <aop:pointcut id="serviceMethods"
                      expression="execution(public * com.wb.service.impl.*.*.*(..))"/>
        <!--通知调用-->
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>
    <!--配置事务管理器-->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>


    <!--配置事务的传播属性和异常处理-->
    <tx:advice transaction-manager="transactionManager" id="transactionAdvice">
        <tx:attributes>
            <tx:method name="read*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="load*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

1 0
原创粉丝点击