`

spring 事物通过<tx 标签配置

 
阅读更多

配置文件如下:

<?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:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jdbc
 http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
   
    <context:annotation-config />
 
 <!--aop support-->
    <aop:aspectj-autoproxy/>
   
    <!--hibernate、数据库配置-->
    <import resource="application-datasource.xml"/>

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:config/hibernate.cfg.xml"/>
        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
 </property>
    </bean>
   
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    <!-- hibernate配置结束 -->
 
 <!-- 声明性事务配置 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
   
    <tx:advice id="baseServiceAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="getDao" propagation="NOT_SUPPORTED"/>
            <tx:method name="getJdbcTemplate" propagation="NOT_SUPPORTED"/>
            <tx:method name="getHDao" propagation="NOT_SUPPORTED"/>
            <tx:method name="getHibernateDao" propagation="NOT_SUPPORTED"/>
            <tx:method name="getHibernateTemplate" propagation="NOT_SUPPORTED"/>
            <tx:method name="getModelClass" propagation="NOT_SUPPORTED"/>
            <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
           
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
   
    <aop:config>
        <aop:pointcut id="baseServiceTxOperation" expression="execution(* com.hisun.mvc.service..*.*(..))"/>
        <aop:advisor pointcut-ref="baseServiceTxOperation" advice-ref="baseServiceAdvice"/>
    </aop:config>
   
    <!-- 声明性事务配置结束 -->
 
    <!-- 使用annotation 扫描机制注册bean -->
    <context:component-scan base-package="com.hisun.**">
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" />
    </context:component-scan>

 </beans>



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

我解释一下execution(* com.hisun.mvc.service..*.*(..))"中几个通配符的含义:

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

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

 

<tx:advice/> 有关的设置

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

 

  • 事务传播设置是 REQUIRED

  • 隔离级别是 DEFAULT

  • 事务是 读/写

  • 事务超时默认是依赖于事务系统的,或者事务超时没有被支持。

  • 任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚

 

这些默认的设置当然也是可以被改变的。 <tx:advice/><tx:attributes/> 标签里的 <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

 

分享到:
评论

相关推荐

    开发Spring的过程中出现Eclipse不能识别标签

    开发Spring的过程中出现Eclipse不能识别&lt;tx:advice /&gt;标签,需要引用 spring-tx标签,spring-tx-4.0.xsd 可以解决标签不识别的问题

    spring的5中事物配置 介绍spring的5中事物配置

    spring的5中事物配置 介绍spring的5中事物配置

    spring配置 spring配置 spring配置 spring配置 spring配置

    spring配置 spring配置 spring配置 spring配置 spring配置

    ssm配置spring配置

    ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssmspring配置ssm...

    spring中事物配置

    利用AOP定义声明式事物,配置事务管理器 , 配置事务的通知,配置事物代理,被注入的目标对象假如向拥有事务,必须有接口(AOP事务必须面向接口)

    开发工具 spring-tx-4.3.6.RELEASE

    开发工具 spring-tx-4.3.6.RELEASE开发工具 spring-tx-4.3.6.RELEASE开发工具 spring-tx-4.3.6.RELEASE开发工具 spring-tx-4.3.6.RELEASE开发工具 spring-tx-4.3.6.RELEASE开发工具 spring-tx-4.3.6.RELEASE开发工具...

    spring-tx事物源码

    该资源是Spring源码中事物的源代码,本资源已经经过编译,只需要导入工程即可。

    spring-tx-5.3.10-API文档-中文版.zip

    标签:springframework、spring、tx、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

    spring-tx-5.2.0.RELEASE-API文档-中文版.zip

    标签:springframework、spring、tx、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

    spring-tx-4.3.12.RELEASE-API文档-中文版.zip

    标签:springframework、spring、tx、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

    spring-tx-5.1.3.RELEASE-API文档-中文版.zip

    标签:springframework、spring、tx、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

    Spring2.5和Hibernate3集成--学习spring aop ioc

    &lt;tx:method name="add*" propagation="REQUIRED"/&gt; &lt;tx:method name="del*" propagation="REQUIRED"/&gt; &lt;tx:method name="modify*" propagation="REQUIRED"/&gt; &lt;tx:method name="*" read-only="true"/&gt; &lt;/tx:...

    spring事物代码片段

    spring事物代码片段,包含了定义输入传播性,和参与事物的类

    spring-tx-5.0.0.0.RELEASE.jar_java开发_spring-tx-5.0.0_

    spring-tx-5.0.0.0.RELEASE.jar是一个jar包,是项目额依赖文件,可以下载直接引入项目使用

    spring-tx-5.0.5.RELEASE-API文档-中文版.zip

    标签:springframework、spring、tx、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

    spring-tx-5.0.8.RELEASE-API文档-中英对照版.zip

    标签:springframework、spring、tx、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    spring-tx-5.3.15-API文档-中文版.zip

    标签:spring、tx、springframework、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

    Spring事物配置的五种模式

    Spring 配置文件中关于事务配置的五种常用方法,不多说,你懂的

    spring事物管理配置的5种方式

    spring事物管理配置的5种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识。通过这次的学习发觉Spring的事务配置只要把思路理清,还是...

    spring-tx-5.2.7.RELEASE-API文档-中文版.zip

    标签:springframework、spring、tx、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...

Global site tag (gtag.js) - Google Analytics