转自:http://huqilong.blog.51cto.com/53638/732088
在开发基于 Spring 的应用的过程中碰到了一个让我困惑了好久的问题,我在一个 Service 类的 doSomething1() 方法中通过
this.doSomething2(); 语句调用了同一个类中的 doSomething2 方法,运行时通过调试发现 doSomething1 方法的执行前后正常地执行了自定义的 around 装备,但是在 doSomething2 方法执行前后并未如我所期望的那样执行自定义的 aroundadvice 。今天终于恍然大悟,把它当作笔记写下来。Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的;二是基于 CGLIB 技术而实现的。今天的目标是探索基于 JDK Dynamic Proxy 的动态代理。首先来看看如何自己动手实现一个对Service Bean 对象的动态代理。为了能够更清楚地看到 Spring AOP 动态代理的本质,我决定不使用 JDK 中提供的 Dynamic Proxy API,就使用最普通的 java 代码来模拟一个动态代理实例。
首先,来创建一个需要代理的接口:
packagedemo.interf;
publicinterface ICustomerService {
public void doSomething1();
public void doSomething2();
}
然后就是具体服务类:
packagedemo.interf.impl;
importdemo.interf.ICustomerService;
public classCustomerServiceImpl implements ICustomerService {
public void doSomething1() {
System.out.println("InsideCustomerServiceImpl.doSomething1()");
doSomething2();
}
public void doSomething2() {
System.out.println("InsideCustomerServiceImpl.doSomething2()");
}
}
下面我们就来模拟动态生成代理类的过程,若使用 JDK Dynamic Proxy,这一过程是在运行时进行的。
CustomerServiceImpl 类对象的代理类:
package demo.interf.impl;
import demo.interf.ICustomerService;
public class CustomerServiceProxy implements ICustomerService {
private ICustomerServicecustomerService;
public voidsetCustomerService(ICustomerService customerService) {
this.customerService =customerService;
}
public void doSomething1(){
doBefore();
customerService.doSomething1();
doAfter();
}
public void doSomething2(){
doBefore();
customerService.doSomething2();
doAfter();
}
private void doBefore() {
// 例如,可以在此处开启事务
System.out.println("dosome important things before...");
}
private void doAfter() {
// 例如,可以在此处提交或回滚事务、释放资源等等
System.out.println("dosome important things after...");
}
}
使用代理对象调用业务逻辑操作的客户端程序:
package test;
import demo.interf.ICustomerService;
import demo.interf.impl.CustomerServiceImpl;
import demo.interf.impl.CustomerServiceProxy;
public class TestProxy {
public static void main(String[]args) {
// 创建代理目标对象。对于Spring来说,这一工作
// 是由Spring DI容器完成的。
ICustomerServiceserviceProxyTarget = new CustomerServiceImpl();
// 创建代理对象。对于Spring来说,这一工作
// 也是由Spring DI容器完成的。
CustomerServiceProxy serviceProxy= new CustomerServiceProxy();
serviceProxy.setCustomerService(serviceProxyTarget);
ICustomerService serviceBean= (ICustomerService) serviceProxy;
// 调用业务逻辑操作
serviceBean.doSomething1();
}
}
好了,完成了。现在以调试方式运行这个应用,你会发现在doSomething1() 中调用doSomething2() 方法的时候并未去执行CustomerServiceProxy类的doBefore()、doAfter()方法。再来看看这句关键代码:doSomething2();把它隐含的意思也表达出来吧:this.doSomething2();哦,我明白了,在CustomerServiceImpl 类中this 关键字表示的是当前这个CustomerServiceImpl类的实例。那程序当然就会去执行CustomerServiceImpl 类中的doSomething2() 方法了,而不会去执行CustomerServiceProxy 类中的doSomething2() 方法!!
在使用Spring AOP 的时候,我们从 IOC容器中获取的Service Bean 对象其实都是代理对象,而不是那些Service Bean 对象本身,也就是说获取的并不是被代理对象或代理目标。当我在自己的Service 类中使用this 关键字嵌套调用同类中的其他方法时,由于this 关键字引用的并不是该Service Bean 对象的代理对象,而是其本身,故Spring AOP 是不能拦截到这些被嵌套调用的方法的。