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

时间:2015-02-22 22:27:56

标签: spring datasource spring-boot

似乎我错过了一些东西:自动注入数据源工作,但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

1 个答案:

答案 0 :(得分:8)

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

@Autowired
private PlatformTransactionManager transactionManager;