I'm working on a library that is consumed by some other project. The library offers database access through JDBC and I'd like to add support for R2DBC too in the same library. The consuming project should be able to switch between JDBC and R2DBC based on a configuration property.
The problem that I'm facing is that the R2DBC auto-configuration provided by spring-boot-starter-data-r2dbc
(2.5.4) overrides the JDBC configuration and the consuming project can only work with R2DBC.
Further more, when the project is being built, there are certain tasks like documentation or code generation, tests, etc, that depend on the spring context being loaded but do not require database access. These tasks fail because the context cannot be loaded due to the missing R2DBC properties:
BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$Pool.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [io.r2dbc.pool.ConnectionPool]: Factory method 'connectionFactory' threw exception;
nested exception is org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryOptionsInitializer$ConnectionFactoryBeanCreationException: Failed to determine a suitable R2DBC Connection URL
Of course, I could specify the required properties, but it doesn't feel right to load components I don't use. I would like to disable R2DBC completely (much like you can disable Vault support with spring.cloud.vault.enabled=false
) and only load it when I need it. Any ideas on how to do that?