
Spring事务代理方式有两种,一种是类代理方式,一种是接口代理方式。在Spring的配置文件中可以指定代理方式,目前我们使用的都是类代理方式。 //以下配置指定为类代理方式
<property name="proxyTargetClass"><value>true</value></property>
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动态代理来实现接口代理