Last active
March 1, 2020 14:04
-
-
Save cg4jins/eeac0b4e7fac1e2863c72bad01038a6d to your computer and use it in GitHub Desktop.
DataSourceConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EntityScan(basePackages = ["com.beauty.domain"]) | |
@EnableJpaRepositories(basePackages = ["com.beauty.repository"]) | |
@EnableTransactionManagement | |
@EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class]) | |
class DataSourceConfig { | |
companion object { | |
const val MASTER = "master" | |
const val SLAVE = "slave" | |
} | |
@Bean(name = ["masterDataSource"]) | |
@ConfigurationProperties(prefix = "spring.datasource.master") | |
fun masterDataSource(): DataSource { | |
return DataSourceBuilder.create().type(HikariDataSource::class.java).build() | |
} | |
@Bean(name = ["slaveDataSource"]) | |
@ConfigurationProperties(prefix = "spring.datasource.slave") | |
fun slaveDataSource(): DataSource { | |
return DataSourceBuilder.create().type(HikariDataSource::class.java).build() | |
} | |
@Bean(name = ["routingDataSource"]) | |
fun routingDataSource(@Qualifier("masterDataSource") masterDataSource: DataSource, | |
@Qualifier("slaveDataSource") slaveDataSource: DataSource): DataSource { | |
val routingDataSource = ReplicationRoutingDataSource() | |
val dataSourceMap = HashMap<String, Any>() | |
dataSourceMap[MASTER] = masterDataSource | |
dataSourceMap[SLAVE] = slaveDataSource | |
routingDataSource.setTargetDataSources(dataSourceMap.toMap()) | |
routingDataSource.setDefaultTargetDataSource(masterDataSource) | |
return routingDataSource | |
} | |
@Primary | |
@Bean | |
fun dataSource(@Qualifier("routingDataSource") routingDataSource: DataSource): DataSource { | |
return LazyConnectionDataSourceProxy(routingDataSource) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment