返回

PropertySourcesPlaceholderConfigurer的原理与实战

后端

当然,现在就来分析PropertySourcesPlaceholderConfigurer类的原理和实战使用!

PropertySourcesPlaceholderConfigurer简介

PropertySourcesPlaceholderConfigurer是一个Spring配置解析器,它可以解析XML文件和Java属性文件中的占位符。占位符的形式为${变量名},PropertySourcesPlaceholderConfigurer会根据变量名从Spring的Environment中查找对应的值,并用这个值替换占位符。

原理分析

PropertySourcesPlaceholderConfigurer的原理很简单,它主要做了以下几件事:

  1. 实现BeanDefinitionRegistryPostProcessor接口,在Spring容器加载BeanDefinition后,PropertySourcesPlaceholderConfigurer会进行后处理。
  2. 在后处理过程中,PropertySourcesPlaceholderConfigurer会解析BeanDefinition中的占位符,并用实际值替换占位符。
  3. PropertySourcesPlaceholderConfigurer支持两种占位符解析方式:XML文件占位符解析和Java属性占位符解析。

XML文件占位符解析

XML文件占位符解析是通过DocumentBuilderFactory和XPathExpressionEvaluator来实现的。DocumentBuilderFactory用于创建XML文档解析器,XPathExpressionEvaluator用于解析XML文档中的XPath表达式。

PropertySourcesPlaceholderConfigurer会先用DocumentBuilderFactory创建一个XML文档解析器,然后用这个解析器解析XML文件。解析完成后,PropertySourcesPlaceholderConfigurer会用XPathExpressionEvaluator来解析XML文档中的XPath表达式,并用实际值替换占位符。

Java属性占位符解析

Java属性占位符解析是通过PropertyPlaceholderHelper来实现的。PropertyPlaceholderHelper是一个工具类,它可以解析Java属性文件中的占位符。

PropertySourcesPlaceholderConfigurer会先用PropertyPlaceholderHelper创建一个PropertyPlaceholderHelper实例,然后用这个实例解析Java属性文件。解析完成后,PropertySourcesPlaceholderConfigurer会用实际值替换占位符。

实战案例

XML文件占位符解析

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:application.properties" />
    </bean>

    <bean id="beanWithPlaceholder" class="com.example.BeanWithPlaceholder">
        <property name="name" value="${name}" />
    </bean>
</beans>

在上面的XML文件中,我们定义了一个PropertySourcesPlaceholderConfigurer bean和一个BeanWithPlaceholder bean。PropertySourcesPlaceholderConfigurer bean用于解析XML文件中的占位符,BeanWithPlaceholder bean用于演示如何使用占位符。

在application.properties文件中,我们定义了name变量的值:

name=John Doe

当Spring容器加载BeanDefinition后,PropertySourcesPlaceholderConfigurer会进行后处理。在后处理过程中,PropertySourcesPlaceholderConfigurer会解析BeanDefinition中的占位符,并用实际值替换占位符。

解析完成后,BeanWithPlaceholder bean的name属性的值为"John Doe"。

Java属性占位符解析

@Configuration
public class JavaConfig {

    @Value("${name}")
    private String name;

    @Bean
    public BeanWithPlaceholder beanWithPlaceholder() {
        return new BeanWithPlaceholder(name);
    }
}

在上面的代码中,我们定义了一个JavaConfig类,它是一个Spring配置类。JavaConfig类中定义了一个name属性,这个属性的值是从application.properties文件中读取的。

在application.properties文件中,我们定义了name变量的值:

name=John Doe

当Spring容器加载BeanDefinition后,PropertySourcesPlaceholderConfigurer会进行后处理。在后处理过程中,PropertySourcesPlaceholderConfigurer会解析BeanDefinition中的占位符,并用实际值替换占位符。

解析完成后,JavaConfig类中的name属性的值为"John Doe"。

总结

PropertySourcesPlaceholderConfigurer是一个非常有用的Spring配置解析器,它可以解析XML文件和Java属性文件中的占位符。PropertySourcesPlaceholderConfigurer的使用非常简单,只需要在Spring配置文件中定义一个PropertySourcesPlaceholderConfigurer bean,然后在需要使用占位符的地方使用${变量名}即可。