增加以下两个配置文件:
TransactionAdviceConfig.java
package com.zero4j.config; 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.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.interceptor.DefaultTransactionAttribute; import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; @Aspect @Configuration public class TransactionAdviceConfig { //使dao使用session private static final String AOP_POINTCUT_EXPRESSION = " (execution(* com..dao..*.*(..))) "; //controller由过滤器中进行设置,这里设置会抽风 /*private static final String AOP_POINTCUT_EXPRESSION2 = " (execution(* com..controller..*.*(..))) ";*/ @Autowired private PlatformTransactionManager transactionManager; @Bean public TransactionInterceptor txAdvice() { DefaultTransactionAttribute txAttr_REQUIRED = new DefaultTransactionAttribute(); txAttr_REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); DefaultTransactionAttribute txAttr_REQUIRED_READONLY = new DefaultTransactionAttribute(); txAttr_REQUIRED_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); txAttr_REQUIRED_READONLY.setReadOnly(true); DefaultTransactionAttribute txAttr_READONLY = new DefaultTransactionAttribute(); txAttr_READONLY.setReadOnly(true); NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); source.addTransactionalMethod("save*", txAttr_REQUIRED); source.addTransactionalMethod("add*", txAttr_REQUIRED); source.addTransactionalMethod("create*", txAttr_REQUIRED); source.addTransactionalMethod("insert*", txAttr_REQUIRED); source.addTransactionalMethod("update*", txAttr_REQUIRED); source.addTransactionalMethod("merge*", txAttr_REQUIRED); source.addTransactionalMethod("del*", txAttr_REQUIRED); source.addTransactionalMethod("remove*", txAttr_REQUIRED); source.addTransactionalMethod("put*", txAttr_REQUIRED); source.addTransactionalMethod("use*", txAttr_REQUIRED); source.addTransactionalMethod("exec*", txAttr_REQUIRED); source.addTransactionalMethod("set*", txAttr_REQUIRED); source.addTransactionalMethod("get*", txAttr_REQUIRED_READONLY);//特殊,service在save后会重新读取,如果设置成REQUIRED_READONLY,则save不会保存数据 source.addTransactionalMethod("count*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("find*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("list*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("query*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("find*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("is*", txAttr_REQUIRED_READONLY); /* source.addTransactionalMethod("save*", txAttr_REQUIRED); source.addTransactionalMethod("delete*", txAttr_REQUIRED); source.addTransactionalMethod("update*", txAttr_REQUIRED); source.addTransactionalMethod("exec*", txAttr_REQUIRED); source.addTransactionalMethod("set*", txAttr_REQUIRED); source.addTransactionalMethod("get*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("query*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("find*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("list*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("count*", txAttr_REQUIRED_READONLY); source.addTransactionalMethod("is*", txAttr_REQUIRED_READONLY); */ source.addTransactionalMethod("*", txAttr_READONLY); return new TransactionInterceptor(transactionManager, source); } @Bean public Advisor txAdviceAdvisor() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression( AOP_POINTCUT_EXPRESSION ); return new DefaultPointcutAdvisor(pointcut, txAdvice()); } /* @Bean public Advisor txAdviceAdvisor2() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression( AOP_POINTCUT_EXPRESSION2 ); return new DefaultPointcutAdvisor(pointcut, txAdvice()); }*/ }
FilterConfig.java
package com.zero4j.config; import org.aspectj.lang.annotation.Aspect; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.orm.hibernate5.support.OpenSessionInViewFilter; @Aspect @Configuration public class FilterConfig { //解决调用接口时session抽风的问题 @Bean public FilterRegistrationBean<OpenSessionInViewFilter> testFilterRegistration() { FilterRegistrationBean<OpenSessionInViewFilter> registration = new FilterRegistrationBean<>(); registration.setFilter(new OpenSessionInViewFilter()); registration.addUrlPatterns("/*");//配置过滤路径 return registration; } }