我对 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)