0
点赞
收藏
分享

微信扫一扫

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理


PropertySourcePlaceholderConfigurer的用途:通过配置文件(比如.properties文件)给bean设置属性,替代属性占位符
示例:

属性配置文件

spring.datasource.url=jdbc:mysql://xxx.xxx.xxx.xxx/test
spring.datasource.username=root
spring.datasource.password=xxxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">



    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations" value="classpath:cn/edu/tju/jdbc.properties"/>
    </bean>

    <bean id="dataSource"
          class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${spring.datasource.driver-class-name}"/>
        <property name="url" value="${spring.datasource.url}"/>
        <property name="username" value="${spring.datasource.username}"/>
        <property name="password" value="${spring.datasource.password}"/>
    </bean>


</beans>

因为在spring配置文件定义了类型为PropertySourcesPlaceholderConfigure的BeanFactoryPostProcessor,所以在容器创建dataSource这个bean时,它会对bean定义进行处理,将属性中的占位符替换为实际值。

工作原理:

当实例化PropertySourcesPlaceholderConfigurer时,通过属性注入,把bean配置时指定的属性文件名字对应的Resource保存到其父类的locations成员变量

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_java


因为PropertySourcesPlaceholderConfigurer是BeanFactoryPostProcessor,所以其postProcessBeanFactory方法会被调用,这个方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_java_02


Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_spring_03


其中调用了processProperties方法,该方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_spring_04


从上图看出,它调用了父类的doProcessProperties方法:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_mvc_05


其中调用了BeanDefinitionVistor的局部变量的visitBeanDefinition方法,这个方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_mvc_06


可以看到它首先判断BeanDefinition中是否存在属性值,如果存在,则调用visitPropertyValues方法来进行处理,这个方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_spring_07


其中调用的resolveValue方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_后端_08


Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_spring_09


其中调用的resolveStringValue方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_spring_10


可以看到它调用了成员变量valueResolver来对属性进行处理,而此处的valueResolver是创建BeanDefinitionReader时通过lambda表达式传进来的,如下图

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_java_11


因为this.ignoreUnresolvablePlaceholders为false,所以会执行propertyResolver.resolveRequiredPlaceholders(strVal),这个方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_xml_12


它调用的doResolvePlaceholders代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_mvc_13


它调用的辅助类的replacePlaceholders方法,代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_后端_14

它调用的parseStringValue代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_spring_15


Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_xml_16


Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_后端_17


其中,`

String propVal = placeholderResolver.resolvePlaceholder(placeholder);

这句完成了真正的属性解析,这里的placeholderResolver是通过lambda表达式传进来的,如下图:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_后端_18


此处的getPropertyAsRawString代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_spring_19


它调用的getProperty方法代码如下:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_xml_20


从上图可以看出,这个方法中会遍历拿到的PropertySource,根据传入的key拿到我们在属性配置文件中配置的值,然后返回,到此,从属性文件中读取属性信息的过程结束。

流程一步一步往回返,回到BeanDefinitionVisitor类:

Spring复习:(56)PropertySourcePlaceholderConfigurer的工作原理_后端_21


最后把从属性文件中获取到的值设置到pvs中,因为pvs是BeanDefinition的引用,因此,设置完成之后,beanDefinition也就被改变了


举报

相关推荐

0 条评论