对Spring Property Placeholder如何使用,以及使用过程中遇到的问题做了简单的描述。
主要是Spring从Properties文件中读取property信息。 properties文件的使用要求如下:
PropertyPlaceholder的定义如下
<bean id="ppc1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:config/p.1.properties</value>
<value>classpath*:config/p.2.properties</value>
<value>classpath*:config/p.3.properties</value>
</list>
</property>
</bean>
<bean id="ppc2"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:config/p.4.properties</value>
</list>
</property>
</bean>
<bean id="ppc3"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:config/p.5.properties</value>
</list>
</property>
</bean>
在使用PropertyPlaceholderConfigurer的时候需要防止防止各种覆盖问题。
p.1.properties
num = 1
hello = Tom
name = 1
p.2.properties
num = 2
p.3.properties
num = 3
p.4.properties
name = 2
p.5.properties
hello=Jerry
spring-property.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-4.1.xsd">
<bean id="ppc1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:config/p.1.properties</value>
<value>classpath*:config/p.2.properties</value>
<value>classpath*:config/p.3.properties</value>
</list>
</property>
</bean>
<bean id="ppc2"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:config/p.4.properties</value>
</list>
</property>
</bean>
<bean id="ppc3"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath*:config/p.5.properties</value>
</list>
</property>
</bean>
</beans>
SpringPropertyPlaceholderSample.java
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Spring测试类
*
* @author bash
* @version V1.0
* @since 2015-11-12 14:16
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-property.xml" })
@FixMethodOrder(MethodSorters.JVM)
public class SpringPropertyPlaceholderSample {
@Value("${num}")
private int num;
@Value("${name}")
private String name;
@Value("${hello}")
private String hello;
@Test
public void testProperty() {
System.out.println(num);
System.out.println(name);
System.out.println(hello);
}
}
程序运行输出如下
3
2
Tom