参考https://youyu4.iteye.com/blog/2232026

      在项目开发阶段,我们常常会避免在项目中将一些地址,用户名密码等写死,因为这样的话通常在更换环境的时候会非常麻烦,要去项目中修改所有用到这些常量的地方,所以我们会在用配置文件来写这些常量,在需要更换的时候只需要修改配置文件就行,这里就会介绍如何加载这些配置文件。

一.Spring里PropertyPlaceholderConfigurer类的基本使用:

       PropertyPlaceholderConfigurer类实现了BeanFactoryPostProcessor接口,它能够对<bean/>中的属性值进行外在化管理。开发者可以提供单独的属性文件来管理相关属性。比如,存在如下属性文件db.propertis。

jdbc.url=jdbc:mysql://192.168.25.128:3306/e3-mall?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:conf/db.properties</value>
		</property>
	</bean>
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
	</bean>

       正常情况下,在dataSource的定义中不会出现${jdbc.url}、${jdbc.password}等类似信息,这里采PropertyPlaceholderConfigurer管理username和password属性的取值。DI容器实例化dataSource前,PropertyPlaceholderConfigurer会修改dataSource的元数据信息(<bean/>定义),它会用db.properties中jdbc.username对应的root值替换${jdbc.username}、jdbc.password对应的root值替换${jdbc.password}。最终,DI容器在实例化dataSource时,DruidDataSource便会得到新的属性值,而不是${jdbc.username}、${jdbc.password}等类似信息。

二.一个spring配置文件中加载多个属性配置文件:

当项目中存在多个properties配置文件的时候,就会用到locations,如下图所示,我的项目中有四份配置文件需要加载

如何使用spring加载csv_加载

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:conf/system.properties</value>
				<value>classpath:conf/db.properties</value>
				<value>classpath:conf/redis.properties</value>
				<value>classpath:conf/resources.properties</value>
			</list>
		</property>
	</bean>

如图所示,使用locations就可以加载多份配置文件,在项目中我有五份spring的配置文件,都需要加载conf下面的属性配置文件,我只需要在applicationContext-activemq.xml中一次性加载这些属性配置文件其他的spring配置文件也都可以使用了

如何使用spring加载csv_加载_02

三.多个spring配置文件分别加载属性配置文件:

     接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,下面分别是applicationContext-activemq.xml以及applicationContext-dao.xml的配置文件,其配置如下:

如何使用spring加载csv_Spring配置文件_03

<bean id="propertyPlaceholderConfigurer2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="2" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="location">
				<value>classpath:conf/system.properties</value>
		</property>
	</bean>

 

如何使用spring加载csv_bc_04

<bean id="propertyPlaceholderConfigurer1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<value>classpath:conf/db.properties</value>
				<value>classpath:conf/redis.properties</value>
				<value>classpath:conf/resources.properties</value>
			</list>
		</property>
	</bean>

 注意一:

其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则必需设置为true,也必须有这个属性,不然的话会报错,propertyConfigurerForProject2的properties文件不会被加载.而且配置的bean的id名称也必须不一样!

注意二:

(1)如果上面的system.properties与其他属性配置文件中有相同的参数配置名称,system.properties中配置的参数值不会被后面的覆盖;
(2)如果db.properties,redis.properties以及resource.properties彼此有相同参数名配置,则取最后resource.properties中的值

四.property-placeholder 使用:

为简化PropertyPlaceholderConfigurer的使用,Spring3.0提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。

<context:property-placeholder location="classpath:jdbc.properties"/>

加载多个配置文件:

<context:property-placeholder location="jdbc.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="redis.properties" ignore-unresolvable="true"/>

注意:ignore-unresolvable="true"属性必须有,这是spring3.0之后的写法,spring2.5版本只能够使用配置PropertyPlaceholderConfigurer的方式来加载属性文件

加载多个配置文件也可以写成如下的方式(在conf目录下有多个属性配置文件):

<context:property-placeholder location="classpath:conf/*.properties" />

注意:如果有多个spring配置文件都需要加载属性配置文件,在一份spring配置文件中加载所有属性配置文件之后,其他的spring配置文件也都可以获取到这些属性!

至此,spring加载属性配置文件已全部讲解完毕,对属性文件中密码暴露的问题会在下篇博文讲解。