SpringAOP源码之 --- 代理
Posted qiezijiajia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringAOP源码之 --- 代理相关的知识,希望对你有一定的参考价值。
概述
1.增强的生成
2.代理的获取
从上一章可以看到,在获取到增强后,就可以通过createProxy创建代理了,源码如下:
protected Object createProxy( Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.copyFrom(this);//获取当前类的属性 if (!proxyFactory.isProxyTargetClass()) { //设置ProxyTargetClass属性,为true使用CGLIB方式代理,为false使用JdkDynamicProxy方式 if (shouldProxyTargetClass(beanClass, beanName)) { proxyFactory.setProxyTargetClass(true); } else { evaluateProxyInterfaces(beanClass, proxyFactory); } } Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); //创建切面 for (Advisor advisor : advisors) { proxyFactory.addAdvisor(advisor); //将创建的切面加入的PRoxyFactory中 } proxyFactory.setTargetSource(targetSource); //设置要代理的类 customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true); } return proxyFactory.getProxy(getProxyClassLoader()); //获取代理 }
protected Advisor[] buildAdvisors(String beanName, Object[] specificInterceptors) { // Handle prototypes correctly... Advisor[] commonInterceptors = resolveInterceptorNames(); List<Object> allInterceptors = new ArrayList<Object>(); if (specificInterceptors != null) {//加入拦截器 allInterceptors.addAll(Arrays.asList(specificInterceptors)); if (commonInterceptors != null) { if (this.applyCommonInterceptorsFirst) { allInterceptors.addAll(0, Arrays.asList(commonInterceptors)); } else { allInterceptors.addAll(Arrays.asList(commonInterceptors)); } } } if (logger.isDebugEnabled()) { int nrOfCommonInterceptors = (commonInterceptors != null ? commonInterceptors.length : 0); int nrOfSpecificInterceptors = (specificInterceptors != null ? specificInterceptors.length : 0); logger.debug("Creating implicit proxy for bean ‘" + beanName + "‘ with " + nrOfCommonInterceptors + " common interceptors and " + nrOfSpecificInterceptors + " specific interceptors"); } Advisor[] advisors = new Advisor[allInterceptors.size()]; for (int i = 0; i < allInterceptors.size(); i++) { advisors[i] = this.advisorAdapterRegistry.wrap(allInterceptors.get(i));//拦截器进行封装转换为advisor } return advisors; }
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException { if (adviceObject instanceof Advisor) {//如果要封装的对象本身就是Advisor就不需要处理 return (Advisor) adviceObject; } if (!(adviceObject instanceof Advice)) {//此封装方法只对advice和advisor有效 throw new UnknownAdviceTypeException(adviceObject); } Advice advice = (Advice) adviceObject; if (advice instanceof MethodInterceptor) { //如果是method拦截器,就使用defaultPointCutAdvisor封装 // So well-known it doesn‘t even need an adapter. return new DefaultPointcutAdvisor(advice); } for (AdvisorAdapter adapter : this.adapters) { //如果存在advisor的适配器,也需要封装 // Check that it is supported. if (adapter.supportsAdvice(advice)) { return new DefaultPointcutAdvisor(advice); } } throw new UnknownAdviceTypeException(advice); }
由于 Spring 中涉及过多的拦截器、通知器、增强方法等方式来对逻辑进行增强,所以非常有必要统一封装成 Advisor 来进行代理的创建,完成了增强的封装过程。
接下来就是创建代理的过程了
以上是关于SpringAOP源码之 --- 代理的主要内容,如果未能解决你的问题,请参考以下文章