春AOPのコアソースJdkDynamicAopProxy

春AOPのソースディレクトリ

春AOP出典01:基本となるソースのJDKの動的プロキシ
春AOP出典02:ProxyFactory
春AOP出典03:JdkDynamicAopProxy
春AOP出典04:インターセプターMethodInvocationコール
春AOP出典05:DefaultAdvisorAutoProxyCreator
春の最終試験の質問フィナーレ:春AOPは、循環依存に会ったとき
ノートgitのソースアドレスします。https://github.com/chaitou/spring-framework-master.git

序文

以下、結合第二スプリングAOPをProxyFactoryコア源はインターフェイスと構成を使用することを余儀なくされていないがプロキシオブジェクト実装、そこにスプリングAOP動的プロキシ生成モード2、cglib代理ときに使用されるJdkDynamicAopProxy薬剤を生成します。逆に使用CglibAopProxyするプロキシの生成に。第一章では、基礎となるソースJDKの動的プロキシに知られている、JDKダイナミック剤getProxy同時に、プロキシを生成する$proxy方法を呼び出すときに、プロキシオブジェクトは、コールバックしますinvoke方法。そうするためにJdkDynamicAopProxy、最も重要なコードが分析されgetProxy及びinvoke方法が。基礎となるソースコード発生剤としては、最初の記事は、さらなる説明を導入されていません
AopProxy

ソースコード解析

getProxy

最初の章では、次のコード基本となるソースコードのJDKの動的プロキシ、我々は再び手動で書いた、そしてこっちに行きます

	@Override
	public Object getProxy(@Nullable ClassLoader classLoader) {
		if (logger.isTraceEnabled()) {
			logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());
		}
		// 获取代理接口
		Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised, true);
		// 判断接口是否又hashCode和equals方法
		findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);
		// 使用JDK代理(classLoader, 接口, 当前JdkDynamicAopProxy对象:用于回调invoke和target对象方法)
		return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
	}
  1. classLoader:現在のクラスローダ
  2. proxiedInterfaces:インターフェースの現在の配列は、プロキシクラスによって実装しました
  3. this:プロキシクラスに自己生成されたオブジェクト、Hのような薬剤の特性は、時間プロパティは、コールバックメソッドをトリガするために使用されるh.invoke強化するための方法を

呼び出し

初めにinvoke最初の修正対象の前に、トランクをつかむためには、唯一のトピックに興味を持って普通增强引介增强そしてので、他の独立した章では、説明をスキップしますので、遭遇した引介增强ように最初にすることはできませんもつれていると

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object oldProxy = null;
		boolean setProxyContext = false;
		// 获取代理目标对象
		TargetSource targetSource = this.advised.targetSource;
		Object target = null;

		try {
			// equals方法处理
			if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
				// The target does not implement the equals(Object) method itself.
				return equals(args[0]);
			}
			// hashCode处理
			else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
				// The target does not implement the hashCode() method itself.
				return hashCode();
			}
			else if (method.getDeclaringClass() == DecoratingProxy.class) {
				// There is only getDecoratedClass() declared -> dispatch to proxy config.
				return AopProxyUtils.ultimateTargetClass(this.advised);
			}
			// Advised接口或者其父接口中定义的方法,直接反射调用
			else if (!this.advised.opaque && method.getDeclaringClass().isInterface() &&
					method.getDeclaringClass().isAssignableFrom(Advised.class)) {
				// Service invocations on ProxyConfig with the proxy config...
				return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
			}

			Object retVal;
			// 目标对象内部调用是无法实现增强的,如果exposeProxy设置为true,需要暴露代理
			// ThreadLocal<Object> currentProxy
			// 对象是ThreadLocal,在finally后会清除currentProxy
			if (this.advised.exposeProxy) {
				// Make invocation available if necessary.
				oldProxy = AopContext.setCurrentProxy(proxy);
				setProxyContext = true;
			}

			// Get as late as possible to minimize the time we "own" the target,
			// in case it comes from a pool.
			// 以防target来自对象池,所以在创建代理前调用get获取target
			target = targetSource.getTarget();
			Class<?> targetClass = (target != null ? target.getClass() : null);

			// Get the interception chain for this method.
			// 1. 获取拦截器链
			List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

			// Check whether we have any advice. If we don't, we can fallback on direct
			// reflective invocation of the target, and avoid creating a MethodInvocation.
			// 拦截器为空,直接调用切点方法
			if (chain.isEmpty()) {
				// We can skip creating a MethodInvocation: just invoke the target directly
				// Note that the final invoker must be an InvokerInterceptor so we know it does
				// nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
				Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
				retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
			}
			else {
				// We need to create a method invocation...
				// 2. 将拦截器统一封装成ReflectiveMethodInvocation
				MethodInvocation invocation =
						new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
				// Proceed to the joinpoint through the interceptor chain.
				// 3. 执行拦截器链
				retVal = invocation.proceed();
			}

			// Massage return value if necessary.
			Class<?> returnType = method.getReturnType();
			if (retVal != null && retVal == target &&
					returnType != Object.class && returnType.isInstance(proxy) &&
					!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
				// Special case: it returned "this" and the return type of the method
				// is type-compatible. Note that we can't help if the target sets
				// a reference to itself in another returned object.
				retVal = proxy;
			}
			else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
				throw new AopInvocationException(
						"Null return value from advice does not match primitive return type for: " + method);
			}
			return retVal;
		}
		finally {
			if (target != null && !targetSource.isStatic()) {
				// Must have come from TargetSource.
				// 如果自定义了TargetSource,释放target资源池由子类实现
				targetSource.releaseTarget(target);
			}
			if (setProxyContext) {
				// Restore old proxy.
				AopContext.setCurrentProxy(oldProxy);
			}
		}
	}

インターセプタチェーンを取得します。1.

この方法は、現在のすべてのかかるmethodマッチングadvice(エンハンスメント)を、トラッキングgetInterceptorsAndDynamicInterceptionAdviceコードを直接緩衝液、またはプロセスがインターセプタを介して取得された場合に、パフォーマンスを向上させるフェッチするスプリングAOPキャッシュを使用することが判明したadvisorChainFactoryインターセプタチェーンを取得します

	public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, @Nullable Class<?> targetClass) {
		MethodCacheKey cacheKey = new MethodCacheKey(method);
		// 从缓存中获取
		List<Object> cached = this.methodCache.get(cacheKey);
		if (cached == null) {
			// 获取拦截器链
			cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
					this, method, targetClass);
			// 设置缓存
			this.methodCache.put(cacheKey, cached);
		}
		return cached;
	}

続行getInterceptorsAndDynamicInterceptionAdviceする方法

// DefaultAdvisorChainFactory.java
public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable {

	@Override
	public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
			Advised config, Method method, @Nullable Class<?> targetClass) {

		// This is somewhat tricky... We have to process introductions first,
		// but we need to preserve order in the ultimate list.
		AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
		Advisor[] advisors = config.getAdvisors();
		List<Object> interceptorList = new ArrayList<>(advisors.length);
		Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
		Boolean hasIntroductions = null;

		// 循环所有切面
		for (Advisor advisor : advisors) {
			if (advisor instanceof PointcutAdvisor) {
				// Add it conditionally.
				PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
				// 校验当前Advisor是否适用于当前对象
				if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
					MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
					boolean match;
					// 校验Advisor是否应用到当前方法上
					if (mm instanceof IntroductionAwareMethodMatcher) {
						if (hasIntroductions == null) {
							hasIntroductions = hasMatchingIntroductions(advisors, actualClass);
						}
						match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions);
					}
					else {
						match = mm.matches(method, actualClass);
					}
					// 匹配成功
					if (match) {
            			// 从advisor中获取拦截器数组
						MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
						if (mm.isRuntime()) {
							// Creating a new object instance in the getInterceptors() method
							// isn't a problem as we normally cache created chains.
							for (MethodInterceptor interceptor : interceptors) {
								// 添加动态拦截器
								interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
							}
						}
						else {
							// 添加普通拦截器
							interceptorList.addAll(Arrays.asList(interceptors));
						}
					}
				}
			}
			// 引介增强
			else if (advisor instanceof IntroductionAdvisor) {
				IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
				if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
					Interceptor[] interceptors = registry.getInterceptors(advisor);
					interceptorList.addAll(Arrays.asList(interceptors));
				}
			}
			// 其他类型的advisor
			else {
				Interceptor[] interceptors = registry.getInterceptors(advisor);
				interceptorList.addAll(Arrays.asList(interceptors));
			}
		}

		return interceptorList;
	}
	...
}

切り欠き引介增强动态增强我々は気に普通拦截器全体のコードは、すべて取得するには、非常に明確な思考であるAdvisorを通じて(カット)pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)顧問は、によって、現在のプロキシオブジェクトをチェックし、一致しpointcutAdvisor.getPointcut().getMethodMatcher()ているが、現在のコールと一致して検証しますmethodチェックは、抽出した場合インターセプターを、に追加advisorinterceptorsinterceptorList

2.インターセプタはReflectiveMethodInvocationにカプセル化

次の呼び出しに焦点を当て、コンストラクタを呼び出し

	protected ReflectiveMethodInvocation(
			Object proxy, @Nullable Object target, Method method, @Nullable Object[] arguments,
			@Nullable Class<?> targetClass, List<Object> interceptorsAndDynamicMethodMatchers) {

		this.proxy = proxy;
		this.target = target;
		this.targetClass = targetClass;
		this.method = BridgeMethodResolver.findBridgedMethod(method);
		this.arguments = AopProxyUtils.adaptArgumentsIfNecessary(method, arguments);
		this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
	}

インターセプタチェーンを実行3.

// 执行拦截器链
retVal = invocation.proceed();
	public Object proceed() throws Throwable {
		// We start with an index of -1 and increment early.
		if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
			return invokeJoinpoint();
		}

		Object interceptorOrInterceptionAdvice =
				this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
		if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
			// Evaluate dynamic method matcher here: static part will already have
			// been evaluated and found to match.
			// 动态匹配
			InterceptorAndDynamicMethodMatcher dm =
					(InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
			Class<?> targetClass = (this.targetClass != null ? this.targetClass : this.method.getDeclaringClass());
			if (dm.methodMatcher.matches(this.method, targetClass, this.arguments)) {
				return dm.interceptor.invoke(this);
			}
			else {
				// Dynamic matching failed.
				// Skip this interceptor and invoke the next in the chain.
				// 不匹配,则跳过
				return proceed();
			}
		}
		else {
			// It's an interceptor, so we just invoke it: The pointcut will have
			// been evaluated statically before this object was constructed.
			// 普通拦截器,直接触发
			return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
		}
	}

同様に、我々は気にし普通拦截器ますが、参照普通拦截器コードの一行だけを

return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);

このコード行ユーザー定義のため注文が連鎖された後も、前に合わせてインターセプタを作ることができるの呼び出しは、これは、右の素晴らしいですか!私たちが選抜されます、次のセクションAspectJAfterAdviceおよびMethodBeforeAdviceInterceptorお問い合わせの前に呼び出さ注文インターセプタが向上し、方法する方法です

公開された42元の記事 ウォン称賛43 ビュー40000 +

おすすめ

転載: blog.csdn.net/chaitoudaren/article/details/105278409