---
title: "Spring 事务实现机制"
author: "BlueSkator"
date: "2015-07-10T10:38:57Z"
tags: ["Spring", "事务", "代理"]
categories: ["企业架构"]
slug: "spring"
draft: false
lastmod: "2015-07-10T10:38:57Z"
id: 2226135
url: "https://blueskator.iteye.com/blog/2226135"
---

Spring是以代理的方式实现对事务的管理。我们在Action中所使用的Service对象，其实是代理对象的实例，并不是我们所写的Service对象实例。既然是两个不同的对象，那为什么我们在Action中可以象使用Service对象一样的使用代理对象呢？为了说明问题，假设有个Service类叫AService，它的Spring事务代理类为AProxyService，AService实现了一个接口 IAService （这里多了一个接口IAService，是为了说明接口代理的方式）。 

Spring事务代理方式有两种，一种是类代理方式，一种是接口代理方式。在Spring的配置文件中可以指定代理方式，目前我们使用的都是类代理方式。 //以下配置指定为类代理方式 

&lt;property name="proxyTargetClass"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt; 

1、类代理方式 

类代理方式的实现方式是通过继承的方式来实现，下面用伪代码来说明。 

interface IAService{   
  public void save(ValueObject object); 

} 

class AService implements IAService{   
  public void save(ValueObject object){ ... } 

} 

// Spring自动生成的代理类. 

class AProxyService extends AService{   
  public void save(ValueObject object){   
      try{   
         启动事务的代码;   
          super.save(object);   
         提交事务的代码;   
      }catch(Exception e){   
         回滚事务的代码;   
      }   
  } 

} 

在Action的调用代码: 

AService a = (AService)getBean("aProxyServiceBeanName"); 

这里 getBean("aProxyServiceBeanName") 得到的是 AProxyService 类的实例，因为AProxyService是AService的一个子类，所以这里可以强制转型为AService。我们后面调用a.save(object)的时候，调用的是AProxyService.save()方法，而这个方法是有事务处理的。Spring就是这样实现了事务管理。 // 下面的这行代码也是同样的效果。 

IAService a = (IAService)getBean("aProxyServiceBeanName"); 

Spring通过CGLib来实现了类代理方式。 

2、接口代理方式 

接口代理方式是通过实现接口，引用类实例来实现的，所以这里一定要有一个接口IAService，而类代理方式是不需要这个接口的。 

// Spring自动生成的代理类. 

class AProxyService implements IAService{   
  private AService aService;   
   public void setAService(AService aService){   
     this.aService = aService;   
  }   
   public void save(ValueObject object){   
      try{   
         启动事务的代码;   
         aService.save(object); // 注意这行代码与上面的不同。   
         提交事务的代码;   
      }catch(Exception e){   
         回滚事务的代码;   
      }   
  } 

} 

在Action的调用代码: 

AService a = (AService)getBean("aProxyServiceBeanName"); 

上面这行代码会报ClassCastException错误，因为 getBean("aProxyServiceBeanName") 得到的是 AProxyService 类的实例，而AProxyService实例并不能转型为AService类型，虽然两者都实现了同一个接口，但他们之间并没有继承关系。就象ArrayList 和 LinkedList，他们可以转换成List，但他们之间并不能互相转换。所以这里必须这样使用： 

IAService a = (IAService)getBean("aProxyServiceBeanName"); // 要转换成接口类型。 

调用a.save() 方法其实就是调用 AProxyService.save() 方法。 

Spring通过Java动态代理来实现接口代理