Rod*_*eas 5 spring-data-jpa spring-boot
我有一个 Spring-boot 应用程序,它使用 spring-data 和 jpa 写入数据库。它有一个用@EnableJpaRepositories和注释的配置类@EnabledTransactionManagement,它连接数据源和其他位:
@Configuration
@EnableJpaRepositories( entityManagerFactoryRef=..., transactionManagerRef=..., ... )
@EnableTransactionManagement
public class DatasourceConfiguration {
@Bean(destroyMethod = "close")
@Primary
@ConfigurationProperties("db.pooling")
ComboPooledDataSource pooledDataSource() {
return new ComboPooledDataSource();
}
// etc
}
Run Code Online (Sandbox Code Playgroud)
现在我想更新我的应用程序,以便它将使用不同的方法记录其事务(写入 Kafka,并不重要)。但是,如果禁用此其他方法,我希望它能够退回到原始的直接到-数据库逻辑。
我尝试使用@ConditionalOnProperty注释,因为javadoc表明它可以注释一个@Configuration类:
@ConditionalOnProperty(prefix="kafka.logging", name="enabled", havingValue = "false", matchIfMissing = true)
@Configuration
@EnableJpaRepositories( ... )
@EnableTransactionManagement
public class DatasourceConfiguration {
Run Code Online (Sandbox Code Playgroud)
然而@EnableJpaRepositories和@EnableTransactionManagement不遵守条件注释。(也就是说,启动失败,因为Spring找不到任何配置的数据源。)
如果此属性丢失或设置为 false,如何才能仅启用 Spring 数据和 JPA?
(请不要建议使用配置文件。我不能使用配置文件。这是一个政策问题,而不是一个选择问题。)
小智 2
如果你想告诉 JPA 不要创建存储库 bean,你可以将 @Conditional 放在 Repository 接口上,而不是放在配置类上。
然后 RepositoryComponentProvider 将遵循您的设置。
| 归档时间: |
|
| 查看次数: |
2274 次 |
| 最近记录: |