事务操作——声明式事务管理(xml方式)
1——配置事务管理器
<!-- 创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
2——配置通知
<!-- 配置通知(方法增强部分)-->
<tx:advice id="txadvice">
<!-- 配置事务相关参数-->
<tx:attributes>
<!-- 指定哪种规则的方法上面添加事务-->
<tx:method name="accountmoney"/>
<!-- <tx:method name="account*"/> 星号代表着规则,也可以不使用星号,直接写方法名称-->
</tx:attributes>
</tx:advice>
3——配置切入点和切面
<!-- 配置切入点和切面-->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pt" expression="execution(* 转账事务.Service.UserService.*(..))"/>
<!-- 配置切面-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
</aop:config>
浙公网安备 33010602011771号