相信使用过SpringBoot框架的朋友们,应该很熟悉里面如何使用事务,只要在service实现类里面的方法上面添加一个@Transcation的注解,然后框架就会自动管理我们的事务,那么这个SpringBoot是如何做到这一点的呢,让我们一起来看看源码
1.首先来看看DataSourceTransactionManagerAutoConfiguration这个类
这个类实例化的实际也是利用了SpringBoot的自动装配原理,这里就不过多解释,因为我们在使用事务的时候已经引入了JdbcTemplate,PlatformTransactionManager这两个类,所以这里的条件是成立的,在SpringBoot启动的时候会刷新Spring容器上下文,这里就会生成DataSourceTransactionManagerConfiguration这个类
2.第二个重要的配置类TransactionAutoConfiguration
/* * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 *