关于Spring事务管理中@Transactional的参数配置

本文详细介绍了Spring框架中基于注解的事务管理,特别是@Transactional的使用。通过在服务类和方法上添加注解,配合XML配置,可以实现事务的声明和管理。文章强调了注解应在public方法上使用,且建议直接在具体类上而不是接口上使用。还提到了如何通过transaction-manager属性控制代理类型,并讨论了方法的事务设置优先级。同时,文中还涉及了事务传播设置、隔离级别等概念,以及如何与AspectJ结合使用。
摘要由CSDN通过智能技术生成

spring作为低侵入的Java EE框架之一,能够很好地与其他框架进行整合,其中Spring与hibernate的整合实现的事务管理是常用的一种功能。  所谓事务,就必须具备ACID特性,即原子性、一致性、隔离性和持久性

注意@Transactional 注解及其支持类所提供的功能最低要求使用Java 5(Tiger)。

除了基于XML文件的声明式事务配置外,你也可以采用基于注解式的事务配置方法。直接在Java源代码中声明事务语义的做法让事务声明和将受其影响的代码距离更近了,而且一般来说不会有不恰当的耦合的风险,因为,使用事务性的代码几乎总是被部署在事务环境中。

下面的例子很好地演示了 @Transactional 注解的易用性,随后解释其中的细节。先看看其中的类定义:

[java]  view plain   copy
  print ?
  1. <!-- the service class that we want to make transactional -->  
  2. @Transactional   
  3. public class DefaultFooService implements FooService {   
  4.        Foo getFoo(String fooName);   
  5.        Foo getFoo(String fooName, String barName);   
  6.        void insertFoo(Foo foo);   
  7.        void updateFoo(Foo foo);   
  8. }   

当上述的POJO定义在Spring IoC容器里时,上述bean实例仅仅通过 行xml配置就可以使它具有事务性的。如下:

[html]  view plain   copy
  print ?
  1. <!-- from the file 'context.xml' -->  
  2.               
  3. <?xml version="1.0" encoding="UTF-8"?>  
  4. <beans xmlns="http://www.springframework.org/schema/beans"  
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.        xmlns:aop="http://www.springframework.org/schema/aop"  
  7.        xmlns:tx="http://www.springframework.org/schema/tx"  
  8.        xsi:schemaLocation="  
  9.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  10.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd  
  11.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  12.   
  13.   <!-- this is the service object that we want to make transactional -->  
  14.   <bean id="fooService" class="x.y.service.DefaultFooService"/>  
  15.   
  16.   <!-- enable the configuration of transactional behavior based on annotations -->  
  17.   <tx:annotation-driven transaction-manager="txManager"/>  
  18.   
  19.   <!-- a PlatformTransactionManager is still required -->  
  20.   <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值