Spring AOP代理執行解析
準備
Spring版本:5.0.8
執行過程解析
根據時序圖進行分析,這裡以前置方法 before
為例
JdkDynamicAopProxy
實現了 InvocationHandler
,在呼叫時會執行 invoke()
方法
// JdkDynamicAopProxy
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
MethodInvocation invocation;
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);
}
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;
// 目標物件內部的自我呼叫無法應用切面中的增強需要通過expose-proxy屬性暴露代理
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 = targetSource.getTarget();
Class<?> targetClass = (target != null ? target.getClass() : null);
// 獲取當前方法的攔截器鏈
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
if (chain.isEmpty()) {
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
// 沒有攔截器則直接呼叫切點方法
retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
}
else {
// 將攔截器封裝在ReflectiveMethodInvocation 以便使用其proceed方法呼叫攔截器
invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
// 執行攔截器鏈
retVal = invocation.proceed();
}
Class<?> returnType = method.getReturnType();
if (retVal != null && retVal == target &&
returnType != Object.class && returnType.isInstance(proxy) &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
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.releaseTarget(target);
}
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
// AdvisedSupport
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;
}
// DefaultAdvisorChainFactory
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.
List<Object> interceptorList = new ArrayList<>(config.getAdvisors().length);
Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
for (Advisor advisor : config.getAdvisors()) {
if (advisor instanceof PointcutAdvisor) {
// Add it conditionally.
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
// 獲取方法攔截器陣列
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
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));
}
}
else {
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
return interceptorList;
}
// DefaultAdvisorAdapterRegistry
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<>(3);
Advice advice = advisor.getAdvice();
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
}
/**
* 在初始化時註冊了三個增強器介面卡
* MethodBeforeAdviceAdapter
* AfterReturningAdviceAdapter
* ThrowsAdviceAdapter
*/
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
// 通過增強器介面卡獲取方法攔截器
interceptors.add(adapter.getInterceptor(advisor));
}
}
if (interceptors.isEmpty()) {
throw new UnknownAdviceTypeException(advisor.getAdvice());
}
return interceptors.toArray(new MethodInterceptor[0]);
}
// MethodBeforeAdviceAdapter
public MethodInterceptor getInterceptor(Advisor advisor) {
MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
return new MethodBeforeAdviceInterceptor(advice);
}
下面看 ReflectiveMethodInvocation
的 proceed()
方法執行攔截器鏈
// ReflectiveMethodInvocation
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;
if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
return dm.interceptor.invoke(this);
}
else {
// 不匹配則不執行攔截器
return proceed();
}
}
else {
// 普通攔截器直接呼叫 將this作為引數傳遞以保證當前例項中攔截器執行
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
}
}
呼叫 MethodBeforeAdviceInterceptor
的 invoke
方法
// MethodBeforeAdviceInterceptor
public Object invoke(MethodInvocation mi) throws Throwable {
// 執行before方法
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
return mi.proceed();
}
// AspectJMethodBeforeAdvice
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
invokeAdviceMethod(getJoinPointMatch(), null, null);
}
// AbstractAspectJAdvice
protected Object invokeAdviceMethod(
@Nullable JoinPointMatch jpMatch, @Nullable Object returnValue, @Nullable Throwable ex)
throws Throwable {
return invokeAdviceMethodWithGivenArgs(argBinding(getJoinPoint(), jpMatch, returnValue, ex));
}
// AbstractAspectJAdvice
protected Object invokeAdviceMethodWithGivenArgs(Object[] args) throws Throwable {
Object[] actualArgs = args;
if (this.aspectJAdviceMethod.getParameterCount() == 0) {
actualArgs = null;
}
try {
ReflectionUtils.makeAccessible(this.aspectJAdviceMethod);
// 呼叫增強方法
return this.aspectJAdviceMethod.invoke(this.aspectInstanceFactory.getAspectInstance(), actualArgs);
}
catch (IllegalArgumentException ex) {
throw new AopInvocationException("Mismatch on arguments to advice method [" +
this.aspectJAdviceMethod + "]; pointcut expression [" +
this.pointcut.getPointcutExpression() + "]", ex);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
整個 AOP
執行流程完成。
參考資料
Spring原始碼深度解析
相關文章
- Spring Aop的執行順序Spring
- 死磕Spring之AOP篇 - Spring AOP自動代理(三)建立代理物件Spring物件
- Spring AOP 自動建立代理Spring
- Spring AOP 原始碼解析Spring原始碼
- 死磕Spring之AOP篇 - Spring AOP自動代理(一)入口Spring
- 從代理機制到Spring AOPSpring
- 3種代理模式-理解Spring Aop模式Spring
- Spring AOP --JDK動態代理方式SpringJDK
- 從動態代理到Spring AOP(中)Spring
- 從動態代理到Spring AOP(上)Spring
- Spring框架系列(10) - Spring AOP實現原理詳解之AOP代理的建立Spring框架
- AOP原始碼解析之二-建立AOP代理前傳,獲取AOP資訊原始碼
- spring aop原理 JDK動態代理和CGLIB動態代理SpringJDKCGLib
- 死磕Spring之AOP篇 - Spring AOP兩種代理物件的攔截處理Spring物件
- Spring中AOP相關原始碼解析Spring原始碼
- JAVA-Spring AOP基礎 - 代理設計模式JavaSpring設計模式
- 死磕Spring之AOP篇 - Spring AOP自動代理(二)篩選合適的通知器Spring
- AOP詳解之三-建立AOP代理後記,建立AOP代理
- 【Spring】AOP的代理預設是Jdk還是Cglib?SpringJDKCGLib
- 深入Spring官網系列(十八):AOP詳細解析!Spring
- 原始碼解析Spring AOP的載入與生效原始碼Spring
- Spring原始碼分析之AOP從解析到呼叫Spring原始碼
- OA專案(使用Spring AOP 給執行方法新增日誌功能)Spring
- Spring AOP裡的靜態代理和動態代理,你真的瞭解嘛?Spring
- Spring AOP 中被代理的物件一定是單例嗎?Spring物件單例
- 淺析Spring中AOP的實現原理——動態代理Spring
- [Spring]AOPSpring
- spring AOPSpring
- Spring框架系列(11) - Spring AOP實現原理詳解之Cglib代理實現Spring框架CGLib
- Spring框架系列(12) - Spring AOP實現原理詳解之JDK代理實現Spring框架JDK
- Spring中AOP相關的API及原始碼解析SpringAPI原始碼
- 死磕Spring之AOP篇 - Spring AOP總覽Spring
- Spring Boot整合Spring AopSpring Boot
- Java動態代理(AOP)Java
- spring-aopSpring
- Spring AOP APISSpringAPI
- Spring AOP IOCSpring
- Spring 的 AOPSpring