【问题标题】:spring-boot: auto configure transaction managerspring-boot:自动配置事务管理器
【发布时间】:2015-04-24 04:52:04
【问题描述】:

似乎我遗漏了一些东西:自动注入数据源有效,但注入 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 datasource spring-boot


    【解决方案1】:

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

    @Autowired
    private PlatformTransactionManager transactionManager;
    

    【讨论】:

    • PlatformTransactionManager 在 Spring Boot 中可以自动配置吗?因为我们需要在 Spring 框架中创建 bean。
    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 2017-05-29
    • 2010-10-18
    • 2022-01-27
    • 2019-07-01
    • 2014-02-22
    • 2011-07-05
    相关资源
    最近更新 更多