gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:12:29 24 4
gpt4 key购买 nike

好像我遗漏了一些东西:自动注入(inject)数据源有效,但注入(inject) 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 Boot 正在注册 PlatformTransactionManager bean,而您正在尝试注入(inject) DataSourceTransactionManager .如果您要更改为适当的类(class),它将开箱即用:

@Autowired
private PlatformTransactionManager transactionManager;

关于spring-boot:自动配置事务管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28664062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com