让spring的配置文件可以使用外部properties里的参数。

property文件global-config-file.properties:

Property代码  收藏代码
  1. #FOR dataSource  
  2. jdbc.dataSource.url=jdbc:postgresql://192.168.1.118:5432/DB_name  
  3. jdbc.dataSource.username=postgres  
  4. jdbc.dataSource.password=123  

示例配置的是数据源参数。

之后在springContext的配置文件中,加入下面代码,:

Xml代码  收藏代码
  1. <bean id="propertyConfigurer"    
  2.          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  3.        <property name="ignoreUnresolvablePlaceholders" value="true" />  
  4.        <property name="location" value="classpath:global-config-file.properties"/>    
  5.    </bean>   

   即引入PropertyPlaceholderConfigurer来读取property配置文件,spring框架会从其那里获取到需要的配置参数。

下面这种是引入properties文件不在src目录下的时候。

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>config/settings.properties</value>  //此文不在src下时,要这样写
<value>classpath:c3p0.properties</value>
<value>classpath:mchange-log.properties</value>
<value>classpath:mchange-commons.properties</value>
</list>
</property>
</bean>

之后再用${key}的格式取代你数据源配置参数:

Xml代码  收藏代码
  1. <bean id="myBatisDataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  2.         <property name="driverClassName" value="org.postgresql.Driver">  
  3.         </property>  
  4.         <property name="url"  
  5.             value="${jdbc.dataSource.url}">     
  6.         </property>  
  7.         <property name="username" value="${jdbc.dataSource.username}"></property>  
  8.         <property name="password" value="${jdbc.dataSource.password}"></property>  
  9.     </bean>  

 例如:${jdbc.dataSourcurl}:框架会将global-config-file.properties读到jdbc.dataSource.url的值“jdbc:postgresql://192.168.1.118:5432/DB_name”填入${jdbc.dataSource.url}所在的位置。

2,将property配置文件与工程分离(独立到服务器中)

   为什么想到要这样做呢?

   这是在实际开发部署中,每次将工程打包部署到服务器,都需要在服务器端修改工程的配置项(如数据源等),感觉很麻烦。

   如果工程优先读取放置在服务器上的配置文件,没有此配置文件才从工程目录里面读取,那样就可以将只在服务器存放一份配置文件并修改成与服务器运行环境一致,今后部署即可不用每次都修改工程配置文件了。

2.1 做法:

  刚才我们在第1点添加了一个propertyConfigurer,现将其改名为“propertyConfigurer2”,并添加一个“propertyConfigurer1”,两者配置为:

Xml代码  收藏代码
  1. <bean id="propertyConfigurer1"    
  2.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  3.         <property name="order" value="1"/>  
  4.          <property name="ignoreResourceNotFound" value="true"/>  
  5.         <property name="ignoreUnresolvablePlaceholders" value="true" />  
  6.         <property name="location" value="file:C:/tempSys/config/global-config-file.properties"/>    
  7.     </bean>   
  8.   
  9.     <bean id="propertyConfigurer2"    
  10.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
  11.         <property name="order" value="2"/>  
  12.         <property name="ignoreUnresolvablePlaceholders" value="true" />  
  13.         <property name="location" value="classpath:global-config-file.properties"/>    
  14.     </bean>   

 order属性表示加载顺序,这种配置表示先加载propertyConfigurer1,在加载propertyConfigurer2,两者配置文件中有同样的参数,后加载的参数不会覆盖先加载的。

  propertyConfigurer1需要添加一个 <property name="ignoreResourceNotFound" value="true"/>属性,表示当找不到这个配置文件时,则跳过,这样就不会抛出FileNotFoundException了。

  这样配置之后,工程会在启动时,首先检查C:/tempSys/config/global-config-file.properties是否存在,如果存在则加载器配置,随后再加载项目目录下的global-config-file.properties;如果不存在,则直接加载项目目录下的global-config-file.properties。

   

   这样做不单可将配置与开发环境分离出来,避免每次部署时繁琐的配置修改工作。同时这也提高了系统部署时的安全性,比如,实际运行的正式数据库账户密码信息将是十分机密的信息,由运维人员保管维护,可能对开发人员都不能公开(开发人员只知道开发测试数据库的信息),将这些信息直接配置在服务器上,服务器也由运维人员进行管理,这样可以减少机密信息的知晓人群,提高安全性

原文地址:https://www.cnblogs.com/sanhuan/p/4788816.html