zl程序教程

您现在的位置是:首页 >  后端

当前栏目

spring 配置文件 获取变量(PropertyPlaceholderConfigurer)

2023-09-11 14:19:34 时间

1.Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中一些动态设定的值(value),在XML中替换为占位该键($key$)的值,.properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性。

2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如: 

Xml代码  收藏代码
  1. <bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="location">  
  3.         <value>classpath:/spring/include/dbQuery.properties</value>  
  4.     </property>  
  5.     <property name="fileEncoding">  
  6.         <value>UTF-8</value>  
  7.     </property>  
  8. </bean>  

其中classpath是引用src目录下的文件写法,当存在多个Properties文件时,配置就需使用locations了:

Xml代码  收藏代码
  1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="locations">  
  3.         <list>  
  4.             <value>classpath:/spring/include/jdbc-parms.properties</value>  
  5.             <value>classpath:/spring/include/base-config.properties</value>  
  6.             <value>classpath*:config/jdbc.properties</value>  
  7.         </list>  
  8.     </property>  
  9. </bean>  

 

Xml代码  收藏代码
  1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="location">  
  3.         <value>classpath:hb.properties</value>  
  4.         <value>/WEB-INF/config/project/project.properties</value>  
  5.     </property>  
  6. </bean>  

接下来我们要使用多个PropertyPlaceholderConfigurer来分散配置,达到整合多工程下的多个分散的Properties文件,其配置如下

Xml代码  收藏代码
  1. <bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="order" value="2" />  
  3.     <property name="ignoreUnresolvablePlaceholders" value="true" />  
  4.     <property name="locations">  
  5.       <list>  
  6.         <value>classpath:/spring/include/jdbc-parms.properties</value>  
  7.         <value>classpath:/spring/include/base-config.properties</value>  
  8.       </list>  
  9.     </property>  
  10. </bean>   

 其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true

3.譬如,jdbc.properties的内容为:

Java代码  收藏代码
  1. jdbc.driverClassName=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round;  
  3. jdbc.username=root  
  4. jdbc.password=123456  

备注:一定要在properties文件中&写为&amp;因为在xml文件中不识别&,必须是&amp 

4.那么在spring配置文件中,我们就可以这样写:

Xml代码  收藏代码
  1. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="locations">  
  3.         <list>  
  4.             <value>classpath: conf/sqlmap/jdbc.properties </value>  
  5.         </list>  
  6.     </property>  
  7. </bean>  
  8.    
  9. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  10.     <property name="driverClassName" value="${jdbc.driverClassName}" />  
  11.     <property name="url" value="${jdbc.url}" />  
  12.     <property name="username" value="${jdbc.username}" />  
  13.     <property name="password" value="${jdbc.password}" />  
  14. </bean>  

这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具

转自: https://hbiao68.iteye.com/blog/2031006

--------------------------------------------------------------------------------------------------------

第二种方式引入properties:转自https://www.cnblogs.com/JamKong/p/4523321.html

那么该如何引入properties文件呢?在哪里进行引入?

一般情况下,如果你只有一个applicationContext.xml配置文件而已的话,那么只需要在applicationContext.xml文件中添加一行:

<!-- 导入外部的properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>  
其中:location属性是指该文件的位置。
如果是在src目录下的话,该位置为:classpath:文件名.后缀
如果是在/WEB-INF/目录下的话,该位置为: /WEB-INF/文件名.后缀。
但是要注意,不要放错位置了,<context:property-placeholder>不能发到<bean></bean>标签里面去。否则会报错;
 
说说:<context:property-placeholder/>的配置,内容好像是来自开涛博客,具体链接不知道了。
<context:property-placeholder/>配置如下:
复制代码
<context:property-placeholder
location="属性文件,多个之间逗号分隔"
file-encoding="文件编码"
ignore-resource-not-found="是否忽略找不到的属性文件"
ignore-unresolvable="是否忽略解析不到的属性,如果不忽略,找不到将抛出异常"
properties-ref="本地Properties配置"
local-override="是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性,否则相反"
system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,OVERRIDE类似于ENVIRONMENT"
order="顺序"
/>
复制代码

location:表示属性文件位置,多个之间通过如逗号/分号等分隔;

file-encoding:文件编码;

ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常

ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常

properties-ref:本地java.util.Properties配置

local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性

system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE

ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer

OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment 

NEVER:只查找properties-ref、location;

order:当配置多个<context:property-placeholder/>时的查找顺序,关于顺序问题请参考:http://www.iteye.com/topic/1131688