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 cokr.xit.foundation.Assert;
import lombok.Setter;
/**
* <ul><li>{@link #dataSource() } </li>
@ -21,57 +20,25 @@ import lombok.Setter;
* @author mjkhan
*/
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
@Setter
public class DatasourceConfig {
private String driverClassName;
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;
*/
private DataSource dataSource;
/** Bean . JDBC application.yml .
* <pre><code> spring:
* datasource:
* driver-class-name: JDBC
* url: URL
* username:
* password: </code></pre>
* hikari:
* driver-class-name: JDBC
* jdbc-url: URL
* username:
* password: </code></pre>
* @return
*/
@Bean
@Bean("dataSource")
@ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource() {
setConfig();
return DataSourceBuilder.create()
.driverClassName(driverClassName)
.url(url)
.username(username)
.password(password)
.build();
return dataSource != null ? dataSource : (dataSource = DataSourceBuilder.create().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 />
* MyBatis .
* <ul><li>MyBatis : classpath:sql/mybatis-config.xml</li>

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

Loading…
Cancel
Save