SpringBoot 注解事务声明式事务
springboot 对新人来说可能上手比springmvc要快,但是对于各位从springmvc转战到springboot的话,有些地方还需要适应下,尤其是xml配置。我个人是比较喜欢注解➕xml是因为看着方便,查找方便,清晰明了。但是xml完全可以使用注解代替,今天就扒一扒springboot中事务使用注解的玩法。
springboot的事务也主要分为两大类,一是xml声明式事务,二是注解事务,注解事务也可以实现类似声明式事务的方法,关于注解声明式事务,目前网上搜索不到合适的资料,所以在这里,我将自己查找和总结的几个方法写到这里,大家共同探讨
springboot 之 xml事务
可以使用 @ImportResource("classpath:transaction.xml") 引入该xml的配置,xml的配置如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" ></property> </bean> <tx:advice id="cftxAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="query*" propagation="SUPPORTS" read-only="true" ></tx:method> <tx:method name="get*" propagation="SUPPORTS" read-only="true" ></tx:method> <tx:method name="select*" propagation="SUPPORTS" read-only="true" ></tx:method> <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" ></tx:method> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="allManagerMethod" expression="execution (* com.exmaple.fm..service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod" order="0" /> </aop:config> </beans>
springboot 启动类如下:
package com.example.fm; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @ImportResource("classpath:transaction.xml") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
启动后即可开启事务,不过项目里导入了xml配置,如果不想导入xml配置,可以使用注解的方式。
springboot 之 注解事务
注解事务讲解之前,需要先了解下spring创建代理的几个类,在spring内部,是通过BeanPostProcessor来完成自动创建代理工作的。BeanPostProcessor接口的实现只是在ApplicationContext初始化的时候才会自动加载,而普通的BeanFactory只能通过编程的方式调用之。根据 匹配规则的不同大致分为三种类别:
a、匹配Bean的名称自动创建匹配到的Bean的代理,实现类BeanNameAutoProxyCreator
<bean id="testInterceptor" class="com.example.service.config.testInerceptor”></bean> <bean id="profileAutoProxyCreator" class="org.springframework.aop.framework. autoproxy.BeanNameAutoProxyProxyCreator"> <bean> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <property name="interceptorNames"> <value> testInterceptor </value> </property> </bean>
b、根据Bean中的AspectJ注解自动创建代理,实现类AnnotationAwareAspectJAutoProxyCreator
<aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="annotationAwareAspectJAutoProxyCreatorTest" class="com.example.service.AnnotationAwareAspectJAutoProxyCreatorTest"/> <aop:config> <aop:aspect ref="annotationAwareAspectJAutoProxyCreatorTest"> <aop:around method="process" pointcut="execution (* com.example.service.fm..*.*(..))"/> </aop:aspect> </aop:config>
c、根据Advisor的匹配机制自动创建代理,会对容器中所有的Advisor进行扫描,自动将这些切面应用到匹配的Bean中,实现类DefaultAdvisorAutoProxyCreator
接下来开讲注解开启事务的方法:
1、Transactional注解事务
需要在进行事物管理的方法上添加注解@Transactional,或者偷懒的话直接在类上面添加该注解,使得所有的方法都进行事物的管理,但是依然需要在需要事务管理的类上都添加,工作量比较大,这里只是简单说下,具体的可以google或者bing
2、注解声明式事务
Component或Configuration中bean的区别,有时间我会专门写一篇来讲解下
a.方式1,这里使用Component或Configuration事务都可以生效
package com.exmple.service.fm9.config; import java.util.Collections; import java.util.HashMap; import java.util.Map; 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.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; 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.TransactionAttribute; import org.springframework.transaction.interceptor.TransactionInterceptor; /** * Created by guozp on 2017/8/28. */ @Aspect //@Component 事务依然生效 @Configuration public class TxAdviceInterceptor { private static final int TX_METHOD_TIMEOUT = 5; private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.alibaba.fm9..service.*.*(..))"; @Autowired private PlatformTransactionManager transactionManager; @Bean public TransactionInterceptor txAdvice() { NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); /*只读事务,不做更新操作*/ RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute(); readOnlyTx.setReadOnly(true); readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED ); /*当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务*/ RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute(); requiredTx.setRollbackRules( Collections.singletonList(new RollbackRuleAttribute(Exception.class))); requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); requiredTx.setTimeout(TX_METHOD_TIMEOUT); Map<String, TransactionAttribute> txMap = new HashMap<>(); txMap.put("add*", requiredTx); txMap.put("save*", requiredTx); txMap.put("insert*", requiredTx); txMap.put("update*", requiredTx); txMap.put("delete*", requiredTx); txMap.put("get*", readOnlyTx); txMap.put("query*", readOnlyTx); source.setNameMap( txMap ); TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source); return txAdvice; } @Bean public Advisor txAdviceAdvisor() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(AOP_POINTCUT_EXPRESSION); return new DefaultPointcutAdvisor(pointcut, txAdvice()); //return new DefaultPointcutAdvisor(pointcut, txAdvice); } }
b.方式1,这里使用Component或Configuration事务都可以生效
package com.exmple.service.fm9.config; import java.util.Collections; import java.util.HashMap; import java.util.Map; 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.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; 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.TransactionAttribute; import org.springframework.transaction.interceptor.TransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; /** * Created by guozp on 2017/8/29. */ //@Component 事务依然生效 @Configuration public class TxAnoConfig { /*事务拦截类型*/ @Bean("txSource") public TransactionAttributeSource transactionAttributeSource(){ NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource(); /*只读事务,不做更新操作*/ RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute(); readOnlyTx.setReadOnly(true); readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED ); /*当前存在事务就使用当前事务,当前不存在事务就创建一个新的事务*/ //RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute(); //requiredTx.setRollbackRules( // Collections.singletonList(new RollbackRuleAttribute(Exception.class))); //requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, Collections.singletonList(new RollbackRuleAttribute(Exception.class))); requiredTx.setTimeout(5); Map<String, TransactionAttribute> txMap = new HashMap<>(); txMap.put("add*", requiredTx); txMap.put("save*", requiredTx); txMap.put("insert*", requiredTx); txMap.put("update*", requiredTx); txMap.put("delete*", requiredTx); txMap.put("get*", readOnlyTx); txMap.put("query*", readOnlyTx); source.setNameMap( txMap ); return source; } /**切面拦截规则 参数会自动从容器中注入*/ @Bean public AspectJExpressionPointcutAdvisor pointcutAdvisor(TransactionInterceptor txInterceptor){ AspectJExpressionPointcutAdvisor pointcutAdvisor = new AspectJExpressionPointcutAdvisor(); pointcutAdvisor.setAdvice(txInterceptor); pointcutAdvisor.setExpression("execution (* com.alibaba.fm9..service.*.*(..))"); return pointcutAdvisor; } /*事务拦截器*/ @Bean("txInterceptor") TransactionInterceptor getTransactionInterceptor(PlatformTransactionManager tx){ return new TransactionInterceptor(tx , transactionAttributeSource()) ; } }
c.方式1,这里使用Component或Configuration事务都可以生效
package com.exmple.service.fm9.config; import java.util.Properties; import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Component; import org.springframework.transaction.interceptor.TransactionInterceptor; /** * Created by guozp on 2017/8/28. * */ //@Component @Configuration public class TxConfigBeanName { @Autowired private DataSourceTransactionManager transactionManager; // 创建事务通知 @Bean(name = "txAdvice") public TransactionInterceptor getAdvisor() throws Exception { Properties properties = new Properties(); properties.setProperty("get*", "PROPAGATION_REQUIRED,-Exception,readOnly"); properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception,readOnly"); properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception,readOnly"); properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception,readOnly"); properties.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception,readOnly"); TransactionInterceptor tsi = new TransactionInterceptor(transactionManager,properties); return tsi; } @Bean public BeanNameAutoProxyCreator txProxy() { BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator(); creator.setInterceptorNames("txAdvice"); creator.setBeanNames("*Service", "*ServiceImpl"); creator.setProxyTargetClass(true); return creator; } }
d.方式1,这里使用Component或Configuration并不是所有事务都可以生效,例如Configuration的时候如果打开注释部分的而且不把代码都移动到 defaultPointcutAdvisor(),事物会失效,具体原因暂时不明,如果各位有明白的,可以指点我下。
ackage com.alibaba.fm9.config; import java.util.Properties; import javax.sql.DataSource; import org.springframework.aop.aspectj.AspectJExpressionPointcut; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Component; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.interceptor.TransactionInterceptor; /** * Created by guozp on 2017/8/28. * ??????? */ @Configuration //事务失效,都移动到一个方法不失效 //@Component // 事务可行,不用都移动到一个方法 public class TxOtherConfigDefaultBean { public static final String transactionExecution = "execution (* com.alibaba.fm9..service.*.*(..))"; @Autowired private PlatformTransactionManager transactionManager; //@Bean //@ConditionalOnMissingBean //public PlatformTransactionManager transactionManager() { // return new DataSourceTransactionManager(dataSource); //} @Bean public TransactionInterceptor transactionInterceptor() { Properties attributes = new Properties(); attributes.setProperty("get*", "PROPAGATION_REQUIRED,-Exception"); attributes.setProperty("add*", "PROPAGATION_REQUIRED,-Exception"); attributes.setProperty("update*", "PROPAGATION_REQUIRED,-Exception"); attributes.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception"); //TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager(), attributes); TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, attributes); return txAdvice; } //@Bean //public AspectJExpressionPointcut aspectJExpressionPointcut(){ // AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); // pointcut.setExpression(transactionExecution); // return pointcut; //} @Bean public DefaultPointcutAdvisor defaultPointcutAdvisor(){ //AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); //pointcut.setExpression(transactionExecution); //DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); //advisor.setPointcut(pointcut); //advisor.setAdvice(transactionInterceptor()); AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(transactionExecution); DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(); advisor.setPointcut(pointcut); Properties attributes = new Properties(); attributes.setProperty("get*", "PROPAGATION_REQUIRED,-Exception"); attributes.setProperty("add*", "PROPAGATION_REQUIRED,-Exception"); attributes.setProperty("update*", "PROPAGATION_REQUIRED,-Exception"); attributes.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception"); TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, attributes); advisor.setAdvice(txAdvice); return advisor; } }
简单来说,springboot使用上述注解的几种方式开启事物,可以达到和xml中声明的同样效果,但是却告别了xml,使你的代码远离配置文件。
文章出处:http://www.cnblogs.com/guozp/articles/7446477.html

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Spring Boot教程全集-修正集合
修正1:20 使用Caching-Redis 位置:com.roncoo.education.cache.impl.RoncooUserLogCacheImpl,第34行,@CachePut(key = "#p0") 正确:@CachePut(key = "#p0.id") 原因:key值定义写错。#p0是指使用第一个参数作为key值,上面selectById是将主键作为key,如果更新将对象作为key,则会导致更新之后,查找不到最新的信息,对象作为key,因为修改,会每修改一次都产生一条记录,因为每个对象的属性都会变化 感谢学员(你的微笑、很美)的发现。 修正2:31 生产部署-注意事项和如何使用脚本 位置:kill -9 ${procedure}(第一个kill的位置) 正确:kill ${procedure} 原因:kill -9 是立即杀死应用,第一关闭的时候,不应该是杀死,这样就无法令spring boot应用进行优雅关机,当kill不成功的时候,才考虑进行kill -9。
- 下一篇
微服务架构的基础框架选择:Spring Cloud还是Dubbo?
最近一段时间不论互联网还是传统行业,凡是涉及信息技术范畴的圈子几乎都在讨论微服务架构。近期也看到各大技术社区开始组织一些沙龙和论坛来分享Spring Cloud的相关实施经验,这对于最近正在整理Spring Cloud相关套件内容与实例应用的我而言,还是有不少激励的。 目前,Spring Cloud在国内的知名度并不高,在前阵子的求职过程中,与一些互联网公司的架构师、技术VP或者CTO在交流时,有些甚至还不知道该项目的存在。可能这也与国内阿里巴巴开源服务治理框架Dubbo有一定的关系,除了Dubbo本身较为完善的中文文档之外,不少科技公司的架构师均出自阿里系,所以就目前情况看,短期国内还是Dubbo的天下。 那么第一次实施微服务架构时,我们应该选择哪个基础框架更好呢? 以下内容均为作者个人观点,知识面有限,如有不对,纯属正常,不喜勿喷。 Round 1:背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点。阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给Apache并加入Apache基金会等,为中国互联网人争足...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS8编译安装MySQL8.0.19
- Hadoop3单机部署,实现最简伪集群
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- CentOS7安装Docker,走上虚拟化容器引擎之路