【发布时间】:2022-01-10 15:54:41
【问题描述】:
我正在开发一个被其他项目使用的库。该库通过 JDBC 提供数据库访问,我也想在同一个库中添加对 R2DBC 的支持。消费项目应该能够基于配置属性在 JDBC 和 R2DBC 之间切换。
我面临的问题是spring-boot-starter-data-r2dbc (2.5.4) 提供的 R2DBC 自动配置覆盖了 JDBC 配置,而消费项目只能使用 R2DBC。
此外,在构建项目时,某些任务(如文档或代码生成、测试等)取决于正在加载的 spring 上下文,但不需要访问数据库。这些任务因缺少 R2DBC 属性而无法加载上下文而失败:
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
当然,我可以指定所需的属性,但是加载我不使用的组件感觉不对。我想完全禁用 R2DBC(就像您可以使用 spring.cloud.vault.enabled=false 禁用 Vault 支持一样)并且仅在需要时才加载它。关于如何做到这一点的任何想法?
【问题讨论】:
-
Profiles,兄弟! (详情请多发帖..)
-
图书馆是做什么的?因为听起来 jooq.org 已经做了你需要的事情......
-
@ConditionalOnBean可能有助于确定要实例化哪些(您的)bean。 -
@SpringBootApplication(exclude=R2dbcAutoConfiguration.class)...@Profile("with")@Configuration@Import(R2dbcAutoConfiguration.class);) -
@xerx593 我忽略了
@SpringBootApplication(exclude=R2dbcAutoConfiguration.class),因为我需要在库中进行配置(该库还控制用于构建项目的 gradle 插件)。阅读文档我发现排除也可以指定为属性:spring.autoconfigure.exclude- 这看起来很有希望。感谢您的提示,我明天在工作中测试它并返回结果。
标签: java spring spring-boot spring-data-r2dbc r2dbc