Spring Boot 1.0多数据源配置

版权声明:请附链接,自由转载 https://blog.csdn.net/kangkanglou/article/details/82684536

在Spring Boot 1.x下,我们的多数据源配置如下:

读数据库

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {
        "cn.cib.repository.read"})
public class RepositoryPrimaryConfig {
    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    @Qualifier("r_ds")
    private DataSource r_ds;

    @Bean(destroyMethod = "", name = "entityManagerPrimary")
    @Primary
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder)
                .getObject()
                .createEntityManager();
    }

    @Bean(destroyMethod = "", name = "entityManagerFactoryPrimary")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(r_ds)
                .properties(getVendorProperties(r_ds))
                .packages("cn.cib.entity.read")
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        //for spring boot 1.x
        return jpaProperties.getHibernateProperties(dataSource);
        //for spring boot 2.x
        //return jpaProperties.getProperties();
    }

    @Bean(destroyMethod = "", name = "transactionManagerPrimary")
    @Primary
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }

}

写数据库

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary", transactionManagerRef = "transactionManagerSecondary", basePackages = {
        "cn.cib.repository.write"})
public class RepositorySecondaryConfig {
    @Autowired
    private JpaProperties jpaProperties;

    @Autowired
    @Qualifier("w_ds")
    private DataSource w_ds;

    @Bean(destroyMethod = "", name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Bean(destroyMethod = "", name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
        return builder.dataSource(w_ds)
                .properties(getVendorProperties(w_ds))
                .packages("cn.cib.entity.write")
                .persistenceUnit("secondaryPersistenceUnit").build();
    }

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        //for spring boot 1.x
        return jpaProperties.getHibernateProperties(dataSource);
        //for spring boot 2.x
        //return jpaProperties.getProperties();
    }

    @Bean(destroyMethod = "", name = "transactionManagerSecondary")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }

}

注意:Spring Boot 2.x对org.springframework.boot.autoconfigure.orm.jpa.JpaProperties类进行了调整:

  • Spring Boot 2.x 中有关JpaPropertiesgetHibernateProperties方法定义:
    public Map<String, Object> getHibernateProperties(HibernateSettings settings) {
        return this.hibernate.getAdditionalProperties(this.properties, settings);
    }
  • Spring Boot 1.x 中有关JpaPropertiesgetHibernateProperties方法定义:
    /**
     * Get configuration properties for the initialization of the main Hibernate
     * EntityManagerFactory.
     * @param dataSource the DataSource in case it is needed to determine the properties
     * @return some Hibernate properties for configuration
     */
    public Map<String, String> getHibernateProperties(DataSource dataSource) {
        return this.hibernate.getAdditionalProperties(this.properties, dataSource);
    }

相比于1.x,Spring Boot 2.x对getHibernateProperties函数参数进行了调整,由DataSource 换成了HibernateSettings

所以,如果我们要从Spring Boot 1.x升级到Spring Boot 2.x,那么,我们需要修改getVendorProperties方法如下:

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        //for spring boot 1.x
        //return jpaProperties.getHibernateProperties(dataSource);
        //for spring boot 2.x
        return jpaProperties.getProperties();
    }

关于Spring Boot 2.x另一种多数据源配置方式,也可参考我之前的一篇文章:Spring Boot 2.0 多数据源配置

更多源码请参考链接:

https://github.com/ypmc/spring-cloud/tree/master/spring-batch

猜你喜欢

转载自blog.csdn.net/kangkanglou/article/details/82684536