Transactional是spring中定义的事务注解,在方法或类上加该注解开启事务。主要是通过反射获取bean的注解信息,利用AOP对编程式事务进行封装实现。
spring事务注解:

- 自定义一个注解
/**
* @Target 作用域(作用在方法上,类上,或是属性上)
* @Retention 运行周期
* @interface 定义注解
*/
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
//自定义注解的属性
int id() default 0;
String name() default "默认名称";
String[]arrays();
String title() default "默认标题";
}- 测试
import java.lang.reflect.Method;
public class User {
@MyAnnotation(name="吴磊",arrays = {"2","3"})
public void aMethod () {}
public void bMethod () {}
public static void main(String[] args) throws ClassNotFoundException {
//1. 反射获取到类信息
Class<?> forName = Class.forName("com.demo.User");
//2. 获取到当前类(不包含继承)所有的方法
Method[] declaredMethods = forName.getDeclaredMethods();
//3. 遍历每个方法的信息
for (Method method : declaredMethods) {
System.out.println("方法名称:" + method.getName());
//4. 获取方法上面的注解
MyAnnotation annotation = method.getDeclaredAnnotation(MyAnnotation.class);
if(annotation == null) {
System.out.println("该方法上没有加注解....");
}else {
System.out.println("Id:" + annotation.id());
System.out.println("Name:" + ());
System.out.println("Arrays:" + annotation.arrays());
System.out.println("Title:" + annotation.title());
}
System.out.println("--------------------------");
}
}
}=============================
【控制台输出】
方法名称:main
该方法上没有加注解…
方法名称:aMethod
Id:0
Name:吴磊
Arrays:[Ljava.lang.String;@74a14482
Title:默认标题
方法名称:bMethod
该方法上没有加注解…
总结:通过上面这么一个小demo我们就能发现,反射获取到每一个方法的注解信息然后进行判断,如果这是@Transactional注解,spring就会开启事务。
接下来我们可以按照这种思路基于上一篇博客的编程式事务自己实现一个事务注解。
- 导包
<dependencies>
<!-- 引入Spring-AOP等相关Jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>- 配置spring.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 扫描指定路劲 -->
<context:component-scan base-package="com.wulei"/>
<!-- 开启切面代理 -->
<aop:aspectj-autoproxy />
<!-- 1. 数据源对象: C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 2. JdbcTemplate工具类实例 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3. 配置事务 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>3.1 自定义事务注解 (通过反射解析方法上的注解,如果有这个注解就执行事务逻辑)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//@Transaction可以作用在类和方法上, 我们这里只作用在方法上。
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
/**
* 自定义事务注解
*/
public @interface MyAnnotation {
}3.2 封装编程式事务
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
@Component
@Scope("prototype") // 申明为多例,解决线程安全问题。
/**
* 手写编程式事务
*/
public class TransactionUtil {
// 全局接受事务状态
private TransactionStatus transactionStatus;
// 获取事务源
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
// 开启事务
public TransactionStatus begin() {
System.out.println("开启事务");
transactionStatus = dataSourceTransactionManager.getTransaction(new DefaultTransactionAttribute());
return transactionStatus;
}
// 提交事务
public void commit(TransactionStatus transaction) {
System.out.println("提交事务");
if(dataSourceTransactionManager != null) dataSourceTransactionManager.commit(transaction);
}
// 回滚事务
public void rollback(TransactionStatus transaction) {
System.out.println("回滚事务...");
if(dataSourceTransactionManager != null) dataSourceTransactionManager.rollback(transaction);
}
}3.3 通过AOP封装事务工具类, 基于环绕通知和异常通知来触发事务。
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import com.wulei.transaction.TransactionUtil;
@Aspect// 申明为切面
@Component
/**
* 切面类封装事务逻辑
*/
public class AopTransaction {
@Autowired
private TransactionUtil transactionUtil;
private TransactionStatus transactionStatus;
/**
* 环绕通知 在方法之前和之后处理事情
* @param pjp 切入点
*/
@Around("execution(* com.wulei.service.*.*(..))")
public void around(ProceedingJoinPoint pjp) throws Throwable {
// 1.获取方法的注解
MyAnnotation annotation = this.getMethodMyAnnotation(pjp);
// 2.判断是否需要开启事务
transactionStatus = begin(annotation);
// 3.调用目标代理对象方法
pjp.proceed();
// 4.判断关闭事务
commit(transactionStatus);
}
/**
* 获取代理方法上的事务注解
* @param pjp 切入点
*/
private MyAnnotation getMethodMyAnnotation(ProceedingJoinPoint pjp) throws Exception {
//1. 获取代理对对象的方法
String methodName = pjp.getSignature().getName();
//2. 获取目标对象
Class<?> classTarget = pjp.getTarget().getClass();
//3. 获取目标对象类型
Class<?>[] par = ((MethodSignature) pjp.getSignature()).getParameterTypes();
//4. 获取目标对象方法
Method objMethod = classTarget.getMethod(methodName, par);
//5. 获取该方法上的事务注解
MyAnnotation annotation = objMethod.getDeclaredAnnotation(MyAnnotation.class);
return annotation;
}
/** 开启事务 */
private TransactionStatus begin(MyAnnotation annotation) {
if(annotation == null) return null;
return transactionUtil.begin();
}
/** 关闭事务 */
private void commit(TransactionStatus transactionStatus) {
if(transactionStatus != null) transactionUtil.commit(transactionStatus);
}
/**
* 异常通知进行 回滚事务
*/
@AfterThrowing("execution(* com.wulei.service.*.*(..))")
public void afterThrowing() {
// 获取当前事务 直接回滚
//TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
if(transactionStatus != null) transactionUtil.rollback(transactionStatus);
}
}- 编写dao层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/*
CREATE TABLE `t_users` (
`name` varchar(20) NOT NULL,
`age` int(5) DEFAULT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public int add(String name, Integer age) {
String sql = "INSERT INTO t_users(NAME, age) VALUES(?,?);";
int result = jdbcTemplate.update(sql, name, age);
System.out.println("插入成功");
return result;
}
}- 编写service
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wulei.dao.UserDao;
import com.wulei.transaction.MyAnnotation;
@Service
public class UserService {
@Autowired
private UserDao userDao;
// 加上事务注解
@MyAnnotation
public void add() {
userDao.add("test001", 20);
int i = 1 / 0;
userDao.add("test002", 21);
// // 如果非要try,那么出现异常不会被aop的异常通知监测到,必须手动在catch里面回滚事务。
// try {
// userDao.add("test001", 20);
// int i = 1 / 0;
// userDao.add("test002", 21);
// } catch (Exception e) {
// // 回滚当前事务
// System.out.println("回滚");
// TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
// }
}
public void del() {
System.out.println("del...");
}
}- 测试
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wulei.service.UserService;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
// aop()对userService类进行了拦截,添加自定义事务注解的方法会触发事务逻辑
userService.add();
// del()方法没有加注解,则什么也不会触发。
//userService.del();
}
}@Transactional失效的几种场景
1.一个有@Transactional的方法被没有@Transactional方法调用时,会导致Transactional作用失效。也是最容易出现的情况。
那为啥会出现这种情况?其实这还是由于使用Spring AOP代理造成的,因为只有当事务方法被当前类以外的代码调用时,才会由Spring生成的代理对象来管理。
2.对非public方法进行事务注解。@Transactional 将会失效。
原因:是应为在Spring AOP代理时,事务拦截器在目标方法前后进行拦截,DynamicAdvisedInterceptor的intercept 方法会获取Transactional注解的事务配置信息,
因为在Spring AOP 代理时,如上图所示 TransactionInterceptor (事务拦截器)在目标方法执行前后进行拦截,DynamicAdvisedInterceptor(CglibAopProxy 的内部类)的 intercept 方法或 JdkDynamicAopProxy 的 invoke 方法会间接调用 AbstractFallbackTransactionAttributeSource的 computeTransactionAttribute 方法会间接调用 AbstractFallbackTransactionAttributeSource的 computeTransactionAttribute 方法,这个方法会获取Transactional 注解的事务配置信息。他会首先校验事务方法的修饰符是不是public,不是 public则不会获取@Transactional 的属性配置信息。
3.Transactional 事务配置属性中的propagation 属性配置的问题。
当propagation属性配置为:
TransactionDefinition.PROPAGATION_SUPPORTS:如果当前存在事务,则加入该事务;如果当前没有事务,则以非事务的方式继续运行。
TransactionDefinition.PROPAGATION_NOT_SUPPORTED:以非事务方式运行,如果当前存在事务,则把当前事务挂起。 TransactionDefinition.PROPAGATION_NEVER:以非事务方式运行,如果当前存在事务,则抛出异常
4.还存在一种情况:
在一个类中A方法被事务注释,B方法也被事务注释。
@Transactional
public void A(){
try{
this.B();
}catch(Exception e){
logger.error();
}
}
但在执行B方法是报错,但是异常被A catch 住,此时事务也会失效。
















