-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
33 deletions.
There are no files selected for viewing
80 changes: 65 additions & 15 deletions
80
src/main/java/com/ayucoupon/common/config/datasource/DataSourceConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,80 @@ | ||
package com.ayucoupon.common.config.datasource; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import com.ayucoupon.common.aop.multidatasource.MyRoutingDataSource; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
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.DependsOn; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(DataSourceConfigProperties.class) | ||
@RequiredArgsConstructor | ||
public class DataSourceConfig { | ||
|
||
private final DataSourceConfigProperties properties; | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
HikariConfig hikariConfig = new HikariConfig(); | ||
hikariConfig.setDriverClassName(properties.getDriverClassName()); | ||
hikariConfig.setJdbcUrl(properties.getUrl()); | ||
hikariConfig.setUsername(properties.getUserName()); | ||
hikariConfig.setPassword(properties.getPassword()); | ||
hikariConfig.setMaximumPoolSize(properties.getMaximumPoolSize()); | ||
return new HikariDataSource(hikariConfig); | ||
private final String PRIMARY_DATA_SOURCE = "primary"; | ||
private final String SECONDARY_DATA_SOURCE = "secondary"; | ||
private final String ROUTING_DATA_SOURCE = "routingDataSource"; | ||
|
||
@Bean(PRIMARY_DATA_SOURCE) | ||
@ConfigurationProperties(prefix = "spring.datasource.primary") | ||
public DataSource primaryDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean(SECONDARY_DATA_SOURCE) | ||
@ConfigurationProperties(prefix = "spring.datasource.secondary") | ||
public DataSource secondaryDataSource() { | ||
return DataSourceBuilder.create().build(); | ||
} | ||
|
||
@Bean(ROUTING_DATA_SOURCE) | ||
@Primary | ||
@DependsOn({PRIMARY_DATA_SOURCE, SECONDARY_DATA_SOURCE}) | ||
public DataSource routingDataSource( | ||
@Qualifier(PRIMARY_DATA_SOURCE) DataSource primary, | ||
@Qualifier(SECONDARY_DATA_SOURCE) DataSource secondary) { | ||
MyRoutingDataSource routingDataSource = new MyRoutingDataSource(); | ||
|
||
Map<Object, Object> dataSourceMap = new HashMap<>(); | ||
|
||
dataSourceMap.put(PRIMARY_DATA_SOURCE, primary); | ||
dataSourceMap.put(SECONDARY_DATA_SOURCE, secondary); | ||
|
||
routingDataSource.setTargetDataSources(dataSourceMap); | ||
routingDataSource.setDefaultTargetDataSource(primary); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@Bean("entityManagerFactory") | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactory( | ||
@Qualifier(ROUTING_DATA_SOURCE) DataSource dataSource) { | ||
LocalContainerEntityManagerFactoryBean entityManagerFactory | ||
= new LocalContainerEntityManagerFactoryBean(); | ||
entityManagerFactory.setDataSource(dataSource); | ||
entityManagerFactory.setPackagesToScan("com.ayucoupon"); | ||
entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); | ||
entityManagerFactory.setPersistenceUnitName("entityManager"); | ||
return entityManagerFactory; | ||
} | ||
|
||
@Bean("transactionManager") | ||
public PlatformTransactionManager platformTransactionManager( | ||
@Qualifier("entityManagerFactory") LocalContainerEntityManagerFactoryBean entityManagerFactory) { | ||
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); | ||
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory.getObject()); | ||
return jpaTransactionManager; | ||
} | ||
|
||
} |
18 changes: 0 additions & 18 deletions
18
src/main/java/com/ayucoupon/common/config/datasource/DataSourceConfigProperties.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
src/main/java/com/ayucoupon/common/config/warmup/AopConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ayucoupon.common.config.warmup; | ||
|
||
import com.ayucoupon.common.aop.multidatasource.DataSourceAspect; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AopConfig { | ||
|
||
@Bean | ||
public DataSourceAspect dataSourceAspect() { | ||
return new DataSourceAspect(); | ||
} | ||
|
||
} |