multi-datasource 지원 추가

master
mjkhan21 4 months ago
parent 036ffbb552
commit 39f6e58284

@ -0,0 +1,124 @@
package cokr.xit.foundation.boot;
import java.util.List;
import java.util.Properties;
import java.util.stream.Stream;
import javax.sql.DataSource;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.egovframe.rte.psl.dataaccess.mapper.MapperConfigurer;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.paging.MapperSupport;
/**
* <ul><li>{@link #dataSource() } </li>
* <li>{@link #sqlSession() MyBatis} </li>
* <li>{@link #mapperConfigurer() } </li>
* </ul>
* @author mjkhan
*/
public abstract class AbstractDatasource extends AbstractComponent {
private DataSource dataSource;
/** Bean . JDBC application.yml .
* <pre><code> spring:
* datasource:
* hikari:
* driver-class-name: JDBC
* jdbc-url: URL
* username:
* password: </code></pre>
* @return
*/
protected DataSource dataSource() {
return dataSource != null ? dataSource : (dataSource = DataSourceBuilder.create().build());
}
/**SqlSessionFactoryBean .<br />
* MyBatis .
* <ul><li>MyBatis : classpath:sql/mybatis-config.xml</li>
* <li> xml: classpath:sql/mapper&#47;**&#47;*.xml</li>
* </ul>
* @return SqlSessionFactoryBean
*/
protected SqlSessionFactoryBean sqlSession() {
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setConfigLocation(resolver.getResource("classpath:sql/mybatis-config.xml"));
Resource[] mapperLocations = getMapperLocations(resolver);
if (!isEmpty(mapperLocations))
bean.setMapperLocations(mapperLocations);
bean.setPlugins(new MapperSupport());
bean.setDatabaseIdProvider(databaseIdProvider());
return bean;
} catch (Exception e) {
throw Assert.runtimeException(e);
}
}
/** .
* @param resolver PathMatchingResourcePatternResolver
* @return
* @throws Exception
*/
private Resource[] getMapperLocations(PathMatchingResourcePatternResolver resolver) throws Exception {
List<Resource> resources = Stream.of(mapperLocationPatterns())
.flatMap(location -> {
try {
return Stream.of(resolver.getResources(location));
} catch (Exception e) {
throw runtimeException(e);
}
})
.toList();
return resources.toArray(new Resource[resources.size()]);
}
/** .
* @return
*/
protected String[] mapperLocationPatterns() {
return new String[] {"classpath:sql/mapper/**/*.xml"};
}
/**MapperConfigurer .<br />
* base package cokr.xit .
* @return MapperConfigurer
*/
protected MapperConfigurer mapperConfigurer() {
MapperConfigurer bean = new MapperConfigurer();
String basePackages = mapperBasePackages();
if (!isEmpty(basePackages))
bean.setBasePackage(basePackages);
bean.setSqlSessionFactoryBeanName(sqlSessionName());
return bean;
}
protected String mapperBasePackages() {
return "cokr.xit";
}
protected String sqlSessionName() {
return "sqlSession";
}
protected VendorDatabaseIdProvider databaseIdProvider() {
VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
Properties properties = new Properties();
properties.put("MariaDB", "mariadb");
properties.put("Oracle", "oracle");
databaseIdProvider.setProperties(properties);
return databaseIdProvider;
}
}

@ -0,0 +1,73 @@
package cokr.xit.foundation.boot;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;
/** .
* <p> cokr.xit..service.bean..*ServiceBean .
* @author mjkhan
*/
public class AbstractTransaction {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
protected void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
protected DataSourceTransactionManager txManager() {
DataSourceTransactionManager bean = new DataSourceTransactionManager();
bean.setDataSource(getDataSource());
return bean;
}
protected TransactionInterceptor txAdvice() {
RuleBasedTransactionAttribute read = new RuleBasedTransactionAttribute(
TransactionDefinition.PROPAGATION_REQUIRED,
List.of(new RollbackRuleAttribute(Throwable.class))
);
read.setReadOnly(true);
RuleBasedTransactionAttribute write = new RuleBasedTransactionAttribute(
TransactionDefinition.PROPAGATION_REQUIRED,
List.of(new RollbackRuleAttribute(Throwable.class))
);
NameMatchTransactionAttributeSource txAttrSrc = new NameMatchTransactionAttributeSource();
txAttrSrc.setNameMap(Map.of(
"get*", read,
"*", write
));
TransactionInterceptor bean = new TransactionInterceptor();
bean.setTransactionManager(txManager());
bean.setTransactionAttributeSource(txAttrSrc);
return bean;
}
protected Advisor txAdvisor() {
String expression = Foundation.transactionPointcut();
AspectJExpressionPointcut requiredTx = new AspectJExpressionPointcut();
requiredTx.setExpression(expression);
DefaultPointcutAdvisor bean = new DefaultPointcutAdvisor();
bean.setPointcut(requiredTx);
bean.setAdvice(txAdvice());
return bean;
}
}

@ -9,7 +9,6 @@ import java.util.stream.Collectors;
import org.egovframe.rte.fdl.cmmn.trace.LeaveaTrace;
import org.egovframe.rte.fdl.property.impl.EgovPropertyServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.util.AntPathMatcher;
@ -21,7 +20,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author mjkhan
*/
@Configuration
@ComponentScan(basePackages = "cokr.xit")
public class CommonConfig {
/**AntPathMatcher .
* @return AntPathMatcher

@ -1,20 +1,13 @@
package cokr.xit.foundation.boot;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.egovframe.rte.psl.dataaccess.mapper.MapperConfigurer;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import cokr.xit.foundation.Assert;
import cokr.xit.foundation.data.paging.MapperSupport;
/**
* <ul><li>{@link #dataSource() } </li>
@ -24,9 +17,7 @@ import cokr.xit.foundation.data.paging.MapperSupport;
* @author mjkhan
*/
@Configuration
public class DatasourceConfig {
private DataSource dataSource;
public class DatasourceConfig extends AbstractDatasource {
/** Bean . JDBC application.yml .
* <pre><code> spring:
* datasource:
@ -37,10 +28,11 @@ public class DatasourceConfig {
* password: </code></pre>
* @return
*/
@Override
@Bean("dataSource")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource() {
return dataSource != null ? dataSource : (dataSource = DataSourceBuilder.create().build());
return super.dataSource();
}
/**SqlSessionFactoryBean .<br />
@ -50,42 +42,25 @@ public class DatasourceConfig {
* </ul>
* @return SqlSessionFactoryBean
*/
@Override
@Bean
public SqlSessionFactoryBean sqlSession() {
try {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource());
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setConfigLocation(resolver.getResource("classpath:sql/mybatis-config.xml"));
bean.setMapperLocations(resolver.getResources("classpath:sql/mapper/**/*.xml"));
bean.setPlugins(new MapperSupport());
bean.setDatabaseIdProvider(databaseIdProvider());
return bean;
} catch (Exception e) {
throw Assert.runtimeException(e);
}
return super.sqlSession();
}
/**MapperConfigurer .<br />
* base package cokr.xit .
* @return MapperConfigurer
*/
@Override
@Bean
public MapperConfigurer mapperConfigurer() {
MapperConfigurer bean = new MapperConfigurer();
bean.setBasePackage("cokr.xit");
bean.setSqlSessionFactoryBeanName("sqlSession");
return bean;
return super.mapperConfigurer();
}
@Override
@Bean
VendorDatabaseIdProvider databaseIdProvider() {
VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
Properties properties = new Properties();
properties.put("MariaDB", "mariadb");
properties.put("Oracle", "oracle");
databaseIdProvider.setProperties(properties);
return databaseIdProvider;
public VendorDatabaseIdProvider databaseIdProvider() {
return super.databaseIdProvider();
}
}

@ -2,6 +2,7 @@ package cokr.xit.foundation.boot;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
/**Spring Boot xit-foundation.jar
@ -18,7 +19,8 @@ import org.springframework.test.context.ContextConfiguration;
ServletConfig.class,
MvcConfig.class,
DatasourceConfig.class,
TransactionConfig.class,
TransactionConfig.class
})
@ComponentScan(basePackages = "cokr.xit")
@ContextConfiguration("classpath:spring/context-*.xml")
public class FoundationApplication {}

@ -6,6 +6,7 @@ import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.ContextConfiguration;
/**Spring Boot xit-foundation.jar stand-alone
@ -22,8 +23,9 @@ import org.springframework.test.context.ContextConfiguration;
@ImportAutoConfiguration({
CommonConfig.class,
DatasourceConfig.class,
TransactionConfig.class,
TransactionConfig.class
})
@ComponentScan(basePackages = "cokr.xit")
@ContextConfiguration("classpath:spring/context-*.xml")
public class StandAloneApplication implements CommandLineRunner {
@Override

@ -1,24 +1,15 @@
package cokr.xit.foundation.boot;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;
/** .
@ -29,51 +20,28 @@ import org.springframework.transaction.interceptor.TransactionInterceptor;
@Aspect
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class TransactionConfig {
public class TransactionConfig extends AbstractTransaction {
@Override
@Resource(name = "dataSource")
private DataSource dataSource;
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
@Override
@Bean
public DataSourceTransactionManager txManager() {
DataSourceTransactionManager bean = new DataSourceTransactionManager();
bean.setDataSource(dataSource);
return bean;
return super.txManager();
}
@Override
@Bean
public TransactionInterceptor txAdvice() {
RuleBasedTransactionAttribute read = new RuleBasedTransactionAttribute(
TransactionDefinition.PROPAGATION_REQUIRED,
List.of(new RollbackRuleAttribute(Throwable.class))
);
read.setReadOnly(true);
RuleBasedTransactionAttribute write = new RuleBasedTransactionAttribute(
TransactionDefinition.PROPAGATION_REQUIRED,
List.of(new RollbackRuleAttribute(Throwable.class))
);
NameMatchTransactionAttributeSource txAttrSrc = new NameMatchTransactionAttributeSource();
txAttrSrc.setNameMap(Map.of(
"get*", read,
"*", write
));
TransactionInterceptor bean = new TransactionInterceptor();
bean.setTransactionManager(txManager());
bean.setTransactionAttributeSource(txAttrSrc);
return bean;
return super.txAdvice();
}
@Override
@Bean
public Advisor txAdvisor() {
String expression = Foundation.transactionPointcut();
AspectJExpressionPointcut requiredTx = new AspectJExpressionPointcut();
requiredTx.setExpression(expression);
DefaultPointcutAdvisor bean = new DefaultPointcutAdvisor();
bean.setPointcut(requiredTx);
bean.setAdvice(txAdvice());
return bean;
return super.txAdvisor();
}
}
Loading…
Cancel
Save