aspect注解源码分析加载与生效
2021-09-05 19:33:36 0 举报
aspect注解源码分析加载与生效
作者其他创作
大纲/内容
获取当前类中除Pointcut注解的方法外的所有方法getAdvisorMethods(Class<?> aspectClass)
注解生效
org.springframework.aop.framework.AdvisedSupport#getInterceptorsAndDynamicInterceptionAdvice查找增强器
当找到有@aspect注解类时获取增强器 this.advisorFactory.getAdvisors(factory)
org.springframework.aop.aspectj.annotation.AspectJAdvisorFactory#getAdvice此方法进行生成advice根据不同注解
org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean此步调用的是查看是否有切面类
after->AspectJAfterAdvice
走默认方法反射
是
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization如果确实存在切面增强器,则会创建代理类
org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#shouldSkip
第二步
有
@Override public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class<?> targetClass = config.getTargetClass(); if (targetClass == null) { throw new AopConfigException(\"TargetSource cannot determine target class: \" + \"Either an interface or a target is required for proxy creation.\"); } if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) { return new JdkDynamicAopProxy(config); } return new ObjenesisCglibAopProxy(config); } else { return new JdkDynamicAopProxy(config); } }
第二步:this.aspectJAdvisorsBuilder.buildAspectJAdvisors()从bean工厂中找到所有带有@aspect注解的类
走各自的代理逻辑proceed方法,也就是会走我们的切面方法
第一步:super.findCandidateAdvisors()从bean工厂中找到所有Advisor的实现类
Around->AspectJAroundAdvice
hasInstantiationAwareBeanPostProcessors()由于EnableAspectJAutoProxy引入了AnnotationAwareAspectJAutoProxyCreator
org.springframework.aop.framework.JdkDynamicAopProxy#invoke
第一步
无
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization调用后置处理器
以增强器为集合为value,key为当前类名放入缓存
org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory#getPointcut获取注解表达式
new InstantiationModelAwarePointcutAdvisorImpl返回实例
关键代码
org.springframework.aop.framework.ProxyFactory#getProxy
org.springframework.aop.aspectj.annotation.InstantiationModelAwarePointcutAdvisorImpl#instantiateAdvice
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#createProxy走代理类创建并返回bean,不走docreateBean方法了
否
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessBeforeInstantiation调用bean实例化前后置处理器
jdk代理类
直接返回,继续走创建bean的其他流程
org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory#getAdvisor获取注解
afterreturn->AspectJAfterReturningAdvice
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#wrapIfNecessarywrapIfNecessary方法调用
cglib代理类
before->AspectJMethodBeforeAdvice
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation
afterthrow->AspectJAfterThrowingAdvice
org.springframework.aop.framework.DefaultAopProxyFactory#createAopProxy
走其他创建bena流程
createBean创建任意bean
org.springframework.aop.framework.ProxyCreatorSupport#createAopProxy
aspect加载
0 条评论
下一页