package com.fzzy.conf;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
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;
|
|
/**
|
* 数据源配置-单数据源
|
*/
|
@Configuration
|
public class DataSourcePrimary {
|
|
/**
|
* 扫描spring.datasource.primary开头的配置信息
|
*
|
* @return 数据源配置信息
|
*/
|
@Primary
|
@Bean(name = "primaryDataSourceProperties")
|
@ConfigurationProperties(prefix = "spring.datasource.primary")
|
public DataSourceProperties dataSourceProperties() {
|
return new DataSourceProperties();
|
}
|
|
|
/**
|
* 取主库数据源对象
|
*
|
* @param dataSourceProperties 注入名为primaryDataSourceProperties的bean
|
* @return 数据源对象
|
*/
|
@Primary
|
@Bean(name = "primaryDataSource")
|
public DataSource dataSource(@Qualifier("primaryDataSourceProperties") DataSourceProperties dataSourceProperties) {
|
return dataSourceProperties.initializeDataSourceBuilder().build();
|
}
|
|
|
/**
|
* 该方法仅在需要使用JdbcTemplate对象时选用
|
*
|
* @param dataSource 注入名为primaryDataSource的bean
|
* @return 数据源JdbcTemplate对象
|
*/
|
@Primary
|
@Bean(name = "primaryJdbcTemplate")
|
public JdbcTemplate jdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
|
return new JdbcTemplate(dataSource);
|
}
|
|
}
|