56°

SpringFramework之ProxyFactory

    Spring版本是5.0.4.release.

    ProxyFactory在Springaop中占有举足轻重的地位,用来间接创建代理,如下List-1所示,我们给ServiceImpl创建代理。

    List-1

public interface IService {
String hello();

}

public class ServiceImpl implements IService { @Override public String hello() { System.out.println("service的hello方法"); return "Hello"; } }

import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method;

public class BeforeAdvice implements MethodBeforeAdvice {

@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
    System.out.println("beforeAdvice的before方法");
}

}

import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method;

public class AfterAdvice implements AfterReturningAdvice {

@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
    System.out.println("AfterAdvice的afterReturning方法");
}

}

import org.junit.Test; import org.springframework.aop.framework.ProxyFactory; public class AopTest {

@Test
public void test(){
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(IService.class);
    proxyFactory.setTarget(new ServiceImpl());
    proxyFactory.addAdvice(new BeforeAdvice());
    proxyFactory.addAdvice(new AfterAdvice());
    IService proxy = (IService) proxyFactory.getProxy();
    String result = proxy.hello();
    System.out.println(result);
}

}

    运行结果如下List-2

    List-2

beforeAdvice的before方法
service的hello方法
AfterAdvice的afterReturning方法
Hello

    proxyFactory.addAdvice(new BeforeAdvice())时,会将Advice转换为Advisor,最后再调用addAdvisor方法。

    当我们调用ProxyFactory的getProxy时,会先调用createAopProxy()->getAopProxyFactory().createAopProxy(this),AopProxyFactory是DefaultAopProxyFactory——在另一篇里面讲到。createAopProxy方法里面把this传入,ProxyFactory实现了AdvisedSupport,所以在createAopProxy时将ProxyFactory中设置的targetSource、advice等传递到了DefaultAopProxyFactory,之后传递到JdkDynamicAopProxy中。

    最后getProxy调用的是JdkDynamicAopProxy的getProxy方法,如下List-3所示,Proxy.newProxyInstance的方法中,最后的InvocationHandler是this——JdkDynamicAopProxy实现了InvocationHandler,所以当我们调用目标类的方法时,会调用JdkDynamicAopProxy的invoke方法。

    List-3

public Object getProxy(@Nullable ClassLoader classLoader) {
	if (logger.isDebugEnabled()) {
		logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource());
	}
	Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);
	findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
	return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
}

    这里就引出了一个问题,在JdkDynamicAopProxy的invoke方法中,拦截器链是怎么构造出来了。

Reference

  1. https://github.com/spring-projects/spring-framework/tree/5.0.x

本文由【克虏伯】发布于开源中国,原文链接:https://my.oschina.net/u/2518341/blog/3073792

全部评论: 0

    我有话说: