---
title: "Spring 常用基础点汇总(二)Spring中Properties文件读取"
author: "huttoncs"
date: "2016-12-16T18:19:28Z"
tags: []
categories: ["开源软件"]
slug: "spring-spring-properties"
draft: false
lastmod: "2016-12-16T18:19:28Z"
id: 2345351
url: "https://huttoncs.iteye.com/blog/2345351"
---

Spring对Properties文件的管理

 

一、通过util方式

   util方式有两种方法，一种是通过配置bean方式，一种是通过标签方式。具体方式如下：

 1、通过配置bean的方法

   

01. &lt;!-- 将多个配置文件位置放到列表中 --&gt;
02. &lt;bean id="propertyResources" class="java.util.ArrayList"&gt;
03. &lt;constructor-arg&gt;
04. &lt;list&gt;
05. &lt;!-- 这里支持多种寻址方式：classpath和file --&gt;
06. &lt;value&gt;classpath:/opt/demo/config/db.properties&lt;/value&gt;
07. &lt;!-- 推荐使用file的方式引入，这样可以将配置和代码分离 --&gt;
08. &lt;value&gt;file:/opt/demo/config/demo-mq.properties&lt;/value&gt;
09. &lt;value&gt;file:/opt/demo/config/demo-remote.properties&lt;/value&gt;
10. &lt;/list&gt;
11. &lt;/constructor-arg&gt;
12. &lt;/bean&gt;

2、通过标签的方式

   标签其实是对配置的一种简化的写法，看起来更简洁，但一次只能配置一个property文件，加载多个时需要配置多个util标签。另外在配置前物业添加上必须在XML中加入util名称空间(namespace)

 

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="

   http://www.springframework.org/schema/util 

   http://www.springframework.org/schema/util/spring-util-3.1.xsd 

"

具体的使用方式如下：

&lt;util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties" /&gt; 

 

 3、对util配置的properties引用方式如下：

     (1).bean配置中的引用

          &lt;property name="driverClassName" value="#{remoteSettings\['db.driver']}" /&gt;

  &lt;property name="url" value="#{remoteSettings\['db.url']}" /&gt;

  &lt;property name="username" value="#{remoteSettings\['db.userName']}" /&gt;

  &lt;property name="password" value="#{remoteSettings\['db.password']}"/&gt;

 

     （2）java 类中通过注解方式的引用

   

1. public class Client() {
2. @Value("#{remoteSettings\['remote.ip']}")
3. private String ip;
4. @Value("#{remoteSettings\['remote.port']}")
5. private String port;
6. @Value("#{remoteSettings\['remote.serviceName']}")
7. private String service;
8. }

当Bean中存在Properties类型的类变量需要以注入的方式初始化

1. public class Client() {
2. @Value("#{remoteSettings}")
3. private Properties remoteSettings;
4. }

 

 

二、通过Spring属性占位符PropertyPlaceholderConfigurer的使用

同样会有两种方式，一种是通过bean配置方式，一种是通过标签的方式

1、bean配置方式

  &lt;bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;

        &lt;!-- 

        使用location属性定义单个配置文件

        &lt;property name="location"&gt;

            &lt;value&gt;classpath:/com/zsw/config/jdbc.properties&lt;/value&gt;

        &lt;/property&gt;

         --&gt;

  

  &lt;!-- 使用locations属性定义多个配置文件 --&gt;

  &lt;property name="locations"&gt;

     &lt;list&gt;

        &lt;value&gt;classpath:/com/zsw/config/jdbc.properties&lt;/value&gt;

     &lt;/list&gt;

  &lt;/property&gt; 

&lt;/bean&gt;

 

此种方式可以结合util配置方式一起使用，如下：

 

&lt;!-- 用Spring加载和管理DB属性配置文件 --&gt;  

    &lt;bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;  

        &lt;property name="order" value="1" /&gt;  

        &lt;property name="ignoreUnresolvablePlaceholders" value="true" /&gt;   

        &lt;property name="locations" ref="remoteSettings" /&gt;  

    &lt;/bean&gt;

 

 

2 、通过标签的方式

     此标签是在spring3.0以后引用的

1. &lt;context:property-placeholder location="userinfo.properties"/&gt;

PropertyPlaceholderConfigurer内置的功能非常丰富，如果它未找到${xxx}中定义的xxx键，它还会去JVM系统属性（System.getProperty()）和环境变量（System.getenv()）中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性，开发者能够控制这一行为

 

3、bean配置中对占位符的使用

1. bean name="userInfo" class="test.UserInfo"&gt;
2. &lt;property name="username" value="${db.username}"/&gt;
3. &lt;property name="password" value="${db.password}"/&gt;
4. &lt;/bean&gt;

 

/\*\*\*--------------------------------------------------分割线 -------------------------------------\*\*/

 

spring对各种第三方插件都做了很好的支持，同样也对Apache公司提交的配置管理插件提供了很好的支持，通过从数据库中读出配置信息的方式很好的解决了环境差异的问题，集成的方法如下：

引用所使用的jar包：

&lt;!-- configuration to db support --&gt;

&lt;dependency&gt;

&lt;groupId&gt;commons-configuration&lt;/groupId&gt;

&lt;artifactId&gt;commons-configuration&lt;/artifactId&gt;

&lt;version&gt;1.9&lt;/version&gt;

&lt;/dependency&gt;

&lt;dependency&gt;

&lt;groupId&gt;org.springmodules&lt;/groupId&gt;

&lt;artifactId&gt;spring-modules-jakarta-commons&lt;/artifactId&gt;

&lt;version&gt;0.8a&lt;/version&gt;

&lt;exclusions&gt;

&lt;exclusion&gt;

&lt;groupId&gt;xerces&lt;/groupId&gt;

&lt;artifactId&gt;xerces&lt;/artifactId&gt;

&lt;/exclusion&gt;

&lt;exclusion&gt;

&lt;groupId&gt;org.springframework&lt;/groupId&gt;

&lt;artifactId&gt;spring-support&lt;/artifactId&gt;

&lt;/exclusion&gt;

&lt;exclusion&gt;

&lt;groupId&gt;commons-fileupload&lt;/groupId&gt;

&lt;artifactId&gt;commons-fileupload&lt;/artifactId&gt;

&lt;/exclusion&gt;

&lt;exclusion&gt;

&lt;artifactId&gt;commons-chain&lt;/artifactId&gt;

&lt;groupId&gt;commons-chain&lt;/groupId&gt;

&lt;/exclusion&gt;

&lt;/exclusions&gt;

&lt;/dependency&gt;

 

 

具体的配置信息如下：

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;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"&gt;

 

&lt;description&gt;从数据库装入配置到 propertyConfigurer&lt;/description&gt;

 

&lt;!-- 方式一--&gt;

&lt;!-- &lt;context:property-placeholder  properties-ref="applicationProperties" /&gt;  --&gt;

&lt;!-- 方式二 --&gt;

&lt;bean id="propertyConfigurer"  

   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;  

&lt;property name="properties" ref="applicationProperties"&gt;  

     &lt;/property&gt;  

&lt;/bean&gt; 

 

&lt;bean name="applicationProperties" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean"&gt;

&lt;constructor-arg ref="databaseConfiguration" /&gt;

&lt;/bean&gt;

 

&lt;bean name="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration"&gt;

&lt;constructor-arg index="0" ref="dataSource" /&gt;

&lt;constructor-arg index="1" value="T\_APP\_PROPERTIES" /&gt;

&lt;constructor-arg index="2" value="APP\_ID" /&gt;

&lt;constructor-arg index="3" value="KEY\_" /&gt;

&lt;constructor-arg index="4" value="VALUE\_" /&gt;

&lt;constructor-arg index="5" value="test" /&gt;

&lt;/bean&gt;

 

&lt;/beans&gt;

 

 

对Apache DatabaseConfiguration 的使用可以参考其它文档信息

其中配置的构造参数很容易通过读代码看出来，第0个是数据源、第1个是表名、第2、3、4分别对应表中的字段，第5个字段表示的是第2个字段的值，即 APP\_ID="test";KEY_ 对应前面的"db.username",VALUE\_为对应的值