Spring对Properties文件的管理
一、通过util方式
util方式有两种方法,一种是通过配置bean方式,一种是通过标签方式。具体方式如下:
1、通过配置bean的方法
- <!-- 将多个配置文件位置放到列表中 -->
- <bean id="propertyResources" class="java.util.ArrayList">
- <constructor-arg>
- <list>
- <!-- 这里支持多种寻址方式:classpath和file -->
- <value>classpath:/opt/demo/config/db.properties</value>
- <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
- <value>file:/opt/demo/config/demo-mq.properties</value>
- <value>file:/opt/demo/config/demo-remote.properties</value>
- </list>
- </constructor-arg>
- </bean>
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
"
具体的使用方式如下:
<util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties" />
3、对util配置的properties引用方式如下:
(1).bean配置中的引用
<property name="driverClassName" value="#{remoteSettings['db.driver']}" />
<property name="url" value="#{remoteSettings['db.url']}" />
<property name="username" value="#{remoteSettings['db.userName']}" />
<property name="password" value="#{remoteSettings['db.password']}"/>
(2)java 类中通过注解方式的引用
- public class Client() {
- @Value("#{remoteSettings['remote.ip']}")
- private String ip;
- @Value("#{remoteSettings['remote.port']}")
- private String port;
- @Value("#{remoteSettings['remote.serviceName']}")
- private String service;
- }
当Bean中存在Properties类型的类变量需要以注入的方式初始化
- public class Client() {
- @Value("#{remoteSettings}")
- private Properties remoteSettings;
- }
二、通过Spring属性占位符PropertyPlaceholderConfigurer的使用
同样会有两种方式,一种是通过bean配置方式,一种是通过标签的方式
1、bean配置方式
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--
使用location属性定义单个配置文件
<property name="location">
<value>classpath:/com/zsw/config/jdbc.properties</value>
</property>
-->
<!-- 使用locations属性定义多个配置文件 -->
<property name="locations">
<list>
<value>classpath:/com/zsw/config/jdbc.properties</value>
</list>
</property>
</bean>
此种方式可以结合util配置方式一起使用,如下:
<!-- 用Spring加载和管理DB属性配置文件 -->
<bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations" ref="remoteSettings" />
</bean>
2 、通过标签的方式
此标签是在spring3.0以后引用的
- <context:property-placeholder location="userinfo.properties"/>
PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为
3、bean配置中对占位符的使用
- bean name="userInfo" class="test.UserInfo">
- <property name="username" value="${db.username}"/>
- <property name="password" value="${db.password}"/>
- </bean>
/***--------------------------------------------------分割线 -------------------------------------**/
spring对各种第三方插件都做了很好的支持,同样也对Apache公司提交的配置管理插件提供了很好的支持,通过从数据库中读出配置信息的方式很好的解决了环境差异的问题,集成的方法如下:
引用所使用的jar包:
<!-- configuration to db support -->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.springmodules</groupId>
<artifactId>spring-modules-jakarta-commons</artifactId>
<version>0.8a</version>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-support</artifactId>
</exclusion>
<exclusion>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</exclusion>
<exclusion>
<artifactId>commons-chain</artifactId>
<groupId>commons-chain</groupId>
</exclusion>
</exclusions>
</dependency>
具体的配置信息如下:
<?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">
<description>从数据库装入配置到 propertyConfigurer</description>
<!-- 方式一-->
<!-- <context:property-placeholder properties-ref="applicationProperties" /> -->
<!-- 方式二 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="applicationProperties">
</property>
</bean>
<bean name="applicationProperties" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="databaseConfiguration" />
</bean>
<bean name="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg index="0" ref="dataSource" />
<constructor-arg index="1" value="T_APP_PROPERTIES" />
<constructor-arg index="2" value="APP_ID" />
<constructor-arg index="3" value="KEY_" />
<constructor-arg index="4" value="VALUE_" />
<constructor-arg index="5" value="test" />
</bean>
</beans>
对Apache DatabaseConfiguration 的使用可以参考其它文档信息
其中配置的构造参数很容易通过读代码看出来,第0个是数据源、第1个是表名、第2、3、4分别对应表中的字段,第5个字段表示的是第2个字段的值,即 APP_ID="test";KEY_ 对应前面的"db.username",VALUE_为对应的值