😉😉 学习交流群:
✅✅1:这是孙哥suns给大家的福利!
✨✨2:我们免费分享Netty、Dubbo、k8s、Mybatis、Spring...应用和源码级别的视频资料
🥭🥭3:QQ群:583783824 📚📚 工作微信:BigTreeJava 拉你进微信群,免费领取!
🍎🍎4:本文章内容出自上述:Spring应用课程!💞💞
💞💞5:以上内容,进群免费领取呦~ 💞💞💞💞
一:Spring事务处理
1:什么是事务?
保证业务操作完整性的数据库操作,就是数据库中的操作要么一起成功要么 一起失败,而且不能产生响应的影响,事务这个概念是数据库的范畴,我们通过Java代码只是完成对这种机制的调用
2:事务的ACID
1、原子性:一起成功一起失败
2、一致性:与实际发生相一致
3、隔离性:事务之间不可以相互访问
4、持久性:持久到数据库
3:如何来控制事务
这里我们先不考虑Spring
JDBC控制事务
Connection.setAutoCommit(false)开启事务
ConnectIon.commit()提交事务
Connection.rollback()回滚事务
其核心就是控制Connection链接对象来控制事务
Mybatis控制事务
Mybatis自动开启事务
sqlSession.commit()提交事务
sqlSession.rollback()回滚事务
总结:开启-提交-回滚
Mybatis底层的SqlSession底层也是封装的Connection,底层也是调用的是Connection.commit,不论是当前的jdbc还是Mybatis当中的sqlSession还是将来的jta,底层都是调用的Connection.commit,封装的都是COnnection对象,所有的技术,我们看到
的表象是不一样的,但是底层都是一样的,以上的都是不讨论Spring的事务控制的前提下。**
二:Spring控制事务开发
1:开发步骤
事务是业务开发过程中的额外功能,因为是额外功能所以我们可以采用Aop的方式来进行开发,Spring当中的事务是通过Aop的方式来进行控制事务开发的
(一):原始对象
(1)这里的原始对象就是XXXUserServiceImpl中的方法,核心功能(业务功能+Dao的调用)
(2)我们要把Dao作为Service的成员边量,通过Spring的方式进行依赖注入的方式进行赋值,并为他提供get、set方法。
(二):额外功能
(1)第一种实现MethodInterceptor,在里面的invoke方法中调用原始对象原始方法,并进行额外功能编写,如果原始方法抛出异常,我们就进行事务的回滚。
(2)@Aspect @Around这种方式呢实现原理是一样的,我们可以把开启提交回滚的代码写在额外功能当中,所以Spring也会写,所以呢,Spring框架就直接把这个代码给写了,给分装好了,我们直接用即可,那我们在org.springframework.jdbc.datasourceDataSourceTransactionManager这里边把这个给封装好了,这里边就是事务控制的额外功能,只需要用好这个类就行,要想这个类起作用,就需要在额外功能中对这个连接对象的支持和配合,所以说DataSourceTranSactionManager依赖连接对象,我们需要给她注入连接,但是我们现在引入了连接池,现在呢连接来自于连接池,所以把连接池注入给她即可,这样这个类就可以基于连接池对象完成对事物的控制。在这里我们需要注意两个要点,第一个代码不用我们写了,Spring已经帮我们写好了,第二个要点,我们是用Spring进行实物控制的时候需要为这个类注入连接池对象,有了连接池对象就等效于有了链接,就可以控制事务了。
(三):切入点
@Transactionl就可以控制事务了。应用这个注解就可以给业务方法加入事务控制了。
1、用在类上:类中所有方法都会加入事务
2、用在方法上:这个方法会加入事务
(四):组装切面
切面一定是有两个部分组成的,切入点+额外功能。在Spring进行实物控制的时候,他的组装切面的时候是通过标签来控制的,在标签当中会体现这两个元素。
<tx:annotation-driven transaction-manager=""/>这个属性适用于获取额外功能,切入点他自己会直接 扫描注解来组装注解信息,引入这个标签我们就组装好了事务的切面
三:Spring控制事务的编码
1:搭建开发环境
引入相应的jar包,Spring引入事务控制的时候,他有开发了一个jar包需要我们引入进来,Spring-tx,引入这个jar包就把Spring与事务整合的Jar包引入了进来
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
2:原始对象和切入点
package com.dashu.service;
import com.dashu.Dao.UserDao;
import com.dashu.entity.User;
import org.springframework.transaction.annotation.Transactional;
/**
* @Auther: DaShu
* @Date: 2021/7/26 22:36
* @Description:
*/
/*定义切入点*/
@Transactional
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void register(User user) {
userDao.save(user);
}
}
3:额外功能和切面组装
<!--DataSourceTransactionManager-->
<bean id="dataSourceTransactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--组装事物切面,选择Tx结尾的。-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
4:测试Spring事务处理
@Test
public void test2(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
User user = new User();
user.setName("张晓哎");
user.setAge(21);
userService.register(user);
}
5:测试结果打印
2021-07-26 22:54:34 DEBUG ClassPathXmlApplicationContext:590 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@66d33a
2021-07-26 22:54:34 DEBUG XmlBeanDefinitionReader:396 - Loaded 10 bean definitions from class path resource [applicationContext.xml]
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'scanner'
2021-07-26 22:54:34 DEBUG LogFactory:135 - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2021-07-26 22:54:34 DEBUG ClassPathMapperScanner:437 - Identified candidate component class: file [D:\giteesource\jdbc\spring-mybatis\target\classes\com\dashu\dao\UserDao.class]
2021-07-26 22:54:34 DEBUG ClassPathMapperScanner:49 - Creating MapperFactoryBean with name 'userDao' and 'com.dashu.Dao.UserDao' mapperInterface
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.config.internalTransactionalEventListenerFactory'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'dataSource'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
2021-07-26 22:54:34 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'sqlSessionFactoryBean'
2021-07-26 22:54:34 DEBUG SqlSessionFactoryBean:49 - Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration
2021-07-26 22:54:35 DEBUG SqlSessionFactoryBean:49 - Parsed mapper file: 'file [D:\giteesource\jdbc\spring-mybatis\target\classes\com.dashu.mapper\UserDaoMapper.xml]'
2021-07-26 22:54:35 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'userService'
2021-07-26 22:54:35 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'userDao'
2021-07-26 22:54:35 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'dataSourceTransactionManager'
2021-07-26 22:54:35 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
2021-07-26 22:54:35 DEBUG DataSourceTransactionManager:372 - Creating new transaction with name [com.dashu.service.UserServiceImpl.register]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2021-07-26 22:54:35 INFO DruidDataSource:1003 - {dataSource-1} inited
2021-07-26 22:54:35 DEBUG DataSourceTransactionManager:265 - Acquired Connection [com.mysql.jdbc.JDBC4Connection@273444fe] for JDBC transaction
2021-07-26 22:54:35 DEBUG DataSourceTransactionManager:282 - Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@273444fe] to manual commit
2021-07-26 22:54:35 DEBUG SqlSessionUtils:49 - Creating a new SqlSession
2021-07-26 22:54:35 DEBUG SqlSessionUtils:49 - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 22:54:35 DEBUG SpringManagedTransaction:49 - JDBC Connection [com.mysql.jdbc.JDBC4Connection@273444fe] will be managed by Spring
2021-07-26 22:54:35 DEBUG save:159 - ==> Preparing: Insert into tree (name,age) values (?,?)
2021-07-26 22:54:35 DEBUG save:159 - ==> Parameters: 张晓哎(String), 21(Integer)
2021-07-26 22:54:35 DEBUG save:159 - <== Updates: 1
2021-07-26 22:54:35 DEBUG SqlSessionUtils:49 - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 22:54:35 DEBUG SqlSessionUtils:49 - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 22:54:35 DEBUG SqlSessionUtils:49 - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 22:54:35 DEBUG SqlSessionUtils:49 - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 22:54:35 DEBUG DataSourceTransactionManager:743 - Initiating transaction commit
2021-07-26 22:54:35 DEBUG DataSourceTransactionManager:327 - Committing JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@273444fe]
2021-07-26 22:54:36 DEBUG DataSourceTransactionManager:385 - Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@273444fe] after transaction
6:事务编码细节
上述测试实例并不能证明事务加进来了,因为原有逻辑可以完成插入操作的,事务加上没有一定是加上了,以下是验证细节
package com.dashu.service;
import com.dashu.Dao.UserDao;
import com.dashu.entity.User;
import org.springframework.transaction.annotation.Transactional;
/**
* @Auther: DaShu
* @Date: 2021/7/26 22:36
* @Description:
*/
/*定义切入点*/
@Transactional
public class UserServiceImpl implements UserService{
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void register(User user) {
userDao.save(user);
throw new RuntimeException("测试异常");
}
}
D:\DevelopPackage\jdk8\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\DevelopPackage\idea\IntelliJ IDEA 2020.2.2\lib\idea_rt.jar=58943:D:\DevelopPackage\idea\IntelliJ IDEA 2020.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\DevelopPackage\idea\IntelliJ IDEA 2020.2.2\lib\idea_rt.jar;D:\DevelopPackage\idea\IntelliJ IDEA 2020.2.2\plugins\junit\lib\junit5-rt.jar;D:\DevelopPackage\idea\IntelliJ IDEA 2020.2.2\plugins\junit\lib\junit-rt.jar;D:\DevelopPackage\jdk8\jre\lib\charsets.jar;D:\DevelopPackage\jdk8\jre\lib\deploy.jar;D:\DevelopPackage\jdk8\jre\lib\ext\access-bridge-64.jar;D:\DevelopPackage\jdk8\jre\lib\ext\cldrdata.jar;D:\DevelopPackage\jdk8\jre\lib\ext\dnsns.jar;D:\DevelopPackage\jdk8\jre\lib\ext\jaccess.jar;D:\DevelopPackage\jdk8\jre\lib\ext\jfxrt.jar;D:\DevelopPackage\jdk8\jre\lib\ext\localedata.jar;D:\DevelopPackage\jdk8\jre\lib\ext\nashorn.jar;D:\DevelopPackage\jdk8\jre\lib\ext\sunec.jar;D:\DevelopPackage\jdk8\jre\lib\ext\sunjce_provider.jar;D:\DevelopPackage\jdk8\jre\lib\ext\sunmscapi.jar;D:\DevelopPackage\jdk8\jre\lib\ext\sunpkcs11.jar;D:\DevelopPackage\jdk8\jre\lib\ext\zipfs.jar;D:\DevelopPackage\jdk8\jre\lib\javaws.jar;D:\DevelopPackage\jdk8\jre\lib\jce.jar;D:\DevelopPackage\jdk8\jre\lib\jfr.jar;D:\DevelopPackage\jdk8\jre\lib\jfxswt.jar;D:\DevelopPackage\jdk8\jre\lib\jsse.jar;D:\DevelopPackage\jdk8\jre\lib\management-agent.jar;D:\DevelopPackage\jdk8\jre\lib\plugin.jar;D:\DevelopPackage\jdk8\jre\lib\resources.jar;D:\DevelopPackage\jdk8\jre\lib\rt.jar;D:\giteesource\jdbc\spring-mybatis\target\classes;D:\DevelopPackage\repository\junit\junit\4.12\junit-4.12.jar;D:\DevelopPackage\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\DevelopPackage\repository\com\alibaba\druid\1.1.18\druid-1.1.18.jar;D:\DevelopPackage\repository\mysql\mysql-connector-java\5.1.48\mysql-connector-java-5.1.48.jar;D:\DevelopPackage\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;D:\DevelopPackage\repository\org\springframework\spring-jdbc\5.1.14.RELEASE\spring-jdbc-5.1.14.RELEASE.jar;D:\DevelopPackage\repository\org\springframework\spring-beans\5.1.14.RELEASE\spring-beans-5.1.14.RELEASE.jar;D:\DevelopPackage\repository\org\springframework\spring-core\5.1.14.RELEASE\spring-core-5.1.14.RELEASE.jar;D:\DevelopPackage\repository\org\springframework\spring-jcl\5.1.14.RELEASE\spring-jcl-5.1.14.RELEASE.jar;D:\DevelopPackage\repository\org\springframework\spring-tx\5.1.14.RELEASE\spring-tx-5.1.14.RELEASE.jar;D:\DevelopPackage\repository\org\springframework\spring-context\5.1.4.RELEASE\spring-context-5.1.4.RELEASE.jar;D:\DevelopPackage\repository\org\springframework\spring-aop\5.1.4.RELEASE\spring-aop-5.1.4.RELEASE.jar;D:\DevelopPackage\repository\org\springframework\spring-expression\5.1.4.RELEASE\spring-expression-5.1.4.RELEASE.jar;D:\DevelopPackage\repository\org\mybatis\mybatis-spring\2.0.2\mybatis-spring-2.0.2.jar;D:\DevelopPackage\repository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;D:\DevelopPackage\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\DevelopPackage\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.dashu.test.TestMybatisSpring,test2
2021-07-26 23:00:29 DEBUG ClassPathXmlApplicationContext:590 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@66d33a
2021-07-26 23:00:29 DEBUG XmlBeanDefinitionReader:396 - Loaded 10 bean definitions from class path resource [applicationContext.xml]
2021-07-26 23:00:29 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'scanner'
2021-07-26 23:00:29 DEBUG LogFactory:135 - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
2021-07-26 23:00:29 DEBUG ClassPathMapperScanner:437 - Identified candidate component class: file [D:\giteesource\jdbc\spring-mybatis\target\classes\com\dashu\dao\UserDao.class]
2021-07-26 23:00:29 DEBUG ClassPathMapperScanner:49 - Creating MapperFactoryBean with name 'userDao' and 'com.dashu.Dao.UserDao' mapperInterface
2021-07-26 23:00:29 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2021-07-26 23:00:29 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2021-07-26 23:00:29 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.config.internalTransactionalEventListenerFactory'
2021-07-26 23:00:29 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.aop.config.internalAutoProxyCreator'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'dataSource'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'sqlSessionFactoryBean'
2021-07-26 23:00:30 DEBUG SqlSessionFactoryBean:49 - Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration
2021-07-26 23:00:30 DEBUG SqlSessionFactoryBean:49 - Parsed mapper file: 'file [D:\giteesource\jdbc\spring-mybatis\target\classes\com.dashu.mapper\UserDaoMapper.xml]'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'userService'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'userDao'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'dataSourceTransactionManager'
2021-07-26 23:00:30 DEBUG DefaultListableBeanFactory:213 - Creating shared instance of singleton bean 'org.springframework.transaction.interceptor.TransactionInterceptor#0'
2021-07-26 23:00:30 DEBUG DataSourceTransactionManager:372 - Creating new transaction with name [com.dashu.service.UserServiceImpl.register]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
2021-07-26 23:00:30 INFO DruidDataSource:1003 - {dataSource-1} inited
2021-07-26 23:00:31 DEBUG DataSourceTransactionManager:265 - Acquired Connection [com.mysql.jdbc.JDBC4Connection@273444fe] for JDBC transaction
2021-07-26 23:00:31 DEBUG DataSourceTransactionManager:282 - Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@273444fe] to manual commit
2021-07-26 23:00:31 DEBUG SqlSessionUtils:49 - Creating a new SqlSession
2021-07-26 23:00:31 DEBUG SqlSessionUtils:49 - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 23:00:31 DEBUG SpringManagedTransaction:49 - JDBC Connection [com.mysql.jdbc.JDBC4Connection@273444fe] will be managed by Spring
2021-07-26 23:00:31 DEBUG save:159 - ==> Preparing: Insert into tree (name,age) values (?,?)
2021-07-26 23:00:31 DEBUG save:159 - ==> Parameters: xi(String), 21(Integer)
2021-07-26 23:00:31 DEBUG save:159 - <== Updates: 1
2021-07-26 23:00:31 DEBUG SqlSessionUtils:49 - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 23:00:31 DEBUG SqlSessionUtils:49 - Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 23:00:31 DEBUG SqlSessionUtils:49 - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1972e513]
2021-07-26 23:00:31 DEBUG DataSourceTransactionManager:836 - Initiating transaction rollback
2021-07-26 23:00:31 DEBUG DataSourceTransactionManager:342 - Rolling back JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@273444fe]
2021-07-26 23:00:31 DEBUG DataSourceTransactionManager:385 - Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@273444fe] after transaction
java.lang.RuntimeException: 测试异常
at com.dashu.service.UserServiceImpl.register(UserServiceImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy18.register(Unknown Source)
at com.dashu.test.TestMybatisSpring.test2(TestMybatisSpring.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Process finished with exit code -1
第二个细节,是 --proxy-target-class="false"这是默认属性,默认是false,可以设置成true,设置成true就是使用cglib动态代理。
<tx:annotation-driven transaction-manager="dataSourceTransactionManager" proxy-target-class="false"/>完成动态代理底层的切换