我想为我的spring boot项目配置两个数据源,但是在两个数据库中我有相同的实体,所以我问是否有可能将这些实体放在一个包中并初始化存储库中的特定数据源。
这是我的配置:
@Configuration
@EnableAutoConfiguration
public class NwlConfiguration {
@Bean
@Primary
@ConfigurationProperties("spring.datasource")
public DataSourceProperties source1DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "source1")
@Primary
@ConfigurationProperties("spring.datasource")
public DataSource source1DataSource() {
return source1DataSourceProperties().initializeDataSourceBuilder().build();
}
@Bean
@ConfigurationProperties("source2.datasource")
public DataSourceProperties source2DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "source2")
@ConfigurationProperties("source2.datasource")
public DataSource source2DataSource() {
return source2DataSourceProperties().initializeDataSourceBuilder().build();
}
}
答案 0 :(得分:0)
您的配置看起来还可以。唯一的事情是您需要分配将用于哪个存储库的配置。
如果有多个实体,可以将它们放在一个公共包中。
喜欢
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager", basePackages = {"com.foobar.bar.repo"})
public class BarDbConfig {
完整代码
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "barEntityManagerFactory",
transactionManagerRef = "barTransactionManager", basePackages = {"com.foobar.bar.repo"})
public class BarDbConfig {
@Bean(name = "barDataSource")
@ConfigurationProperties(prefix = "bar.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "barEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean barEntityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("barDataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.foobar.bar.domain").persistenceUnit("bar")
.build();
}
@Bean(name = "barTransactionManager")
public PlatformTransactionManager barTransactionManager(
@Qualifier("barEntityManagerFactory") EntityManagerFactory barEntityManagerFactory) {
return new JpaTransactionManager(barEntityManagerFactory);
}
}
第二个数据源配置类
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"com.foobar.foo.repo"})
public class FooDbConfig {
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.foobar.foo.domain").persistenceUnit("foo")
.build();
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
有关更多详细信息,请参见this article