曹工说Spring Boot源码(18)-- Spring AOP源码分析三部曲,终于快讲完了 (aop:config完整解析【下】)

曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享

曹工说Spring Boot源码(2)-- Bean Definition到底是什么,咱们对着接口,逐个方法讲解

曹工说Spring Boot源码(3)-- 手动注册Bean Definition不比游戏好玩吗,我们来试一下

曹工说Spring Boot源码(4)-- 我是怎么自定义ApplicationContext,从json文件读取bean definition的?

曹工说Spring Boot源码(5)-- 怎么从properties文件读取bean

曹工说Spring Boot源码(6)-- Spring怎么从xml文件里解析bean的

曹工说Spring Boot源码(7)-- Spring解析xml文件,到底从中得到了什么(上)

曹工说Spring Boot源码(8)-- Spring解析xml文件,到底从中得到了什么(util命名空间)

曹工说Spring Boot源码(9)-- Spring解析xml文件,到底从中得到了什么(context命名空间上)

曹工说Spring Boot源码(10)-- Spring解析xml文件,到底从中得到了什么(context:annotation-config 解析)

曹工说Spring Boot源码(11)-- context:component-scan,你真的会用吗(这次来说说它的奇技淫巧)

曹工说Spring Boot源码(12)-- Spring解析xml文件,到底从中得到了什么(context:component-scan完整解析)

曹工说Spring Boot源码(13)-- AspectJ的运行时织入(Load-Time-Weaving),基本内容是讲清楚了(附源码)

曹工说Spring Boot源码(14)-- AspectJ的Load-Time-Weaving的两种实现方式细细讲解,以及怎么和Spring Instrumentation集成

曹工说Spring Boot源码(15)-- Spring从xml文件里到底得到了什么(context:load-time-weaver 完整解析)

曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)

曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)

工程代码地址 思维导图地址

工程结构图:

曹工说Spring Boot源码(18)-- Spring AOP源码分析三部曲,终于快讲完了 (aop:config完整解析【下】)

概要

本篇是接着前两篇讲的,为了避免不必要的重复,请大家先看下。

曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)

曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)

本篇主要讲解spring如何在bean生成过程中,完成“狸猫换太子”操作的。(利用代理对象,替换ioc容器中的原有bean)。

回顾前文

spring解析的xml如下:

<!--目标对象--> <bean/> <!--切面--> <bean/> <!--配置切入点--> <aop:config> <aop:pointcut expression="execution(public * foo.Perform.sing(..))"/> <aop:aspect ref="performAspect"> <aop:after method="afterPerform" pointcut-ref="mypointcut"/> </aop:aspect> </aop:config>

经前文的理解,总共生成了如下几个bean definition。

bean definition 中bean class 备注
PerformAspect   通知  
Performer   要切的目标  
AspectJExpressionPointcut   切点,即那一行  
org.springframework.aop.aspectj.AspectJPointcutAdvisor   advisor,解析 这一行得到  
AspectJAwareAdvisorAutoProxyCreator   实现了BeanPostProcessor接口,解析<aop:config>得到  
spring大致启动流程

解析xml、注解,通过各种渠道,得到bean definition;

从bean definition集合中,找出bean class实现了BeanFactoryPostProcessor接口的子集,然后通过getBean来获取这部分特殊的bean,然后依次调用其postProcessBeanFactory方法

来对其余的bean definition进行处理;

public interface BeanFactoryPostProcessor { /** * Modify the application context's internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for overriding or adding * properties even to eager-initializing beans. * @param beanFactory the bean factory used by the application context * @throws org.springframework.beans.BeansException in case of errors */ void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory); }

从bean definition集合中,找出bean class实现了BeanPostProcessor接口的子集,然后通过getBean来获取这部分特殊的bean,然后保存起来。

找出不具有特殊功能的(没实现BeanFactoryPostProcessor,也没实现BeanPostProcessor)bean definition 集合,再过滤出单例bean,且lazy-init属性为false(说明要在spring容器初始化过程中,实例化的bean)的这部分,作为子集;然后依次调用其getBean方法。

以上就是spring比较粗的流程,当然,很多细节没说,不过核心流程是这样的。

spring aop如何生效 1. 实例化AspectJAwareAdvisorAutoProxyCreator

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zypydw.html