Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot: auto configure transaction manager

Seems like I am missing something: Automatically injecting a data source works, but injection of DataSourceTransactionManager fails.

Dependencies:

<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>

Code:

@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);
  }
}

I expected that the DataSourceTransactionManagerAutoConfiguration would take care of it, but it didn't. Any clues?

The sample up is on github: https://github.com/jangalinski/springboot-playground

like image 617
Jan Galinski Avatar asked Feb 22 '15 22:02

Jan Galinski


1 Answers

Spring Boot is registering PlatformTransactionManager bean, and you're trying to inject DataSourceTransactionManager. If you'll change to proper class it will work out of the box:

@Autowired
private PlatformTransactionManager transactionManager;
like image 104
Jakub Kubrynski Avatar answered Oct 03 '22 19:10

Jakub Kubrynski