上述源码总结起来其实就是通过@EnableAspectProxy中的AspectAutoProxyRegistrar给容器中注入了一个AnnotationAwareAspectJAutoProxyCreator,这个类的作用就是创建Aspect切面代理,这也就是整个AOP的原理,查看AnnotationAwareAspectJAutoProxyCreator继承关系图
该类间接实现类BeanFactoryAware接口,也就拥有了beanFactory的功能,实现了BeanPostProcessor,也就实现了前置后置处理器的功能
AnnotationAwareAspectJAutoProxyCreator创建注册流程
在test包中新建一个测试类BeanConfigTest
@SpringBootTest public class BeanConfigTest { @Test public void getBean(){ ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class); String[] beanNames = context.getBeanDefinitionNames(); for (String beanName : beanNames) { System.out.println(beanName); } } } 复制代码
在new AnnotationConfigApplicationContext(BeanConfig.class)这一行打断点,启动Debug模式
1.register()传入配置类BeanConfig,准备开始创建容器
2.refresh()刷新容器,进入refresh()方法
3.registerBeanPostProcessors(),注册Bean的后置处理器拦截,处理流程为
1)获取IoC容器中所有的postProcessors
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false); 复制代码
2)给容器中增加BeanPostProcessor
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); 复制代码
3)先注册实现了PriorityOrdered接口的BeanPostProcessor 再注册实现类Ordered接口的,最后注册没有实现Ordered接口的
for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); priorityOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } 复制代码
Bean Name为org.springframework.aop.config.internalAutoProxyCreator其实就是 AnnotationAwareAspectJAutoProxyCreator
4)再进入getBean()
- 接下来就是创建Bean的流程了,与创建普通的业务Bean流程一致
6) Bean创建完成之后会执行
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext)); 复制代码
将Bean注册到容器中
创建增强型业务Bean HelloController流程
在PostProcessor后置处理器创建成功之后,才会创建业务Bean, refresh() --> finishBeanFactoryInitilization()完成BeanFactory的初始化工作,然后开始创建Bean
创建业务Bean流程中的createBean()方法这里返回一个增强代理Bean,创建一个代理对象
进入resolveBeforeInstantiation,这里判断是否实现InstantiationAwareBeanPostProcessors
进入 postProcessBeforeInstantiation,开始创建代理对象
createProxy() --> proxyFactory.getProxy(getProxyClassLoader()) --> createAopProxy
AOP两种实现方式
接着就是执行doCreateBean() ...., 和创建单实例Bean的流程一致
InstantiationAwareBeanPostProcessor
每一个Bean创建之前都会调用postProcessBeforeInstantiation(),这个方法会判断当前Bean是否存在advisedBeans中,adivsedBeans中保存了所有需要增强的Bean,这里就跟AOP类LogAspect有关
LogAspect执行的方法
总结
- @EnableAspectJAutoProxy 开启AOP功能
- @EnableAspectJAutoProxy 会给容器中注册一个组件 AnnotationAwareAspectJAutoProxyCreator
- AnnotationAwareAspectJAutoProxyCreator是一个后置处理器;
- 容器的创建流程:
4-1. registerBeanPostProcessors()注册后置处理器;创建AnnotationAwareAspectJAutoProxyCreator对象
4-2. finishBeanFactoryInitialization() 初始化剩下的单实例bean
4-2-1. 创建业务逻辑组件和切面组件 AnnotationAwareAspectJAutoProxyCreator拦截组件的创建过程
4-2-2. 组件创建完之后,判断组件是否需要增强 是:切面的通知方法,包装成增强器(Advisor);给业务逻辑组件创建一个代理对象(cglib); - 执行目标方法:
5-1. 代理对象执行目标方法
5-2. CglibAopProxy.intercept();
5-2-1. 得到目标方法的拦截器链(增强器包装成拦截器MethodInterceptor)
5-2-2. 利用拦截器的链式机制,依次进入每一个拦截器进行执行;
5-2-3. 效果:
正常执行:前置通知-》目标方法-》后置通知-》返回通知
出现异常:前置通知-》目标方法-》后置通知-》异常通知