AdvisedSupport解析AopConfig,使pointCutClass映射自己的advices,获取pointCutClass实例时,返回代理类的实例。
AOP借助:JdkDynamicAopProxy、CglibAopProxy
前置通知(Before Advice):pointCutMethod调用前执行Advice的Method
后置通知(After Advice):pointCutMethod调用后执行Advice的Method
返回后通知(After Return Advice):pointCutMethod返回非Void后执行Aspect的Advice的Method
环绕通知(Around Advice):pointCutMethod调用前和后执行的Advice的Method
异常通知(After Throwing Advice):pointCutMethod throw exception后执行的Advice的Method
AOP是面向切面。能给多个Bean进行统一的行为增强,比如日志。Bean上需代理的方法称为切入点@Pointcut,是被代理类。代理时增加的方法称为通知,通知放在切面类@Aspect里。调用代理类,会调用被代理类的方法和切面类的方法,进行行为增强。
通知分前置通知@Before、后置通知@After、返回后通知@After-returning、环绕通知@Around、异常通知@After-throwing。
AbstractAutowireCapableBeanFactory的initializeBean会调用到AbstractAutoProxyCreator的createProxy。然后会走到DefaultAopProxyFactory的createAopProxy,里面判断一下,类是切入点时,如果类没有实现接口,走CglibAopProxy;实现了接口走JdkDynamicAopProxy。
AopProxy里,会为被代理类的方法对应MethodInterceptor链,一个MethodInterceptor对应一个通知。代理类的方法执行时,链里的各个MethodInterceptor会执行。MethodInterceptor对应不同的通知类型,有不同的实现类。对应前置通知的MethodInterceptor执行前置通知后,下一个MethodInterceptor会执行。对应后置通知的MethodInterceptor,先让下一个MethodInterceptor执行,然后执行后置通知。对应异常通知的MethodInterceptor,先让下一个MethodInterceptor执行,捕获异常后执行异常通知。