spring-boot: 自动配置事务管理器

spring-boot: auto configure transaction manager

好像我遗漏了什么:自动注入数据源有效,但注入 DataSourceTransactionManager 失败。

依赖关系:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.2.1.RELEASE</version>
</parent>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>

代码:

@SpringBootApplication
public class MainApplication {

  @Autowired
  private DataSource dataSource;

  // this fails
  @Autowired
  private DataSourceTransactionManager transactionManager;

  public static void main(String... args) {
    SpringApplication.run(MainApplication.class, args);
  }
}

我预计 DataSourceTransactionManagerAutoConfiguration 会处理它,但它没有。有什么线索吗?

采样时间为 github:https://github.com/jangalinski/springboot-playground

Spring 引导正在注册 PlatformTransactionManager bean,而您正在尝试注入 DataSourceTransactionManager。如果您更改为正确的 class,它将开箱即用:

@Autowired
private PlatformTransactionManager transactionManager;