为了在项目中用好框架,以及出现问题时候能够快速定位、分析、优化,文章尝试从源码角度分析Spring集成apollo的过程。期望文章能够把以下几个事情描述清楚:,由于Spring Framework和Spring Boot集成apollo的方式有些许不同,分别进行分析。,
,refresh时序图,图中概括地描述了refresh过程,其中标识黄颜色的地方是这次分析的重点,下面分别进行描述。,当解析xml文件apollo:config标记的时候调用BeanDefinitionParserDelegate.parseCustomElement(…),主要流程如下:,
,个性化命名空间解析,图中流程主要完成了两件事:,apollo中ConfigPropertySourcesProcessor实现了Spring接口BeanDefinitionRegistryPostProcessor、BeanFactoryPostProcessor,ConfigPropertySourcesProcessor实现如下:,实际调用的是
DefaultConfigPropertySourcesProcessorHelper.postProcessBeanDefinitionRegistry方法,该方法主要完成以下几件事:,用于解析apollo注解ApolloConfig、
ApolloConfigChangeListener。,为注解为ApolloConfig的字段赋值对应的namespace Config对象。,为注解为ApolloConfigChangeListener的方法添加namespace的监听。,上面代码中的@EnableApolloConfig是一个接入点,后面会分析到。,
,Spring Boot启动流程,图中概括地描述了SpringApplication启动过程(其中会将启动类加入到spring容器中),其中标识黄颜色的地方是这次分析的重点,下面分别进行描述。,我们关注BeanDefinitionLoader类中属性annotatedReader=new
AnnotatedBeanDefinitionReader(registry),AnnotatedBeanDefinitionReader构造方法如下:,与我们这次分析相关的类是:
ConfigurationClassPostProcessor,该类实现了BeanDefinitionRegistryPostProcessor、BeanFactoryPostProcessor接口。,ImportResource、Bean注解,没有则直接返回;,ImportResource、Bean注解进行解析。,文章主要介绍了apollo借助Spring扩展点完成了与Spring的集成:Spring Framework集成方式使用到了loadBeanDefinitions阶段中apollo:config NamespaceHandler,BeanDefinitionRegistryPostProcessor,BeanFactoryPostProcessor,BeanPostProcessor;Spring Boot集成方式使用到了ApplicationContextInitializer,BeanDefinitionRegistryPostProcessor,BeanFactoryPostProcessor,BeanPostProcessor;,apollo还提供了通过注解类ApolloAutoConfiguration与Spring Boot完成集成,原理是一致的。
© 版权声明
文章版权归作者所有,未经允许请勿转载。