首页 > 其他分享 >spring tx:advice 和 aop:config 配置事务

spring tx:advice 和 aop:config 配置事务

时间:2023-06-09 10:06:47浏览次数:36  
标签:回滚 rollback tx spring advice 事务 任意 com crm


<?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"
    xmlns:context="http://www.springframework.org/schema/context"
    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="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <!-- <tx:method name="*" propagation="true" />-->
        </tx:attributes>

    </tx:advice>

    <aop:config>
        <aop:pointcut id="      allManagerMethod      "
            expression="execution(* com.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice"
            pointcut-ref="      allManagerMethod      " />
    </aop:config>
</beans>




Eclipse不能识别<tx:advice/>标签

在开发Spring的过程中,有时会出现Eclipse不能识别<tx:advice/>标签。

提示出现以下错误:

The prefix "tx" for element "tx:advice" is not bound



这个错误的原因很简单是:

我们在定义申明AOP的时候。。没有加载schema。

具体表现如下:

<beans>

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
     <tx:attributes> 
         <tx:method name="get*" read-only="true"/> 
         <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/> 
     </tx:attributes> 
</tx:advice>

<!-- aop代理设置--> 
<aop:config proxy-target-class="true">   
....

</aop:config>

</beans>



这时会抛出异常不认<TX>标签。。起先还以为是没有加载JAR包呢。。

后来读AOP文档才发现<beans>中要加入“xmlns:aop”的命名申明,并在“xsi:schemaLocation”中指定aop配置的schema的地址

配置文件如下:

<?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 ">



这些才是最关键的地方。。后面的配置不变。。。。


Spring使用 <tx:advice>和 <aop:config> 用来配置事务,具体如何配置你可以参考Spring文档。

我解释一下(* com.evan.crm.service.*.*(..))中几个通配符的含义:

第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.evan.crm.service下的任意class
第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法
第四个 .. —— 通配 方法可以有0个或多个参数

综上:包com.evan.crm.service下的任意class的具有任意返回值类型、任意数目参数和任意名称的方法

<tx:advice/>

这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置。默认的 <tx:advice/> 设置如下:



  • 事务传播设置是 REQUIRED
  • 隔离级别是 DEFAULT
  • 事务是 读/写
  • 事务超时默认是依赖于事务系统的,或者事务超时没有被支持。
  • 任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚



这些默认的设置当然也是可以被改变的。 <tx:advice/><tx:attributes/> 标签里的 <tx:method/> 各种属性设置总结如下:rollback-for/no-rollback-for:配置哪些异常可以导致/不导致回滚,在默认情况下,抛出RuntimeException或其子类将导致回滚,其它异常不会导致回滚 .



表 9.1. <tx:method/> 有关的设置



name


与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*''handle*''on*Event'等等。

propagation


REQUIRED

事务传播行为

isolation


DEFAULT

事务隔离级别

timeout


-1

事务超时的时间(以秒为单位)

read-only


false

事务是否只读?

rollback-for


将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'

no-rollback-for


被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException

标签:回滚,rollback,tx,spring,advice,事务,任意,com,crm
From: https://blog.51cto.com/u_16065168/6445470

相关文章

  • spring中获取属性的方法SpelExpressionParser()
    Aa=newA();Bb=newB();声明el表达式ExpressionParserpaeser=newSpelExpressionParser();设置你要通过el表达式取的字段Expressionaid=paeser.parseExpression("id");代表内置对象(b)中的id属性Expressionbid=paeser.parseExpression(b.id);如果b对象......
  • 用Spring Cloud Alibaba构建用户中心!只要5分钟
    你好,我是田哥。前几天,有几位朋友私信我,叫我写一个使用SpringCloudAlibaba搭建项目。今天,我给安排上,使用SpringCloudAlibaba搭建一个用户中心。下面,我就来给大家说一下,我饿还是如何搭建的,具体步骤如下:使用IDEA创建项目结构添加maven相关依赖安装Nacos业务代码整个技术栈:Spring......
  • spring mvc
    springmvc接收json格式字符串@PostMapping("jsonhello")publicStringjsonhello(@RequestBodyMap<String,Object>map){System.out.println(JSONObject.toJSONString(map));return"SUCCESS";}apipost请求wireshark......
  • SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
    前言这篇博客主要记录的是谷粒商城项目的完整开发过程大家想看的话也可以去这里看看:笔记地址传送门后续还会继续维护这个笔记一、项目架构二、技术总和三、开发准备1、开发环境搭建2、工程初始化四、分布式基础篇(全栈开发篇)五、分布式高级篇(微服务架构篇)......
  • 徒手撸一个Spring Boot中的starter,解密自动化配置
    starter背景SpringBoot目前已经变成了后端开发这必备技能之一,其中一个主要原因是SpringBoot中有个非常重要的机制(starter机制)。starter能够抛弃以前繁杂的配置,将其统一集成进starter,使用的时候只需要在maven中引入对应的starter依赖即可,SpringBoot就能自动扫描到要加载的信息并......
  • SpringCloud必会知识点最全(精美版)
    为什么要学习SpringCloud在项目开发中随着业务越来越多,导致功能之间耦合性高、开发效率低、系统运行缓慢难以维护、不稳定。微服务架构可以解决这些问题,而SpringCloud是微服务架构最流行的实现.1.微服务微服务架构是使用一套小服务来开发单个应用的方式或途径,每个服务基于单一业......
  • spring事务概要
    Spring框架是一个2003年2月才出现的开源项目,该开源项目起源自RodJohnson在2002年末出版的《ExpertOne-on-OneJ2EEDesignandDevelopment》一书中的基础性代码。在该书中,RodJohnson倡导J2EE实用主义的设计思想,而Spring框架正是这一思想的更全面和具体的实现。Spring框......
  • JAVA的springboot+vue学习平台管理系统,校园在线学习管理系统,附源码+数据库+论文+PPT
    1、项目介绍在Internet高速发展的今天,我们生活的各个领域都涉及到计算机的应用,其中包括学习平台的网络应用,在外国学习平台已经是很普遍的方式,不过国内的管理平台可能还处于起步阶段。学习平台具有学习信息管理功能的选择。学习平台采用java技术,基于springboot框架,mysql数据库进行......
  • 【转载】CXF spring jaxws:endpoint jaxws:server 区别 与 关系
    通过spring配置实现webservice的配置有两种,一种是jaxws:endpoint ,另外一种是jaxws:server。百度上没查到相关区别,又由于本人没有跟代码,所以又google了一下,才找到:First,theyareallfortheserversideconfiguration.Second,jaxws:endpointiscomingfromJAXWSAPI,a......
  • 转载:Spring 框架的设计理念与设计模式分析
    Spring框架的设计理念与设计模式分析<!--LEADSPACE_BODY_END--><!--SUMMARY_BEGIN-->Spring作为现在最优秀的框架之一,已被广泛的使用,并且有很多对其分析的文章。本文将从另外一个视角试图剖析出Spring框架的作者设计Spring框架的骨骼架构的设计理念,有那几个核心组件?为......