声明式事务控制

<!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!-- 配置事务的属性
    id:指定advice的id,后边要用到
    transaction-manager:写的是事务管理器的id
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <!-- find开头的方法加只读事务 ,*表示通配符,匹配任意-->
        <tx:method name="find*" read-only="true"/>
        <!-- 其余方法是加可读写的事务 -->
        <tx:method name="*"/>
        </tx:attributes>
</tx:advice>

<!-- 配置事务切面 -->
<aop:config>
    <!-- 配置切入点表达式:告诉框架哪些方法要控制事务 -->
    <aop:pointcut expression="execution(* cn.itcast.service.impl.*.*(..))" id="pt"/>
   <!--将定义好的事务属性应用到上述的切入点 -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>

猜你喜欢

转载自blog.csdn.net/luoxiao2554/article/details/80443358