Skip to content

Commit

Permalink
BootConfig 增加 aop 数据库事物配置信息。
Browse files Browse the repository at this point in the history
  • Loading branch information
rhb0213 committed Jul 20, 2017
1 parent 9329e34 commit 06b6a90
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,100 @@

package ${prj.basePackage}.config;

import org.springframework.context.annotation.Configuration;
<#if imports??>
<#list imports as import>
import ${import};
</#list>
</#if>

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
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.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass = true,exposeProxy = true)
public class BootConfig {

protected static Logger log = LoggerFactory.getLogger(BootConfig.class);

@Bean
@Autowired
public PlatformTransactionManager transactionManager(DataSource dataSource) {
PlatformTransactionManager transManager = new DataSourceTransactionManager(dataSource);
return transManager;
}

@Bean
public AspectJExpressionPointcutAdvisor aspectJExpressionPointcutAdvisor() {
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.setExpression("execution(* com.power.test.project.service.*.*(..)) ");
advisor.setAdvice(transactionInterceptor());
return advisor;
}

@Bean
public TransactionInterceptor transactionInterceptor() {
TransactionInterceptor interceptor = new TransactionInterceptor();
interceptor.setTransactionAttributeSource(nameMatchTransactionAttributeSource());
return interceptor;
}

@Bean
public NameMatchTransactionAttributeSource nameMatchTransactionAttributeSource() {
NameMatchTransactionAttributeSource attributeSource = new NameMatchTransactionAttributeSource();

// 查询类操作 不需要事物
// PROPAGATION_NOT_SUPPORTED:如果没有,就以非事务方式执行;如果有,就将当前事务挂起。即无论如何不支持事务。
final RuleBasedTransactionAttribute transSupported = new RuleBasedTransactionAttribute();
transSupported.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
transSupported.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
transSupported.setReadOnly(Boolean.TRUE);

// 持久化操作REQUIRED 事物
// PROPAGATION_REQUIRED:默认事务类型,如果没有,就新建一个事务;如果有,就加入当前事务。适合绝大多数情况。
final RuleBasedTransactionAttribute transRequired = new RuleBasedTransactionAttribute();
transRequired.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
transRequired.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
transRequired.setReadOnly(Boolean.FALSE);

// 持久化操作 REQUIRES_NEW 事物
// PROPAGATION_REQUIRES_NEW:如果没有,就新建一个事务;如果有,就将当前事务挂起。
final RuleBasedTransactionAttribute transRrequires_new = new RuleBasedTransactionAttribute();
transRrequires_new.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
transRrequires_new.setReadOnly(Boolean.FALSE);

attributeSource.setNameMap(new HashMap<String, TransactionAttribute>() {{
put("get*", transSupported);
put("find*", transSupported);
put("query*", transSupported);

put("do*", transRequired);
put("create*", transRequired);
put("insert*", transRequired);
put("delete*", transRequired);
put("update*", transRequired);

put("log*", transRrequires_new);
}});
return attributeSource;
}



}
14 changes: 6 additions & 8 deletions mydog-plugin-project/src/main/resources/templates/_app.java.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package ${prj["basePackage"]};

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
<#--import org.springframework.context.annotation.Bean;-->
<#--import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;-->
<#--import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;-->
import org.springframework.context.annotation.Bean;
<#--import org.springframework.web.socket.config.annotation.EnableWebSocket;-->


@SpringBootApplication(scanBasePackages = "${prj["basePackage"]}.**")
@EnableAutoConfiguration
@SpringBootApplication
@MapperScan("${prj["basePackage"]}.mapping")
<#--@EnableWebSocket-->
public class App {
Expand All @@ -23,9 +21,9 @@ public class App {

<#--@Bean-->
<#--public EmbeddedServletContainerFactory servletContainer() {-->
<#--JettyEmbeddedServletContainerFactory factory =-->
<#--new JettyEmbeddedServletContainerFactory();-->
<#--return factory;-->
<#--JettyEmbeddedServletContainerFactory factory =-->
<#--new JettyEmbeddedServletContainerFactory();-->
<#--return factory;-->
<#--}-->

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down

0 comments on commit 06b6a90

Please sign in to comment.