前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring PlaceHolder使用注意事项

Spring PlaceHolder使用注意事项

作者头像
白凡
发布2018-08-07 17:17:05
1.8K0
发布2018-08-07 17:17:05
举报
文章被收录于专栏:光变光变

对Spring Property Placeholder如何使用,以及使用过程中遇到的问题做了简单的描述。

Spring Property Placeholder

1. 使用指南

主要是Spring从Properties文件中读取property信息。 properties文件的使用要求如下:

  1. 除非不可拒因素,properties文件必须放在项目部署的模块中(一般都为maven工程中的war模块)。
  2. 必须使用自己项目中的properties。不能引用别人jar包中的properties文件,如果需要,在工程中定义一份。
  3. 文件的命名方式为项目名称.模块名称.用途名称.properties。比如:
    • hrs.common.data.rpc.war.jdbc.properties.
  4. 测试环境中的在第3条基础上,在文件的签名加test.前缀即可。

2. Spring定义

2.1 PlaceHolder定义

PropertyPlaceholder的定义如下

代码语言:javascript
复制
<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>
2.1 Spring定义注意事项

在使用PropertyPlaceholderConfigurer的时候需要防止防止各种覆盖问题。

  1. bean的id属性必须不同,取得名称要有意义。比如使用项目名称+模块名称+用途名称+Ppc。比如
    • hrscDataJdbcPpc
  2. properties中的属性不能覆盖,如果有覆盖,Spring会按照以下规则取值。
    • 同个bean,最后定义的location中的property为最终结果。如2.1示例中,Bean-ppc1的三个location,分别定义了key相同但value不同的property,其中p.1.properties:num=1、p.2.properties:num=2、p.3.properties:num=3,最终获取num的值为3
    • 不同bean,最现加载的为最终结果(加载顺序依次为 order属性值> bean声明顺序 )。如2.1示例中,如果Bean-ppc1:name=1、Bean-ppc2:name=2,则最终获取name的值为2,因为Bean-ppc1的order属性小于Bean-ppc2的order属性。如2.1示例中,如果Bean-ppc1:hello=Tom、Bean-ppc3:hello=Jerry,则最终获取hello的值为Tom,因为纵然两个Bean的order相同,但Bean-ppc1的定义早于Bean-ppc3
    • 如果spring配置文件中有使用import标签,

3. 完整代码

p.1.properties

代码语言:javascript
复制
num = 1
hello = Tom
name = 1

p.2.properties

代码语言:javascript
复制
num = 2

p.3.properties

代码语言:javascript
复制
num = 3

p.4.properties

代码语言:javascript
复制
name = 2

p.5.properties

代码语言:javascript
复制
hello=Jerry

spring-property.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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);
    }
}

程序运行输出如下

代码语言:javascript
复制
3
2
Tom
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-11-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring Property Placeholder
    • 1. 使用指南
      • 2. Spring定义
        • 2.1 PlaceHolder定义
        • 2.1 Spring定义注意事项
      • 3. 完整代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档