我遇到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)
在我看来,config类在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)
答案 0 :(得分:0)
在春季启动升级到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中发布。
在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();
}
}