I have a Spring-boot app that writes to database using spring-data and jpa. It has a configuration class annotated with @EnableJpaRepositories
and @EnabledTransactionManagement
which wires up the data sources and other bits:
@Configuration
@EnableJpaRepositories( entityManagerFactoryRef=..., transactionManagerRef=..., ... )
@EnableTransactionManagement
public class DatasourceConfiguration {
@Bean(destroyMethod = "close")
@Primary
@ConfigurationProperties("db.pooling")
ComboPooledDataSource pooledDataSource() {
return new ComboPooledDataSource();
}
// etc
}
Now I want to update my app so that it will instead log its transactions using a different methodology (writing to Kafka, not important.) If this other methodology is disabled, however, I want it to fall back on the original direct-to-database logic.
I attempted using the @ConditionalOnProperty
annotation, because the javadoc indicates that it can annotate a @Configuration
class:
@ConditionalOnProperty(prefix="kafka.logging", name="enabled", havingValue = "false", matchIfMissing = true)
@Configuration
@EnableJpaRepositories( ... )
@EnableTransactionManagement
public class DatasourceConfiguration {
However the @EnableJpaRepositories
and @EnableTransactionManagement
do not honor the conditional annotation. (Which is to say, it fails on startup because Spring can't find any configured data sources.)
How can I only enable Spring data and JPA iff this property is missing or set to false?
(Please don't suggest using profiles. I can't use profiles. It's a policy thing, not a choice thing.)