通过${xxx}PropertyPlaceholderConfigurer动态加载superdiamond

由于某个项目需要读取多个数据源,因此对每个数据源模块创建一个xml文件

在applicationcontext.xml中<import>该xml,spring就能自动装配。

之前有一个疑问${jdbc.password}是如何读取superdiamond配置的呢?


首先,在applicationcontext.xml中

有这样两个bean

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties" ref="propertiesConfiguration"/>
</bean>

<bean id="propertiesConfiguration" class="com.github.diamond.client.PropertiesConfigurationFactoryBean"/>

其中<bean id="propertiesConfiguration" class="com.github.diamond.client.PropertiesConfigurationFactoryBean"/>声明的是superdiamond的配置文件。

PropertyPlaceholderConfigurer是BeanFactoryPostProcessor接口的一个实现。

 

PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。

PropertyPlaceholderConfigurer的实例化过程:

Spring在启动时会通过AbstractApplicationContext#refresh启动容器初始化工作

会委托loadBeanDefinitions解析xml配置文件

loadBeanDefinitions找到DefaultBeanDefinitionDocumentReader#parseBeanDefinition解析具体的bean

这边由于不是标准类定义,所以委托BeanDefinitionParserDelegate解析


通过id找到PropertyPlaceholderBeanDefinitionParser解析器解析

PropertySourcesPlaceholderConfigurer的继承体系

  1. BeanFactoryPostProcessor
    定义一个用于修改容器中bean definition的属性的接口.其实现类在一般类使用前先实例化,并对其他类的属性进行修改.
    这跟BeanPostProcessor有明显的区别,BeanPostProcessor是修改bean实例的.

  2. PropertiesLoaderSupport
    加载properties文件的抽象类.
    这边具体的加载逻辑是委托PropertiesLoaderUtils#fillProperties实现

  3. PropertyResourceConfigurer
    bean definition中占位符的替换就是这个抽象类实现的.
    实现BeanFactoryPostProcessor#postProcessBeanFactory,迭代容器的中的类定义,进行修改
    具体如何修改就通过钩子processProperties交由子类实现

  4. PlaceholderConfigurerSupport
    使用visitor设计模式,通过BeanDefinitionVisitor和StringValueResolver更新属性
    StringValueResolver是一个转化String类型数据的接口,真正更新属性的api实现竟然是在PropertyPlaceholderHelper#parseStringValue

  5. PropertySourcesPlaceholderConfigurer
    覆写postProcessorBeanFactory api定义解析流程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章