1. 程式人生 > 其它 >Spring通過aop實現事務管理

Spring通過aop實現事務管理

1.在resources目錄下的spring-dao.xml中添加於事務有關的bean

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

2.配置事務通知

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--給那些方法配置事務-->
        <!--配置事務的傳播特性: new -->
        <tx:attributes>
            <tx:method name="add" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="update" propagation="REQUIRED"/>
            <tx:method name="query" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
</tx:advice>

3.配置事務切入

<aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.kuang.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>