1、简介
Transactional是spring中定义的事务注解,在方法或类上加该注解开启事务。主要是通过反射获取bean的注解信息,利用AOP对编程式事务进行封装实现。(spring-5.1.8.RELEASE)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {@AliasFor("transactionManager")String value() default "";@AliasFor("value")String transactionManager() default "";Propagation propagation() default Propagation.REQUIRED;Isolation isolation() default Isolation.DEFAULT;int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;boolean readOnly() default false;Class<? extends Throwable>[] rollbackFor() default {};String[] rollbackForClassName() default {};Class<? extends Throwable>[] noRollbackFor() default {};String[] noRollbackForClassName() default {};
}
2、自定义注解
2.1 定义
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation {//自定义注解的属性int id() default 0;String name() default "默认名称";String[]arrays();String title() default "默认标题";
}
2.2 测试
public class App {@MyAnnotation(name = "wqd", arrays = {"2", "3"})public void aMethod() {}public void bMethod() {}public static void main(String[] args) throws ClassNotFoundException {// 反射获取到类的信息Class<?> clazz = Class.forName("com.wyq.App");// 获取当前类(不包括继承)所有方法Method[] methods = clazz.getDeclaredMethods();// 遍历每个方法的信息for (Method method : methods) {System.out.println("方法名称:" + method.getName());// 获取方法上面的注解MyAnnotation annotation = method.getDeclaredAnnotation(MyAnnotation.class);if (annotation == null) {System.out.println("该方法上没有加注解...");} else {System.out.println("Id : " + annotation.id());System.out.println("Name : " + annotation.name());System.out.println("Title : " + annotation.title());System.out.println("Arrays : " + annotation.arrays());}System.out.println("--------------------------");}}
}控制台信息:方法名称:main
该方法上没有加注解...
--------------------------
方法名称:aMethod
Id : 0
Name : wqd
Title : 默认标题
Arrays : [Ljava.lang.String;@24d46ca6
--------------------------
方法名称:bMethod
该方法上没有加注解...
--------------------------
2.3 总结
通过上面这么一个小demo我们就能发现,反射获取到每一个方法的注解信息然后进行判断,如果这是@Transactional注解,spring就会开启事务。接下来我们可以按照这种思路自己实现一个事务注解。
3、手写事务注解
3.1 maven依赖
<dependencies><!-- 引入Spring-AOP等相关Jar --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-orm</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.9.4</version></dependency><dependency><groupId>aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.5.4</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>3.3.0</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.37</version></dependency></dependencies>
3.2 配置spring.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!--扫描指定路径--><context:component-scan base-package="com.wyq" /><!--开启切面代理--><aop:aspectj-autoproxy /><!--数据源对象,C3P0 连接池--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/><property name="user" value="root"/><property name="password" value="123456"/></bean><!--JdbcTemplate 工具类实例--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean><!--配置事务--><bean id="dataSoutceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean>
</beans>
3.3 自定义事务注解 (通过反射解析方法上的注解,如果有这个注解就执行事务逻辑)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MyAnnotation {//自定义注解的属性int id() default 0;String name() default "默认名称";String[] arrays() default {};String title() default "默认标题";
}
3.4 封装编程式事务
@Component
@Scope("prototype")
public class TransactionUtil {// 全局接受事务状态private TransactionStatus transactionStatus;// 获取事务原@Autowiredprivate 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.5 通过AOP封装事务工具类, 基于环绕通知和异常通知来触发事务
@Component
@Aspect
public class AopTransaction {@Autowiredprivate TransactionUtil transactionUtil;private TransactionStatus transactionStatus;/*** 环绕通知,在方法 前---后 处理事情** @param pjp 切入点*/@Around("execution(* com.wyq.service.*.*(..))")public void around(ProceedingJoinPoint pjp) throws Throwable {// 获取方法的注解MyAnnotation annotation = this.getMethodMyAnnotation(pjp);// 判断是否需要开启事务transactionStatus = begin(annotation);// 调用目标代理对象方法pjp.proceed();// 判断关闭事务commit(transactionStatus);}/*** 获取代理方法上的事务注解** @param pjp* @return* @throws Exception*/private MyAnnotation getMethodMyAnnotation(ProceedingJoinPoint pjp) throws Exception {// 获取代理对象的方法String methodName = pjp.getSignature().getName();// 获取目标对象Class<?> classTarget = pjp.getTarget().getClass();// 获取目标对象类型Class<?>[] par = ((MethodSignature) pjp.getSignature()).getParameterTypes();// 获取目标对象方法Method objMethod = classTarget.getMethod(methodName, par);// 获取该方法上的事务注解MyAnnotation annotation = objMethod.getDeclaredAnnotation(MyAnnotation.class);return annotation;}/*** 开启事务** @param annotation* @return*/private TransactionStatus begin(MyAnnotation annotation) {if (annotation == null) {return null;}return transactionUtil.begin();}/*** 提交事务** @param transactionStatus*/private void commit(TransactionStatus transactionStatus) {if (transactionStatus != null) {transactionUtil.commit(transactionStatus);}}/*** 异常通知进行 回滚事务*/@AfterThrowing("execution(* com.wyq.service.*.*(..))")public void afterThrowing() {// 获取当前事务 直接回滚if (transactionStatus != null) {transactionUtil.rollback(transactionStatus);}}
}
3.6 dao 层
/*
CREATE TABLE `t_user0` (`name` varchar(20) NOT NULL,`age` int(5) DEFAULT NULL,PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/
@Repository
public class UserDao {@Autowiredprivate JdbcTemplate jdbcTemplate;public int add(String name, Integer age) {String sql = "INSERT INTO t_user0(NAME, age) VALUES(?,?);";int result = jdbcTemplate.update(sql, name, age);System.out.println("插入成功");return result;}
}
3.7 service 层
@Service
public class UserService {@Autowiredprivate UserDao userDao;// 加上事务注解@MyAnnotationpublic 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...");}
}
3.8 测试
public class Test {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();}
}