从 HibernateJpaAutoConfiguration 中排除某些数据源 [英] Exclude certain DataSource(s) from HibernateJpaAutoConfiguration

查看:66
本文介绍了从 HibernateJpaAutoConfiguration 中排除某些数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 个数据源的项目.一种用于 PostgreSQL,另一种用于 ClickHouse.目前没问题.

I have a project with 2 DataSources. One is for PostgreSQL and other is for ClickHouse. No problem so far.

我打算仅通过 JDBC 将 ClickHouse 与本机 SQL 一起使用.

I intend to use ClickHouse with native SQL via JDBC only.

但我想将基于 JPA 的存储库与 PostgreSQL 数据源一起使用.如果我添加 spring-boot-starter-data-jpa 依赖项,HibernateJpaAutoConfiguration 会为 ALL 注册的 DataSource bean 启动.ClickHouse 不是事务关系数据库,它的 JDBC 实现非常有限和基本,从来没有打算与 Hibernate 一起使用.所以我的问题是:是否可以将 DataSourceAutoConfiguration 用于两个数据源,但以某种方式告诉 HibernateJpaAutoConfiguration 仅使用 PostgreSQL 数据源进行配置?

But I would like to use JPA based repositories with PostgreSQL datasource. And if I add spring-boot-starter-data-jpa dependency, HibernateJpaAutoConfiguration kicks in for ALL registered DataSource beans. ClickHouse is not a transactional relational DB and its JDBC implementation is very limited and basic, never intended for use with Hibernate. So my question is: is it possible to use DataSourceAutoConfiguration for both datasources, but somehow tell HibernateJpaAutoConfiguration to configure just with PostgreSQL datasource?

我仍然想使用自动配置好东西,比如 DataSource 配置和连接池,实际上只有几行属性文件.我知道我可以完全排除 HibernateJpaAutoConfiguration 并自行配置所有内容(entityManager、transactionManager...),但如果可能的话,我想避免这种情况.只是告诉它避免配置 ClickHouse 数据源似乎更优雅.

I would still like to use auto configuration goodies like DataSource configuration with Connection pooling with practically just a few lines in properties file. I know I can exclude HibernateJpaAutoConfiguration completely and configure everything (entityManager, transactionManager, ...) on my own, but I would like to avoid that if possible. Just telling it to avoid configuring ClickHouse datasource seems way more elegant.

我的一些假设是不正确的.阅读已接受的答案.很有见地.

Some of my assumptions were incorrect. Read the accepted answer. It is very insightful.

推荐答案

DataSourceAutoConfiguration 只有在你没有为数据源定义任何 bean 时才会启动.然后它只会创建和配置一个基于数据源的在 application.properties 中配置的设置上.这意味着如果您需要创建两个数据源,则不能用于这种情况,您必须手动定义两个数据源.你可以参考文档 了解如何通过模拟 DataSourceAutoConfiguration 的作用来配置多个数据源.

DataSourceAutoConfiguration will only kick in if you do not define any beans for the datasource .It will then create and configure only one datasource based on the setting configured in the application.properties. That means if you need to create two datasources ,it cannot be used for such case and you have to manually define both datasources. You can refer to the docs for how to configure multiple datasources by simulating what are done by the DataSourceAutoConfiguration.

另一方面,HibernateJpaAutoConfiguration 只有在定义了一个 Datasource bean 时才会启动(请参阅 代码,因为它标有 @ConditionalOnSingleCandidate)

On the other hand, HibernateJpaAutoConfiguration will only kick in if there is only one Datasource bean defined (see the codes as it is marked with @ConditionalOnSingleCandidate)

并且来自 javadoc:

And from the javadoc of @ConditionalOnSingleCandidate:

@Conditional 只匹配指定类的 bean已经包含在 BeanFactory 中,并且一个候选可以是确定.

@Conditional that only matches when a bean of the specified class is already contained in the BeanFactory and a single candidate can be determined.

如果有多个匹配的 bean 实例,条件也会匹配已包含在 BeanFactory 中,但主要候选对象已定义;本质上,条件匹配如果自动连接一个 bean定义的类型会成功.

The condition will also match if multiple matching bean instances are already contained in the BeanFactory but a primary candidate has been defined; essentially, the condition match if auto-wiring a bean with the defined type will succeed.

这意味着你可以简单地用 @Primary 标记你为 PostgreSQL 定义的 DataSource bean,HibernateJpaAutoConfiguration 应该只考虑它而忽略其他.

That mean you can simply mark the DataSource bean that you define for the PostgreSQL with @Primary and HibernateJpaAutoConfiguration should only consider it but ignore others.

这篇关于从 HibernateJpaAutoConfiguration 中排除某些数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆