Spring 引导中的自定义自动配置无法使用 ConditionalOnBean 找到数据源

Custom Auto Configuration in Spring Boot cannot find DataSource with ConditionalOnBean

我在使用 Spring 引导版本 2.0.1.RELEASE 时遇到问题(在 Spring 引导 2.0.0.RELEASE 中,以下配置运行良好)。

有人可以帮我解决为什么这个配置不起作用吗? 我有这个配置,这个配置不符合 ConditionalOnBean 条件。

AutoConfigureOrder / Order / AutoConfigureAfter 似乎没有任何效果。

@Configuration
@ConditionalOnClass({DataSource.class, JpaRepository.class})
@ConditionalOnBean(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureOrder(LOWEST_PRECEDENCE)
@Order(LOWEST_PRECEDENCE)
public class MyJpaAuditAutoConfiguration {

    @ConditionalOnMissingBean
    @Bean
    public MyTransactionHelper transactionHelper() {
        return new MyTransactionHelper();
    }

}

spring.factories 我有:

     org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.example.MyJpaAuditAutoConfiguration

此配置的应用程序启动输出:

DataSourceAutoConfiguration matched:
  - @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

...

JpaAuditAutoConfiguration:
  Did not match:
     - @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans of type javax.sql.DataSource (OnBeanCondition)
  Matched:
     - @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.data.jpa.repository.JpaRepository'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
     - @ConditionalOnProperty (spring.data.jpa.repositories.enabled=true) matched (OnPropertyCondition)

在我看来,配置 class 在 DataSourceAutoConfiguration 之前被评估,这就是找不到 bean 的原因。 DataSourceHealthIndicatorAutoConfiguration 等其他配置找到了 bean。

DataSourceHealthIndicatorAutoConfiguration matched:
  - @ConditionalOnClass found required classes 'org.springframework.jdbc.core.JdbcTemplate', 'org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
  - @ConditionalOnEnabledHealthIndicator management.health.defaults.enabled is considered true (OnEnabledHealthIndicatorCondition)
  - @ConditionalOnBean (types: javax.sql.DataSource; SearchStrategy: all) found bean 'dataSource' (OnBeanCondition)

spring开机升级到2.1.2.RELEASE版本后自动解决

(我在回答中重命名了一些类)

所以在 Spring Boot version 2.1.2.RELEASE:

@Configuration
@ConditionalOnBean(DataSource.class)
@ConditionalOnClass({DataSource.class, JpaRepository.class})
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class JpaTransactionHelperAutoConfiguration {

    @ConditionalOnMissingBean
    @Bean
    public TransactionHelper transactionHelper() {
        return new TransactionHelper();
    }

}

注意:通过删除 @AutoConfigureAfter(DataSourceAutoConfiguration.class),它将无法在 Spring 引导版本 2.1.2.RELEASE.

中工作

Spring Boot version 2.0.1.RELEASE 中,我刚刚删除了 ConditionalOnBean 注释:

@Configuration
@ConditionalOnClass({DataSource.class, JpaRepository.class})
@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class JpaTransactionHelperAutoConfiguration {

    @ConditionalOnMissingBean
    @Bean
    public TransactionHelper transactionHelper() {
        return new TransactionHelper();
    }

}