一、创建AOP代理
AnnotationAwareAspectJAutoProxyCreator
是InstantiationAwareBeanPostProcessor
类型的后置处理器,它会在每一个bean创建之前,调用postProcessBeforeInstantiation()
方法尝试返回一个代理对象。
以自定义的MathCalculator
和LogAspects
这两个Bean为例
轮到mathCalculator
这个bean执行postProcessBeforeInstantiation()
方法
执行postProcessBeforeInstantiation()
方法结束之后,就会去创建mathCalculator
这个bean
创建完mathCalculator
之后会调用postProcessAfterInitialization()
方法
wrapIfNecessary()
方法最终给容器中返回当前组件使用cglib
增强了的代理对象
createProxy()
方法会创建出代理对象
getProxy()方法进一步调用createAopProxy().getProxy(classLoader)
,创建出AOP代理工厂,进而得到AOP代理对象
得到AOP代理工厂
再去创建AOP代理对象
以后容器中获取到的就是这个组件的代理对象,执行目标方法的时候,比如MathCalculator
的div()
除法时,代理对象就会执行通知方法的流程