SpringBoot注解事务声明式事务的方式

  

springboot对新人来说可能上手比springmvc要快,但是对于各位从springmvc转战到springboot的话,有些地方还需要适应下,尤其是xml配置。我个人是比较喜欢注解& # 10133;xml是因为看着方便,查找方便,清晰明了。但是xml完全可以使用注解代替,今天就扒一扒springboot中事务使用注解的玩法。

  

springboot的事务也主要分为两大类,一是xml声明式事务,二是注解事务,注解事务也可以实现类似声明式事务的方法,关于注解声明式事务,目前网上搜索不到合适的资料,所以在这里,我将自己查找和总结的几个方法写到这里,大家共同探讨

  

  

,,,,,可以使用<代码> @ImportResource(“类路径:transaction.xml”) 引入该xml的配置,xml的配置如下

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;豆类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”比;   & lt; bean id=" txManager "   类=皁rg.springframework.jdbc.datasource.DataSourceTransactionManager”比;   & lt;属性名=笆菰础眗ef=笆菰础弊4? lt;/property>   & lt;/bean>   & lt; tx:建议id=癱ftxAdvice事务管理器”=皌xManager”比;   & lt; tx: attributes>   & lt; tx:方法名="查询*”=爸С帧敝欢链?" true "祝辞& lt;/tx: method>   & lt; tx:方法名==爸С帧薄?”传播只读=" true "祝辞& lt;/tx: method>   & lt; tx:方法名=传播“select *”=爸С帧敝欢?" true "祝辞& lt;/tx: method>   & lt; tx:方法名=" * "传播=毙枰盎毓?袄狻弊4? lt;/tx: method>   & lt;/tx: attributes>   & lt;/tx: advice>   & lt; aop: config>   & lt; aop:切入点id==癮llManagerMethod”表达“执行(* com.exmaple.fm…服务。* . * (. .)”/比;   & lt; aop:顾问advice-ref=" txAdvice " pointcut-ref=癮llManagerMethod”秩序=" 0 "/比;   & lt;/aop: config>   & lt;/beans>      

springboot启动类如下:

        包com.example.fm;   进口org.springframework.boot.SpringApplication;   进口org.springframework.boot.autoconfigure.SpringBootApplication;   进口org.springframework.context.annotation.ImportResource;   @ImportResource(“类路径:transaction.xml”)   @SpringBootApplication   公共类应用程序{   公共静态void main (String [] args) {   SpringApplication.run (Application.class, args);   }   }      

启动后即可开启事务,不过项目里导入了xml配置,如果不想导入xml配置,可以使用注解的方式。

  

  

注解事务讲解之前,需要先了解下春天创建代理的几个类,在弹簧内部,是通过BeanPostProcessor来完成自动创建代理工作的.BeanPostProcessor接口的实现只是在ApplicationContext初始化的时候才会自动加载,而普通的BeanFactory只能通过编程的方式调用之,根据,匹配规则的不同大致分为三种类别:

  

,匹配Bean的名称自动创建匹配到的Bean的代理,实现类BeanNameAutoProxyCreator
  

        & lt; bean id=皌estInterceptor”类=癱om.example.service.config.testInerceptor祝辞& lt;/bean>   & lt; bean id=皃rofileAutoProxyCreator”类=" org.springframework.aop.framework。   祝辞autoproxy.BeanNameAutoProxyProxyCreator”;   & lt; bean>   & lt;属性名=癰eanNames”比;   & lt; list>   & lt; value> * Service   & lt;/list>   & lt;/property>   & lt;属性名=癷nterceptorNames”比;   & lt; value>testInterceptor & lt;/value>   & lt;/property>   & lt;/bean>      

b,根据Bean中的AspectJ注解自动创建代理,实现类AnnotationAwareAspectJAutoProxyCreator

        & lt; aop: aspectj-autoproxy proxy-target-class=" true "/比;   & lt; bean id=癮nnotationAwareAspectJAutoProxyCreatorTest”类=癱om.example.service.AnnotationAwareAspectJAutoProxyCreatorTest”/比;   & lt; aop: config>   ref=& lt; aop:方面“annotationAwareAspectJAutoProxyCreatorTest”比;   & lt; aop:在方法="过程"切入点=爸葱?* com.example.service.fm . . * . *(. .)”/比;   & lt;/aop: aspect>   & lt;/aop: config>

SpringBoot注解事务声明式事务的方式