PropertyPlaceholderConfigurer讲解

本文详细介绍了PropertyPlaceholderConfigurer,它是bean工厂后置处理器,用于解析${...}占位符。内容涵盖其简介、继承体系、如何读取单个和多个properties文件,以及Spring提供的替代方案< context:property-placeholder />的使用。此外,还讨论了自定义PropertyPlaceholderConfigurer的实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

观前提示:

本文所使用IDEA版本为ultimate 2019.1,JDK版本为1.8.0_141,Tomcat版本为9.0.12。

1.简介

PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,是 PlaceholderConfigurerSupport 的一个子类,用来解析${…} 占位符。

2.继承体系

在这里插入图片描述

3.使用方法

3.1 读取单个properties文件

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false
jdbc.username=root
jdbc.password=root

定义读取properties的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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>

3.2 读取多个properties文件

如果读取多个properties文件,可将location改为locations,并使用list引入,示例如下:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="locations">
    	<list>
        	<value>jdbc.properties</value>
        	<value>user.properties</value>
        </list>
    </property>
</bean>

3.3 spring的替代方案< context:property-placeholder />

Spring还提供了< context:property-placeholder />标签来替代PropertyPlaceholderConfigurer,示例如下:

<!-- order指定文件加载顺序 -->
<context:property-placeholder order="0" location="jdbc.properties"/>
<context:property-placeholder order="1" location="user.properties"/>

4.自定义PropertyPlaceholderConfigurer

测试目录如下
在这里插入图片描述

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false
jdbc.username=root
jdbc.password=root

applicationContext.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"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean id="configurer" class="testPropertyPlaceholderConfigurer.PropertyConfigurer">
        <property name="location">
            <value>testPropertyPlaceholderConfigurer/jdbc.properties</value>
        </property>
    </bean>

</beans>

PropertyConfigurer.java

package testPropertyPlaceholderConfigurer;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class PropertyConfigurer extends PropertyPlaceholderConfigurer {


    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        System.out.println("propertyName : " + propertyName);
        System.out.println("propertyValue : " + propertyValue);
        return super.convertProperty(propertyName, propertyValue);
    }
}

测试类TestProperty .java

package testPropertyPlaceholderConfigurer;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestProperty {

    private ClassPathXmlApplicationContext ac;

    @Before
    public void before() {
        ac = new ClassPathXmlApplicationContext("testPropertyPlaceholderConfigurer/applicationContext.xml");
    }

    @Test
    public void test() {

    }
}

测试结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值