配置了主要自动的 spring 引导辅助数据源 [英] spring boot secondary datasource with primary auto configured
问题描述
我正在尝试配置两个 jpa 数据源,我和 这个例子一样 ,一切都很好,但是否可以保持自动配置自动完成,只需添加一个新配置,而无需手动创建 LocalContainerEntityManagerFactoryBean
.
i am trying to configure two jpa datasource , i did same as this example , every thing is good but is it possible to keep the auto configuration done automatically and just add a new one without having to create LocalContainerEntityManagerFactoryBean
manually.
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory().getObject());
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(false);
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
factoryBean.setPackagesToScan("com.xxxxxxxx.common.domain","com.xxxxxxx.tekram.cdrserver.domain");
return factoryBean;
}
推荐答案
我不太确定您的具体问题是什么或您希望完成什么,但我将向您展示我如何在 Spring 中使用两个数据源使用自动配置功能启动:
I'm not really sure about what's your concrete problem or what you wish to acomplish, but I'll show you how I've used two dataSources with Spring Boot using the auto configuration features:
为每个数据源创建配置(在单独的类中只是为了更好地阅读):
Create config for every data source (in separate classes only to better reading):
PrimaryDbConfig.java
PrimaryDbConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.aoc.dao", entityManagerFactoryRef = "entityManager", transactionManagerRef = "transactionManager")
public class PrimaryDBConfiguration {
@Bean(name = "dataSource")
@Primary
@ConfigurationProperties(prefix = "primary.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "primary")
@Primary
@Bean(name = "entityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(dataSource())
.persistenceUnit("primary")
.packages("com.aoc.model")
.build();
}
}
SecondaryDbConfig.java
SecondaryDbConfig.java
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.aoc.dao", entityManagerFactoryRef = "secondaryEntityManager", transactionManagerRef = "secondaryTransactionManager")
public class SecondaryDBConfiguration {
@Bean
@ConfigurationProperties(prefix = "secondary.datasource")
public DataSource secDataSource() {
return DataSourceBuilder.create().build();
}
@PersistenceContext(unitName = "secondary")
@Bean(name = "secondaryEntityManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder.dataSource(secDataSource())
.persistenceUnit("secondary")
.packages("com.aoc.siri")
.build();
}
@Bean(name = "secondaryTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder) {
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(entityManagerFactory(builder).getObject());
tm.setDataSource(secDataSource());
return tm;
}
}
用它各自的PersistenceContext
unitName
来注释每个DAO:
Anotate every DAO with it's respective PersistenceContext
unitName
:
OneDaoImpl.java
OneDaoImpl.java
@Repository(value = "OneDaoImpl")
public class OneDaoImpl {
@PersistenceContext(unitName="primary")
private EntityManager manager;
public List<One> getAllOne() {
return (List<One>) manager.createQuery("FROM ONE", One.class).getResultList();
}
}
另一个DaoImpl.java
AnotherDaoImpl.java
@Repository(value = "anotherDaoImpl")
public class AnotherDaoImpl {
@PersistenceContext(unitName = "secondary")
private EntityManager manager;
public List<Another> getAllAnother() {
return (List<Another>) manager.createQuery("FROM Another", Another.class).getResultList();
}
}
然后在你的 src/main/resources/application.properties
primary.datasource.driver-class-name=...
primary.datasource.url=...
primary.datasource.username=xxx
primary.datasource.password=yyy
secondary.datasource.driver-class-name=...
secondary.datasource.url=...
secondary.datasource.username=zzz
secondary.datasource.password=xxx
您可能知道,如果您使用的是 推荐 Spring Boot 包命名 使用这些类和注释,您应该能够同时使用这两个数据源.
As you probably know if you are using the recommended Spring Boot packages naming with these classes and anotations you should be able to use that two dataSources at the same time.
这篇关于配置了主要自动的 spring 引导辅助数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!