开启AOP功能可用注解形式开启:
@EnableAspectJAutoProxy
看下@EnableAspectJAutoProxy注解的作用
由上图源码可知,EnableAspectJAutoProxy注解导入了AspectJAutoProxyRegist类。
分析下AspectJAutoProxyRegist类的作用。
AspectJAutoProxyRegist类继承了ImportBeanDefinitionRegistrar。
class AspectJAutoProxyRegistrar implements ImportBeanDefinitionRegistrar
会在spring ioc 扫描类定义的过程中调用里面的registerBeanDfinitions方法。
然后通过AopConfigUtils工具类在IOC容器中注册了AnnotationAwareAspectAutoProxyCreator类
以下是AnnotationAwareAspectAutoProxyCreator类的继承关系类图
根居上面的类图可知,AnnotationAwareAspectAutoProxyCreator的父类AbstractAutoProxyCreator实现了其中的InstantiationAwareBeanPostProcessor接口方法postProcessBeforeInstantiation
postProcessBeforeInstantiation方法会在IOC容器实例化Bean的过程中调用。
调用处:
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
#resolveBeforeInstantiation
此方法作用会调用org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator
#postProcessBeforeInstantiation
方法再次调用shuldSkip
org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator
#shouldSkip
org.springframework.aop.aspectj.annotation.annotationawareaspectjautoproxycreator
#findCandidateAdvisors
org.springframework.aop.aspectj.annotation.BeanFactoryAspectJAdvisorsBuilder
#buildAspectJAdvisors
通过
this.advisorFactory.getAdvisors(factory);
找到所有的advisor。
然后将adviosr放入advisorsCache缓存中
org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory
#getAdvisor
org.springframework.aop.aspectj.annotation.InstantiationModelAwarePointcutAdvisorImpl
#InstantiationModelAwarePointcutAdvisorImpl
InstantiationModelAwarePointcutAdvisorImpl的构造方法中的instantiateAdvice 方法进行了
org.springframework.aop.aspectj.annotation.InstantiationModelAwarePointcutAdvisorImp
l
#instantiateAdvice
继续调用aspectJAdvisorFactory.getAdvice方法
org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory
#getAdvice
通过getAdvice实例化切面通知AspectJAdvice
下面是方法代码部分截图
由上图可知,根据不同通知类型( @Before, @After, @AfterReturning,@AfterThrowing,@Around)会实例化不同的切面通知AspectJAdvice
resolveBeforeInstantiation->
postProcessBeforeInstantiation->
shouldSkip->
findCandidateAdvisors->
buildAspectJAdvisors->
getAdvisors->
getPointcut->new InstantiationModelAwarePointcutAdvisorImpl()->
instantiateAdvice->
getAdvice->
Idea导出的完整调用链
getAdvice:232, ReflectiveAspectJAdvisorFactory (org.springframework.aop.aspectj.annotation)
instantiateAdvice:142, InstantiationModelAwarePointcutAdvisorImpl (org.springframework.aop.aspectj.annotation)
<init>:106, InstantiationModelAwarePointcutAdvisorImpl (org.springframework.aop.aspectj.annotation)
getAdvisor:209, ReflectiveAspectJAdvisorFactory (org.springframework.aop.aspectj.annotation)
getAdvisors:136, ReflectiveAspectJAdvisorFactory (org.springframework.aop.aspectj.annotation)
buildAspectJAdvisors:109, BeanFactoryAspectJAdvisorsBuilder (org.springframework.aop.aspectj.annotation)
findCandidateAdvisors:90, AnnotationAwareAspectJAutoProxyCreator (org.springframework.aop.aspectj.annotation)
shouldSkip:103, AspectJAwareAdvisorAutoProxyCreator (org.springframework.aop.aspectj.autoproxy)
postProcessBeforeInstantiation:248, AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
applyBeanPostProcessorsBeforeInstantiation:1042, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
resolveBeforeInstantiation:1016, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:471, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
getObject:312, AbstractBeanFactory$1 (org.springframework.beans.factory.support)
getSingleton:230, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:308, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:197, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:761, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:867, AbstractApplicationContext (org.springframework.context.support)
refresh:543, AbstractApplicationContext (org.springframework.context.support)
<init>:84, AnnotationConfigApplicationContext (org.springframework.context.annotation)
main:11, SilasMainClass (com.tuling)
通过@EnableAspectJAutoProxy开启代理功能后,会导入AnnotationAwareAspectAutoProxyCreator类,通过调用类作为后置处理的方法postProcessBeforeInstatiation,在实例化Bean之前就会扫描出切点表达式和生成所有切面通知,并将切面通知放入advisorCache缓存,以便在后续实例化Bean生成代理对象时使用。
以下是对代理对象具体的生成源码进行分析。
Spring AOP 源码探索 之深入解析实例化Bean过程创建代理类$Proxy