相关代码
切面类,声明了一个切点在 PrintService的所有方法,四个通知,每种通知对应一种Advice,Advice的抽象在 org.springframework.aop.aspectj.AbstractAspectJAdvice,每个子类是每种注解的具体实现
package com.wdg.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* User: wangdaogang
* Date: 2020/8/28
* Description: No Description
*/
@Aspect
@Slf4j
@Component
@EnableAspectJAutoProxy
public class LogAspect {
@Pointcut("execution(* com.wdg.aop.PrintService.*(..))")
public void pointCut(){}
@Before("pointCut()")
public void before(){
log.info("LogAspect............before");
}
@After("pointCut()")
public void after(){
log.info("LogAspect............after");
}
@AfterReturning("pointCut()")
public void afterReturn(){
log.info("LogAspect............afterReturn");
}
@AfterThrowing("pointCut()")
public void afterThrowing(){
log.info("LogAspect............afterThrowing");
}
}
配置类,扫描了我们本次测试的两个bean
package com.wdg.config;
import com.wdg.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author: wangdaogang
* @create: 2020/08/02
**/
@ComponentScan(value = {"com.wdg.bean","com.wdg.config","com.wdg.aop"})
@Configuration
public class CustomConfiguration {
@Bean(initMethod = "init")
public Person person(){
Person person = new Person("张三",12);
return person;
}
}
目标对象 PrintService,声明两个方法,一个正常运行,一个会抛异常
package com.wdg.aop;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
/**
* User: wangdaogang
* Date: 2020/8/28
* Description: No Description
*/
@Component
@EnableAspectJAutoProxy
public class PrintService {
public void printHello(){
System.out.println("PrintService...............printHello");
}
public void printHelloWithThrowRunTimeException(){
System.out.println("PrintService...............printHelloWithThrowRunTimeException");
throw new RuntimeException("1111");
}
}
容器启动类
package com.wdg;
import com.wdg.aop.PrintService;
import com.wdg.config.CustomConfiguration;
import com.wdg.entity.Person;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.SpringServletContainerInitializer;
import org.springframework.web.context.ContextLoaderListener;
/**
* @author: wangdaogang
* @create: 2020/08/02
**/
public class IOCDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(CustomConfiguration.class);
PrintService printService = applicationContext.getBean(PrintService.class);
printService.printHello();
}
}
从容器获取代理对象
可以看到获取的PrintService是CGLIB代理的代理对象,IDEA,F7执行当前方法。
org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept
@Override
@Nullable
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object oldProxy = null;
boolean setProxyContext = false;
Object target = null;
//获取目标对象源->这里是PrintService
TargetSource targetSource = this.advised.getTargetSource();
try {
//exposeProxy这个字段配置,是@EnableAspectJAutoProxy 的属性,设置为ture的话,会将当前的代理对象放到ThreadLocal里面
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);
//this.advised.advisors集合的对象,解析成方法执行链
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
Object retVal;
// Check whether we only have one InvokerInterceptor: that is,
// no real advice, but just reflective invocation of the target.
if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
// 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 = methodProxy.invoke(target, argsToUse);
}
else {
// We need to create a method invocation...
retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
}
retVal = processReturnType(proxy, target, method, retVal);
return retVal;
}
finally {
if (target != null && !targetSource.isStatic()) {
targetSource.releaseTarget(target);
}
if (setProxyContext) {
// Restore old proxy.
AopContext.setCurrentProxy(oldProxy);
}
}
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other ||
(other instanceof DynamicAdvisedInterceptor &&
this.advised.equals(((DynamicAdvisedInterceptor) other).advised)));
}
/**
* CGLIB uses this to drive proxy creation.
*/
@Override
public int hashCode() {
return this.advised.hashCode();
}
}
org.springframework.aop.framework.ReflectiveMethodInvocation#ReflectiveMethodInvocation
//将解析完成的MethodInterceptor执行链传入构造一个 ReflectiveMethodInvocation 对象,代理对象的执行过程在
//proceed方法。
retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
org.springframework.aop.framework.ReflectiveMethodInvocation#proceed
@Override
@Nullable
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);
}
}
几个重要的执行方法和变量:
invokeJoinpoint():执行真正的目标对象的方法。
currentInterceptorIndex:方法执行链下标
可以看到当(this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1)即下标移动到集合最后一个元素后,执行目标对象的方法。
下面针对interceptorsAndDynamicMethodMatchers中的四个方法链逐步解析(currentInterceptorIndex=0的拦截器链不是我们自己定义的不用关心)。
currentInterceptorIndex=0,interceptorOrInterceptionAdvice=ExposeInvocationInterceptor
走到、return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); ExposeInvocationInterceptor.invoke方法,传入了当前对象,、
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
MethodInvocation oldInvocation = invocation.get();
invocation.set(mi);
try {
//这里又调了上面传入的ReflectiveMethodInvocation对象的proceed方法,此时currentInterceptorIndex=0
return mi.proceed();
}
finally {
invocation.set(oldInvocation);
}
}
currentInterceptorIndex=1,interceptorOrInterceptionAdvice=AspectJAfterThrowingAdvice
抛出异常后置通知
走到、return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); ExposeInvocationInterceptor.invoke方法,又传入传入了当前对象
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
try {
//AspectJAfterThrowingAdvice 直接调用了 ReflectiveMethodInvocation.proceed()方法,如果抛出异常,会走 异常通知方法。,此时
//currentInterceptorIndex = 1
return mi.proceed();
}
catch (Throwable ex) {
//在目标对象执行目标方法发生异常时,执行 AfterThrowingAdvice方法
if (shouldInvokeOnThrowing(ex)) {
invokeAdviceMethod(getJoinPointMatch(), null, ex);
}
throw ex;
}
}
currentInterceptorIndex=2,interceptorOrInterceptionAdvice=AspectJAfterReturningAdvice
走到、return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this); ExposeInvocationInterceptor.invoke方法,又传入传入了当前对象
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
//先调用了 ReflectiveMethodInvocation.proceed()方法,当方法执行完,会调用返回通知方法,此时currentInterceptorIndex=2
Object retVal = mi.proceed();
this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis());
return retVal;
}
currentInterceptorIndex=3,interceptorOrInterceptionAdvice=AspectJAfterAdvice
interceptorOrInterceptionAdvice对象都不是InterceptorAndDynamicMethodMatcher的实例,都会走下面的
return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);传入了当前ReflectiveMethodInvocation对象本身
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
try {
return mi.proceed();
}
finally {
invokeAdviceMethod(getJoinPointMatch(), null, null);
}
}
可以看到。AspectJAfterAdvice通知使用了 try-finally语句,先调用了ReflectiveMethodInvocation.proceed(),无论是否抛出异常都会执行方法完成通知,当前currentInterceptorIndex=3,走到下一次链式调用
// We start with an index of -1 and increment early.
if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
return invokeJoinpoint();
}
不会走上面这个方法,因为此时currentInterceptorIndex=3
currentInterceptorIndex=4,interceptorOrInterceptionAdvice=AspectJMethodBeforeAdvice
继续向下走,此时interceptorOrInterceptionAdvice=AspectJMethodBeforeAdvice
会走return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);传入了当前ReflectiveMethodInvocation对象本身
@Override
public Object invoke(MethodInvocation mi) throws Throwable {
this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
return mi.proceed();
}
可以看到,当前AspectJMethodBeforeAdvice对象先调用了代理对象的before方法,最终会走到我们自定义切面的before方法。
此时又调用了ReflectiveMethodInvocation.proceed()方法,
invokeJoinpoint()
现在 currentInterceptorIndex=4 会满足
(this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1 )
这个条件
会调用 invokeJoinpoint()方法,这个方法时目标对象真正执行的方法。
- 执行完invokeJoinpoint()方法,AspectJMethodBeforeAdvice这个方法已经全部执行完,方法出栈
- 下面走到 AspectJAfterAdvice的finally语句块,执行结束通知 finally {
invokeAdviceMethod(getJoinPointMatch(), null, null); }
3、AspectJAfterAdvice 通知方法执行完,该方法出栈,栈帧指在AfterReturningAdviceInterceptor.invoke方法,发面执行返回通知(注意:如果Object retVal = mi.proceed();抛出了异常,返回通知是不会执行的,此时会被AspectJAfterThrowingAdvice捕获到,执行异常通知)
AOP大概的执行流程记录结束。
Spring官网:AOP文档