0

我对 Spring Boot 版本 2.0.1.RELEASE 有疑问(在 Spring Boot 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)

在我看来,配置类是之前评估过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)
4

1 回答 1

0

spring boot 升级到版本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 Boot 版本 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();
    }

}
于 2019-02-16T15:04:32.993 回答