SpringBoot 在启动时已经加载了事务管理器,所以只需要在需要添加事务的方法/类上添加@Transactional即可生效,无需额外配置。
TransactionAutoConfiguration 事务的自动配置类解析:
SpringBoot 启动时加载/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports或者spring.factories文件中定义的所有自动配置类。其中包含了org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration事务自动配置类。

配置类中使用注解@ConditionalOnClass验证了PlatformTransactionManager.class接口的实例是否存在。接口的实现类如下:

接口的实现类中包含了DataSourceTransactionManager类,这个类在 SpringBoot 启动时通过org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration 配置类进行了导入,如下图。所以容器中就包含了PlatformTransactionManager.class类型的实例,所以TransactionAutoConfiguration 配置类正常加载。

TransactionAutoConfiguration 配置类中编写了一个内部类public static class EnableTransactionManagementConfiguration,其中包含了@EnableTransactionManagement注解,所以这也就是为什么启动类中不添加@EnableTransactionManagement注解事务也能生效的原因。








![[AI Google] 使用 Gemini 取得更多成就:试用 1.5 Pro 和更多智能功能](https://img-blog.csdnimg.cn/img_convert/0cf0182552ad70efe617676344c72211.png)










