实现方式:AOP配置事务
1.
<!--声明事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
</bean>
2.
< tx:advice id ="txAdvice" transaction-manager ="transactionManager" >
< tx:attributes >
//指定给add方法配置事务
< tx:method name ="add" propagation ="REQUIRED" />
</ tx:attributes >
</ tx:advice >
3.配置事务切入点
<aop:config>
<!-- 切入点 execution(要执行的位置) 第一个*表示所有返回类型 -->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"/>
<!-- advice-ref切入哪种方法 pointcut-ref="pointcut":需要被切入的方法 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
相关知识:注解实现AOP(非实现事务)
1.
<bean id="annotationPointCut" class="diy.AnnotationPointCut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
2.
//标注这个类是一个切面
@Aspect
public class AnnotationPointCut {
//指定在哪些方法之前切入
@Before("execution(* service.UserServiceImpl.*(..))")
public void before(){
System.out.println("这是注解实现方法执行前");
}
}
propagation参数