一尘不染

具有主要自动配置的Spring Boot辅助数据源

spring-boot

我正在尝试配置两个jpa数据源,我和本示例一样,一切都很好,但是有可能保持自动配置自动完成,而无需LocalContainerEntityManagerFactoryBean手动创建就可以添加一个新配置。

 @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;
}

阅读 358

收藏
2020-05-30

共1个答案

一尘不染

我不确定您的具体问题是什么,或者您希望完成什么,但是我将向您展示如何通过自动配置功能在Spring Boot中使用两个数据源:

为每个数据源创建配置(仅在单独的类中以更好地阅读):

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

@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;
   }       
}

注释每个DAO分别是PersistenceContext unitName

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();
}
}

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软件包命名,则应该能够同时使用这两个dataSource。

2020-05-30