gpt4 book ai didi

java - 迁移spring boot时找不到Bean参数

转载 作者:行者123 更新时间:2023-12-02 10:09:47 24 4
gpt4 key购买 nike

我正在将服务从 Spring Boot 1.5 迁移到 Spring Boot 2.1,在此过程中遇到错误。我有以下类来配置我的 spring bean:

@Configuration
public class CompanyTransactionConfiguration {
public CompanyTransactionConfiguration() {
}

@Bean
public TransactionTaskRunner transactionTaskRunner(PlatformTransactionManager transactionManager) {
return new TransactionTaskRunnerImpl(this.readWriteTransactionTemplate(transactionManager), this.readOnlyTransactionTemplate(transactionManager), this.newReadWriteTransactionTemplate(transactionManager));
}
}

当然,还有一个测试类来检查一切是否按预期工作:

@RunWith(SpringRunner.class)
public class ReferrerActivityRepositoryIT extends AbstractDomainIT {

@Autowired
private ReferrerActivityRepository referrerActivityRepository;

@Autowired
private TransactionTaskRunner transactionTaskRunner;

...
}

问题是,在我将依赖项更改为较新的 Spring Boot 版本 (2.1) 后,此测试工作正常,但现在出现以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method transactionTaskRunner in com.company.core.server.config.CompanyTransactionConfiguration required a bean of type 'org.springframework.transaction.PlatformTransactionManager' that could not be found.

The following candidates were found but could not be injected:
- Bean method 'transactionManager' in 'DataSourceTransactionManagerAutoConfiguration.DataSourceTransactionManagerConfiguration' not loaded because @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans
- Bean method 'kafkaTransactionManager' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnProperty (spring.kafka.producer.transaction-id-prefix) did not find property 'spring.kafka.producer.transaction-id-prefix'

...


Action:

Consider revisiting the entries above or defining a bean of type 'org.springframework.transaction.PlatformTransactionManager' in your configuration.

我不知道发生了什么,也许由于 Spring Boot 的更改,我需要添加另一个依赖项或更改我的 application.properties 文件。问题是为什么会发生这种情况?我应该更改什么才能使其正常工作?

谢谢!

最佳答案

您没有定义PlatformTransactionManager bean。我猜你不想自己做。您必须将 spring.kafka. Producer.transaction-id-prefix 属性添加到属性文件中才能将 KafkaAutoConfiguration 用于 PlatformTransactionManager 。

  • Bean method 'kafkaTransactionManager' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnProperty (spring.kafka.producer.transaction-id-prefix) did not find property spring.kafka.producer.transaction-id-prefix

顺便说一句,只要您的 CompanyTransactionConfiguration 构造函数没有参数,它就是多余的。如果类中没有构造函数,编译器将创建一个不带参数的默认构造函数。

关于java - 迁移spring boot时找不到Bean参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55081542/

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