DataSourceConfig.java 1.35 KB
package com.bsth;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author Hill
 */
@Configuration
public class DataSourceConfig {
    @Bean(name = "controlDataSource")
    @Qualifier("controlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.control")
    public DataSource controlDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "msDataSource")
    @Qualifier("msDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.ms")

    public DataSource msDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "controlJdbcTemplate")
    public JdbcTemplate controlJdbcTemplate(@Qualifier("controlDataSource")DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "msJdbcTemplate")
    public JdbcTemplate msJdbcTemplate(@Qualifier("msDataSource")DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
}