我有一个连接到两个数据库的 Spring Boot 应用程序。 一切正常。
我的包结构如下所示
- db
- bar
- BarDbConfig.java
- domain
- BarModel.java
- repo
- BarRepo.java
- foo
- FooDbConfig.java
- domain
- FooModel.java
- repo
- FooRepo.java
应用程序属性
spring.jpa.database=default
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
#first db
bar.datasource.jdbc-url=jdbc:h2:C:\\Project\\com.example\\db3.data
bar.datasource.username=admin
bar.datasource.password=admin
bar.datasource.driver-class-name=org.h2.Driver
#second db
foo.datasource.jdbc-url=jdbc:h2:C:\\Project\\com.example\\db2.data
foo.datasource.username=admin
foo.datasource.password=admin
foo.datasource.driver-class-name=org.h2.Driver
BarDbConfig.java
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager"
)
public class BarDbConfig {
@Bean
@Primary
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource barDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("barDataSource") DataSource dataSource
) {
return builder.dataSource(dataSource)
.packages(BarDbConfig.class.getPackage().getName())
.persistenceUnit("barPersistenceUnit")
.build();
}
@Primary
@Bean
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
FooDbConfig.java
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "fooEntityManagerFactory",
transactionManagerRef = "fooTransactionManager"
)
public class FooDbConfig {
@Bean
@ConfigurationProperties(prefix = "foo.datasource")
public DataSource fooDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean fooEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("fooDataSource") DataSource dataSource
) {
return builder.dataSource(dataSource)
.packages(FooDbConfig.class.getPackage().getName())
.persistenceUnit("fooPersistenceUnit")
.build();
}
@Bean
public PlatformTransactionManager fooTransactionManager(
@Qualifier("fooEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
但是,我想知道所有这些样板代码是否真的有必要?
- 有没有办法删除所有
LocalContainerEntityManagerFactoryBean
和PlatformTransactionManager
内容? - 是否有任何理由需要提供所有这些显式
限定符
名称? - Spring Boot 无法自行查找并自动配置数据源吗?
- 利用约定优于配置连接两个数据库所需的绝对最少代码是多少?
最佳答案
我将回答您在标题中提出的一个问题,然后在问题正文中再次回答:
Can't spring boot find and autoconfigure the
DataSource
s by itself?
99% 的应用程序使用同一个数据库,在这些情况下,您想要发生的情况非常明显,而这正是 Boot 为您所做的。
如果您的应用程序中有 2 个数据库,则许多问题没有合理的默认答案:
哪个存储库应该使用哪个DataSource
?按包分割?每个接口(interface)都有两个存储库?有路由DataSource
吗?它应该按照什么标准路由请求?
您想要跨多个DataSource
还是单独的数据源进行一项事务?或者也许两者都适用于不同的用例?
当然,Boot 可以为所有这些问题选择一个可能的答案,但结果只会对已经很小的开发人员群体中的一小部分有帮助。 因此它还没有完成。
当然,如果您发现某些事情几乎总是以某种方式完成,我相信 Spring Boot 开发人员会欣赏功能请求。
关于java - 为什么spring boot/data不能自动配置多个数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54772546/