datasource, transaction 설정 수정

master
mjkhan21 1 year ago
parent 769298cd61
commit 53debe7940

@ -11,7 +11,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import cokr.xit.foundation.Assert; import cokr.xit.foundation.Assert;
import lombok.Setter;
/** /**
* <ul><li>{@link #dataSource() } </li> * <ul><li>{@link #dataSource() } </li>
@ -21,57 +20,25 @@ import lombok.Setter;
* @author mjkhan * @author mjkhan
*/ */
@Configuration @Configuration
@ConfigurationProperties(prefix = "spring.datasource")
@Setter
public class DatasourceConfig { public class DatasourceConfig {
private String driverClassName; private DataSource dataSource;
private String url;
private String username;
private String password;
/*
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
*/
/** Bean . JDBC application.yml . /** Bean . JDBC application.yml .
* <pre><code> spring: * <pre><code> spring:
* datasource: * datasource:
* driver-class-name: JDBC * hikari:
* url: URL * driver-class-name: JDBC
* username: * jdbc-url: URL
* password: </code></pre> * username:
* password: </code></pre>
* @return * @return
*/ */
@Bean @Bean("dataSource")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource() { public DataSource dataSource() {
setConfig(); return dataSource != null ? dataSource : (dataSource = DataSourceBuilder.create().build());
return DataSourceBuilder.create()
.driverClassName(driverClassName)
.url(url)
.username(username)
.password(password)
.build();
} }
private void setConfig() {
if (driverClassName != null) return;
try {
Yml yml = new Yml("application.yml", "application.yml");
driverClassName = yml.getValue("spring.datasource.driver-class-name");
url = yml.getValue("spring.datasource.url");
username = yml.getValue("spring.datasource.username");
password = yml.getValue("spring.datasource.password");
} catch (Exception e) {
throw Assert.runtimeException(e);
}
}
/**SqlSessionFactoryBean .<br /> /**SqlSessionFactoryBean .<br />
* MyBatis . * MyBatis .
* <ul><li>MyBatis : classpath:sql/mybatis-config.xml</li> * <ul><li>MyBatis : classpath:sql/mybatis-config.xml</li>

@ -1,6 +1,7 @@
package cokr.xit.foundation.boot; package cokr.xit.foundation.boot;
import java.util.Properties; import java.util.List;
import java.util.Map;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -13,7 +14,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute; import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute; import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor; import org.springframework.transaction.interceptor.TransactionInterceptor;
@ -39,25 +42,31 @@ public class TransactionConfig {
@Bean @Bean
public TransactionInterceptor txAdvice() { public TransactionInterceptor txAdvice() {
RuleBasedTransactionAttribute write = new RuleBasedTransactionAttribute(); RuleBasedTransactionAttribute read = new RuleBasedTransactionAttribute(
write.getRollbackRules().add(new RollbackRuleAttribute(Throwable.class)); 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))
);
Properties txAttrs = new Properties(); NameMatchTransactionAttributeSource txAttrSrc = new NameMatchTransactionAttributeSource();
txAttrs.setProperty("*", write.toString()); txAttrSrc.setNameMap(Map.of(
"get*", read,
"*", write
));
TransactionInterceptor bean = new TransactionInterceptor(); TransactionInterceptor bean = new TransactionInterceptor();
bean.setTransactionManager(txManager()); bean.setTransactionManager(txManager());
bean.setTransactionAttributes(txAttrs); bean.setTransactionAttributeSource(txAttrSrc);
return bean; return bean;
} }
@Bean @Bean
public Advisor txAdvisor() { public Advisor txAdvisor() {
String expression = "execution(* cokr.xit..service.bean..*ServiceBean.*(..))"; String expression = "execution(* cokr.xit..service.bean.*ServiceBean.*(..))";
AspectJExpressionPointcut serviceMethod = new AspectJExpressionPointcut();
serviceMethod.setExpression(expression);
AspectJExpressionPointcut requiredTx = new AspectJExpressionPointcut(); AspectJExpressionPointcut requiredTx = new AspectJExpressionPointcut();
requiredTx.setExpression(expression); requiredTx.setExpression(expression);

Loading…
Cancel
Save