迁移Spring Boot时找不到Bean参数

时间:2019-03-09 20:14:57

标签: java spring spring-boot

我正在将服务从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.

我不知道发生了什么,也许由于弹簧启动中的更改或我的application.properties文件的原因,我需要添加另一个依赖项。问题是为什么会这样?我该如何更改才能使它正常工作?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

  
      
  • 未加载'KafkaAutoConfiguration'中的Bean方法'kafkaTransactionManager',因为@ConditionalOnProperty(spring.kafka.producer.transaction-id-prefix)找不到属性spring.kafka.producer.transaction-id-prefix
  •   

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