Spring Boot 使用事务非常简单,首先使用注解 @EnableTransactionManagement 开启事务支持后,然后在访问数据库的Service方法上添加注解 @Transactional 便可。

关于事务管理器,不管是JPA还是JDBC等都实现自接口 PlatformTransactionManager 如果你添加的是 spring-boot-starter-jdbc 依赖,框架会默认注入 DataSourceTransactionManager 实例。如果你添加的是 spring-boot-starter-data-jpa 依赖,框架会默认注入 JpaTransactionManager 实例。

你可以在启动类中添加如下方法,Debug测试,就能知道自动注入的是 PlatformTransactionManager 接口的哪个实现类。

  1. @EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
  2. @SpringBootApplication
  3. public class ProfiledemoApplication {
  4.  
  5. @Bean
  6. public Object testBean(PlatformTransactionManager platformTransactionManager){
  7. System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
  8. return new Object();
  9. }
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(ProfiledemoApplication.class, args);
  13. }
  14. }

这些SpringBoot为我们自动做了,这些对我们并不透明,如果你项目做的比较大,添加的持久化依赖比较多,我们还是会选择人为的指定使用哪个事务管理器。 
代码如下:

  1. @EnableTransactionManagement
  2. @SpringBootApplication
  3. public class ProfiledemoApplication {
  4.  
  5. // 其中 dataSource 框架会自动为我们注入
  6. @Bean
  7. public PlatformTransactionManager txManager(DataSource dataSource) {
  8. return new DataSourceTransactionManager(dataSource);
  9. }
  10.  
  11. @Bean
  12. public Object testBean(PlatformTransactionManager platformTransactionManager) {
  13. System.out.println(">>>>>>>>>>" + platformTransactionManager.getClass().getName());
  14. return new Object();
  15. }
  16.  
  17. public static void main(String[] args) {
  18. SpringApplication.run(ProfiledemoApplication.class, args);
  19. }
  20. }

在Spring容器中,我们手工注解@Bean 将被优先加载,框架不会重新实例化其他的 PlatformTransactionManager 实现类。

然后在Service中,被 @Transactional 注解的方法,将支持事务。如果注解在类上,则整个类的所有方法都默认支持事务。

对于同一个工程中存在多个事务管理器要怎么处理,请看下面的实例,具体说明请看代码中的注释。

  1. @EnableTransactionManagement // 开启注解事务管理,等同于xml配置文件中的 <tx:annotation-driven />
  2. @SpringBootApplication
  3. public class ProfiledemoApplication implements TransactionManagementConfigurer {
  4.  
  5. @Resource(name="txManager2")
  6. private PlatformTransactionManager txManager2;
  7.  
  8. // 创建事务管理器1
  9. @Bean(name = "txManager1")
  10. public PlatformTransactionManager txManager(DataSource dataSource) {
  11. return new DataSourceTransactionManager(dataSource);
  12. }
  13.  
  14. // 创建事务管理器2
  15. @Bean(name = "txManager2")
  16. public PlatformTransactionManager txManager2(EntityManagerFactory factory) {
  17. return new JpaTransactionManager(factory);
  18. }
  19.  
  20. // 实现接口 TransactionManagementConfigurer 方法,其返回值代表在拥有多个事务管理器的情况下默认使用的事务管理器
  21. @Override
  22. public PlatformTransactionManager annotationDrivenTransactionManager() {
  23. return txManager2;
  24. }
  25.  
  26. public static void main(String[] args) {
  27. SpringApplication.run(ProfiledemoApplication.class, args);
  28. }
  29.  
  30. }
  1.  
  2. @Component
  3. public class DevSendMessage implements SendMessage {
  4.  
  5. // 使用value具体指定使用哪个事务管理器
  6. @Transactional(value="txManager1")
  7. @Override
  8. public void send() {
  9. System.out.println(">>>>>>>>Dev Send()<<<<<<<<");
  10. send2();
  11. }
  12.  
  13. // 在存在多个事务管理器的情况下,如果使用value具体指定
  14. // 则默认使用方法 annotationDrivenTransactionManager() 返回的事务管理器
  15. @Transactional
  16. public void send2() {
  17. System.out.println(">>>>>>>>Dev Send2()<<<<<<<<");
  18. }
  19.  
  20. }

注: 
如果Spring容器中存在多个 PlatformTransactionManager 实例,并且没有实现接口 TransactionManagementConfigurer 指定默认值,在我们在方法上使用注解 @Transactional 的时候,就必须要用value指定,如果不指定,则会抛出异常。

对于系统需要提供默认事务管理的情况下,实现接口 TransactionManagementConfigurer 指定。

对有的系统,为了避免不必要的问题,在业务中必须要明确指定 @Transactional 的 value 值的情况下。不建议实现接口 TransactionManagementConfigurer,这样控制台会明确抛出异常,开发人员就不会忘记主动指定。

@EnableTransactionManagement的使用的更多相关文章

  1. SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-001-使用Hibernate(@Inject、@EnableTransactionManagement、@Repository、PersistenceExceptionTranslationPostProcessor)

    一.结构 二.Repository层 1. package spittr.db; import java.util.List; import spittr.domain.Spitter; /** * ...

  2. @EnableTransactionManagement注解理解

    @EnableTransactionManagement表示开启事务支持,在springboot项目中一般配置在启动类上,效果等同于xml配置的<tx:annotation-driven /&g ...

  3. Spring Boot的事务管理注解@EnableTransactionManagement的使用

    @EnableTransactionManagement:负责开启springboot 的事物支持,等同于xml配置文件中的 <tx:annotation-driven /> 然后在访问数 ...

  4. @EnableConfigurationProperties、@EnableAsync、 @EnableTransactionManagement

    ★@ConfigurationProperties和@EnableConfigurationProperties配合使用 @ConfigurationProperties注解主要用来把properti ...

  5. Annotation Type EnableTransactionManagement

    http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Ena ...

  6. 【spring】spring boot中使用@EnableTransactionManagement 以后,spring mvc接收前台ajax的post方法传过来的参数,使用@RequestBody接收不到参数

    在启动类上添加了注解: @EnableTransactionManagement, postMan测试接口,以这种方式传递参数: 测试结果: 接收不到参数 问题解决: 原因:是因为 这个项目中的Con ...

  7. spring 事务 @EnableTransactionManagement原理

    @EnableXXX原理:注解上有个XXXRegistrar,或通过XXXSelector引入XXXRegistrar,XXXRegistrar实现了 ImportBeanDefinitionRegi ...

  8. 【spring cloud】@EnableTransactionManagement注解的意义

    @EnableTransactionManagement注解的意义

  9. 阶段一-01.万丈高楼,地基首要-第2章 单体架构设计与准备工作-2-27 为何不使用@EnableTransactionManagement就能使用事务?

    使用了注解使用事务.但是没有开启注解的启用 启动类里面使用注解 @EnableTransactionManager开启事物的管理. 为什么我们没有开启这个注解,还需要在响应的Service里面使用事务 ...

随机推荐

  1. sql 添加变量

    在sql语句中添加变量. declare @local_variable data_type 声明时需要指定变量的类型, 可以使用set和select对变量进行赋值, 在sql语句中就可以使用@loc ...

  2. mybatis查询foreach使用

    1.mybatis传入map参数,map中包含list: List<FukaModel> fukaModels = price.getSchemaPrice().getFukaList() ...

  3. 【算法】php实现排序(一)

    选择排序方式:先让第一位与其他位比较大小找到最小的数字,然后是第二位与除第一位的其他位比较大小找出第二位,依此类推 $arr = [2,45,12,67,33,5,23,132,46]; for ($ ...

  4. 4.NIO的非阻塞式网络通信

    /*阻塞 和 非阻塞 是对于 网络通信而言的*/ /*原先IO通信在进行一些读写操作 或者 等待 客户机连接 这种,是阻塞的,必须要等到有数据被处理,当前线程才被释放*/ /*NIO 通信 是将这个阻 ...

  5. Eclipse安装windowsbuilder

    详见:https://www.cnblogs.com/plusplus/p/9864708.html https://www.cnblogs.com/lsy-blogs/p/7717036.html ...

  6. vSphere

    VMware vSphere集成容器(VIC)建立了一个在轻量级虚拟机内部署并管理容器的环境.全新的虚拟机环境提供了更高级别的硬件隔离度,灵活性以及可扩展性使得容器对开发人员以及企业应用具有如此大的吸 ...

  7. opengl 4.5 中文api 链接

    https://www.cnblogs.com/wiki3d/p/opengl_a.html

  8. win10激活密钥

    专业版:W269N-WFGWX-YVC9B-4J6C9-T83GX 企业版:NPPR9-FWDCX-D2C8J-H872K-2YT43 家庭版:TX9XD-98N7V-6WMQ6-BX7FG-H8Q9 ...

  9. 手写KMeans算法

    KMeans算法是一种无监督学习,它会将相似的对象归到同一类中. 其基本思想是: 1.随机计算k个类中心作为起始点. 将数据点分配到理其最近的类中心. 3.移动类中心. 4.重复2,3直至类中心不再改 ...

  10. nginx添加模块记录

    查看现有nginx的编译参数: [root@iZbp1d0dkjhfmxnxp7wuhmZ nginx-1.12.2]# nginx -Vnginx version: nginx/1.12.2buil ...