<dependency>
<groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>
</dependency>
配置版事务
也就是jdbc的场景启动器,我们点进去看看
其实也是引⼊了spring-jdbc的依赖,接下来我们要看两个重要的事务⾃动配置类DataSourceTransactionManagerAutoConfiguration
我们看到在spring.factories中配置了事务管理器⾃动配置类DataSourceTransactionManagerAutoConfiguration,我们进去看看
1 @Configuration
2//在类路径下有这个类存在PlatformTransactionManager时,这个配置类才会⽣效
3//⽽前⾯我们已经引⼊了spring-boot-starter-jdbc,那⾃然是存在了
4 @ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class })
5 @AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
6 @EnableConfigurationProperties(DataSourceProperties.class)
7public class DataSourceTransactionManagerAutoConfiguration {
8
9 @Configuration
10 @ConditionalOnSingleCandidate(DataSource.class)
11static class DataSourceTransactionManagerConfiguration {
12
13private final DataSource dataSource;
14
15private final TransactionManagerCustomizers transactionManagerCustomizers;
16
17 DataSourceTransactionManagerConfiguration(DataSource dataSource,
18 ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
19this.dataSource = dataSource;
21 .getIfAvailable();
22 }
23
24 @Bean
25//没有当Spring容器中不存在PlatformTransactionManager这个对象时,创建DataSourceTransactionManager
26//也就是如果我们⾃定义了DataSourceTransactionManager并注⼊Spring容器,这⾥将不会执⾏
27 @ConditionalOnMissingBean(PlatformTransactionManager.class)
28public DataSourceTransactionManager transactionManager(DataSourceProperties properties) {
29//创建DataSourceTransactionManager注⼊Spring容器,并且把dataSource传进去
30 DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.dataSource);
31if (ansactionManagerCustomizers != null) {
33 }
34return transactionManager;
35 }
36
37 }
38
39 }
很明显只要我们导⼊了spring-boot-starter-jdbc场景启动器,并且我们没有⾃定义DataSourceTransactionManager,那么事务管理器⾃动配置类DataSourceTransactionManagerAutoConfiguration会⾃动为我们创建DataSourceTransactionManager并注⼊Spring容器中。但是这还不够,我们前⾯还是需要通过@EnableTransactionManagement开启事务呢,如果不开启事务,@Transactional是不起任何作⽤的。下⾯我们就来看看是如何开启事务的TransactionAutoConfiguration
我们看到在spring.factories中配置了事务⾃动开启配置类TransactionAutoConfiguration,我们进去看看
1 @Configuration
2//和DataSourceTransactionManagerAutoConfiguration中是⼀样的
3//引⼊了spring-boot-starter-jdbc,那⾃然是存在了PlatformTransactionManager
4 @ConditionalOnClass({PlatformTransactionManager.class})
5//这个⾃动配置类必须要在DataSourceTransactionManagerAutoConfiguration这个⾃动配置类之后才能⽣效
6//也就是前⾯我们已经往Spring容器中注⼊了DataSourceTransactionManager这个对象才执⾏这个配置类
7 @AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class})
8 @EnableConfigurationProperties({TransactionProperties.class})
9public class TransactionAutoConfiguration {
10public TransactionAutoConfiguration() {
11 }
11 }
12
13 @Configuration
14 @ConditionalOnBean({PlatformTransactionManager.class})
15 @ConditionalOnMissingBean({AbstractTransactionManagementConfiguration.class})
16public static class EnableTransactionManagementConfiguration {
17public EnableTransactionManagementConfiguration() {
18 }
19
20 @Configuration
21//重点:通过 @EnableTransactionManagement注解开启事务
22//可以看到和我们⾃⼰使⽤@EnableTransactionManagement是⼀样的
23 @EnableTransactionManagement(
24 proxyTargetClass = true
25 )
26 @ConditionalOnProperty(
27 prefix = "spring.aop",
28 name = {"proxy-target-class"},
29 havingValue = "true",
springboot框架的作用
30 matchIfMissing = true
31 )
32public static class CglibAutoProxyConfiguration {
33public CglibAutoProxyConfiguration() {
34 }
35 }
36
37 @Configuration
38 @EnableTransactionManagement(
39 proxyTargetClass = false
40 )
41 @ConditionalOnProperty(
42 prefix = "spring.aop",
43 name = {"proxy-target-class"},
44 havingValue = "false",
45 matchIfMissing = false
46 )
47public static class JdkDynamicAutoProxyConfiguration {
48public JdkDynamicAutoProxyConfiguration() {
49 }
50 }
51 }
52
53 @Configuration
54 @ConditionalOnSingleCandidate(PlatformTransactionManager.class)
55public static class TransactionTemplateConfiguration {
56private final PlatformTransactionManager transactionManager;
57
58public TransactionTemplateConfiguration(PlatformTransactionManager transactionManager) {
60 }
61
62 @Bean
63 @ConditionalOnMissingBean
64public TransactionTemplate transactionTemplate() {
65return new ansactionManager);
66 }
67 }
68 }
我们看到TransactionAutoConfiguration这个⾃动配置类必须要在DataSourceTransactionManagerAutoConfiguration这个配置类之后才能⽣效,也就是前⾯我们已经往Spring容器中注⼊了DataSourceTransactionManager这个对象才执⾏这个配置类,然后通过
@EnableTransactionManagement这个注解开启事务,其实和我们⾃⼰使⽤@EnableTransactionManagement是⼀样的
因此,只要我们在SpringBoot中引⼊了spring-boot-starter-jdbc这个场景启动器,就会帮我们⾃动开启事务了,我们只需要使⽤@Transactional就可以了mybatis-spring-boot-starter
⼤多数时候我们在SpringBoot中会引⼊Mybatis这个orm框架,Mybaits的场景启动器如下
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version>
</dependency>
我们点进去看看
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论