PropertyPlaceholderConfigurer讲解
观前提示:
本文所使用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() {
}
}
测试结果