Content Table

Property Placeholder

properties 文件里定义 key | value,然后在 Bean Configuration File 里用 ${key} 引用 key 对应的值。

Bean Configuration File 里使用 <context:property-placeholder> 引入 properties 文件。

现在很多时候都使用 Gradle 的资源替换来代替了。

1. user.properties

1
2
username = Alice
password = Passw0rd

2. address.propertes

1
2
country = China
province = BeiJing

3. spring-beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:property-placeholder location="classpath:*.properties" ignore-unresolvable="true" file-encoding="UTF-8"/>

<!-- 多个 properties 文件用逗号分隔,只有一个 property-placeholder 生效,不能使用多个
<context:property-placeholder location="classpath:a.properties, b.properties" ignore-unresolvable="true" file-encoding="UTF-8"/> -->

<bean id="address" class="com.xtuer.beans.Address">
<property name="country" value="${country}"/>
<property name="province" value="${province}"/>
</bean>

<bean id="user" class="com.xtuer.beans.User">
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="address" ref="address"/>
</bean>
</beans>

设置 file-encoding 让 properties 文件支持中文,默认支持 ASCII。

4. PropertyPlaceholderTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import com.xtuer.beans.User;
import com.xtuer.util.CommonUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PropertyPlaceholderTest {
private static ApplicationContext context;

@BeforeClass
public static void setup() {
context = new ClassPathXmlApplicationContext("spring-beans.xml");
}

@Test
public void test() {
User user = context.getBean("user", User.class);
CommonUtils.output(user);
}
}

5. 输出

1
2
3
4
5
6
7
8
9
10
11
{
"id" : 0,
"username" : "Alice",
"password" : "Passw0rd",
"address" : {
"id" : 0,
"country" : "China",
"province" : "BeiJing",
"street" : null
}
}