springboot引入外部配置文件配置数据源

2017-10-19  本文已影响0人  jerrybw

最近遇到一个问题:手里有多个项目,数据库都是用的同一个,现在需要从测试环境搬到正式环境,于是就得挨个改配置文件,觉得太麻烦,于是找到了一个解决办法,暂时发现只适用于jpa,不适用与mybatis。在此记录一下。
1.在项目中新建配置类如下

@Configuration
@ComponentScan
@PropertySource(value = {"file:/application/.../datasource.properties","classpath:/application.properties"},ignoreResourceNotFound = true)
public class DataSourceConfig {
   @Bean
   @ConfigurationProperties(prefix="spring.datasource")
   public DataSource dataSource() {
       return new DriverManagerDataSource();
   }
}

2.在上面配置类中file所指向的位置放置一个datasource.properties文件,内容如下:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/subscribe?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
spring.datasource.username=yourName
spring.datasource.password=yourPwd
driverClassName=com.mysql.jdbc.Driver

至此,当需要改数据源的参数时,只需要在datasource.properties这个文件中进行修改即可

之后又遇到一个问题:
在一个项目中用到了两个不同的数据源。也同样在此记录一下解决方法。
1.在项目中新建配置类:

@Configuration
@ComponentScan
@PropertySource(value = {"file:/application/.../datasource.properties","classpath:/application.properties"},
        ignoreResourceNotFound = true)
public class DataSourceConfig {

    //配置第一个数据源
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="alert.spring.datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    //配置第二个数据源
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @Primary
    @ConfigurationProperties(prefix="alert.spring.datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

}

2.分别配置数据源会被哪里使用

//第一个数据源
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= { "com.jerry.work.dao.primary" }) //设置Repository所在位置
public class PrimaryConfig {

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

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

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(primaryDataSource)
                .properties(getVendorProperties(primaryDataSource))
                .packages("com.jerry.work.bean.primary") //设置实体类所在位置
                .persistenceUnit("primaryPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

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

}
//第二个数据源
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySecondary",
        transactionManagerRef="transactionManagerSecondary",
        basePackages= { "com.jerry.work.dao.secondary" }) //设置Repository所在位置
public class SecondaryConfig {

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

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

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
        return builder
                .dataSource(secondaryDataSource)
                .properties(getVendorProperties(secondaryDataSource))
                .packages("com.jerry.work.bean.secondary") //设置实体类所在位置
                .persistenceUnit("secondaryPersistenceUnit")
                .build();
    }

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, String> getVendorProperties(DataSource dataSource) {
        return jpaProperties.getHibernateProperties(dataSource);
    }

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

}

3.在上面配置类DataSourceConfig中file所指向的位置放置一个datasource.properties文件,内容如下:

# 第二个数据源
alert.spring.datasource.secondary.url=jdbc:mysql://127.0.0.1:3306/family?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
alert.spring.datasource.secondary.username=root
alert.spring.datasource.secondary.password=root
alert.spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
# 第一个数据源
alert.spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
alert.spring.datasource.primary.url=jdbc:mysql://127.0.0.1:3306/subscribe?useUnicode=true&characterEncoding=utf-8&autoReconnect=true
alert.spring.datasource.primary.username=root
alert.spring.datasource.primary.password=root

4.在每个数据源配置的类中指定的包下新建所对应的实体类与dao即可实现多数据源。

上一篇下一篇

猜你喜欢

热点阅读