记一次Spring配置声明式事务但是没有生效的问题

本来就是一个很简单的小问题,但是真的被细节打败了。。。
事务死活就是开不启,查资料、找原因也花了好长时间。
索性就记录下来。
我原本的配置文件是这样的
spring-persist-tx.xml:

<?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:context="http://www.springframework.org/schema/context"
	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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<!-- 这是Spring专门管理事务的配置文件 -->
	<!-- 配置自动扫描的包:主要是为了把Service扫描到IOC容器中 -->
	<context:component-scan base-package="com.atguigu.crowd.service"/>
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 装配数据源 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 配置事务切面 -->
	<aop:config>
		<!-- execution(public String com.atguigu.crowd.service.AdminService.saveAdmin(Admin)) -->
		<!-- 考虑到后面我们整合SpringSecurity,避免把UserDetailsService加入到事务控制,让切入点表达式定位到ServiceImpl那个类,而不是Service接口 -->
		<aop:pointcut expression="execution(* *..*ServiceImpl.*(..))" id="txPointcut"/>
		<!-- 将切入点表达式和事务通知关联起来 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
	</aop:config>
	<!-- 配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<!-- 配置事务属性 -->
		<tx:attributes>
			<!-- 查询方法:配置只读属性,让数据库知道这是一个查询操作,能够进行一定优化 -->
			<!-- 我们约定:在Service层中的方法名与dao层中的方法名规范:
							get/query/find		select
							save				insert
							remove				delete -->
			<tx:method name="get" read-only="true"/>
			<tx:method name="find" read-only="true"/>
			<tx:method name="query" read-only="true"/>
			<tx:method name="count" read-only="true"/>
			
			<!-- 增删改方法:配置事务的传播行为、回滚异常 -->
			<!-- propagation属性:
						REQUIRED:默认值,它表示当前方法必须工作在事务中,如果当前线程上没有已经开启的事务,则自己开启新事务
																	   如果已经有了,就使用这个已有的事务
																	   顾虑:可能自己没出错,但被别人开启的事务回滚了
																	   (所以我们不用它)
						REQUIRES_NEW:建议使用的值,它表示不管当前线程上有没有事务,都要自己开事务,在自己的事务中运行
									    好处:不会受到其他事务回滚的影响 -->
			<!-- rollback-for属性:配置事务方法针对什么样的异常回滚
								默认是遇到运行时异常回滚
								建议:编译时异常和运行时异常都回滚 -->
			<tx:method name="save" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
			<tx:method name="update" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
			<tx:method name="remove" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
			<tx:method name="batch" propagation="REQUIRES_NEW" rollback-for="java.lang.Exception"/>
		</tx:attributes>
	</tx:advice>
</beans>

可以看到tx:method方法名字后边没有加*,虽然我们使用切入点表达式execution(* ServiceImpl.(…)),它可以定位到任意包下类名为ServiceImpl的任意方法,但我们还是要精确到具体方法。

我方法名后没有加*号,它就没有办法为这个操作添加事务。

加上*号:
在这里插入图片描述
可以看到事务开启成功:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值