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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值