Spring 读取properties文件key+value方式
来源:互联网 发布:数据库标准规范 编辑:程序博客网 时间:2024/06/05 05:29
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>userinfo.properties</value> </list> </property> </bean> <bean name="userInfo" class="test.UserInfo"> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean>
2)PreferencesPlaceholderConfigurer类进行配置:
<bean id="preferencesConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="location"> <value>config.properties</value> </property> </bean>
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>*.properties</value> </list> </property></bean><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/></bean>
<beans xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsd>
<context:property-placeholder location="classpath*:resources/*.properties" />
<beans 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"> </beans>
<util:properties id="settings" location="WEB-INF/classes/META-INF/spring/test.properties" />
2、
<context:property-placeholder>
标签具体使用:众所周知,Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer
的Bean就会停止对剩余PropertyPlaceholderConfigurer
的扫描,即只能存在一个实例!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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/>
时的查找顺序
使用注意:
1.location中的加载文件的顺序
如果location中有多个文件: classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties
将依次加载,值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值
举例来说:
default.properties文件中有个属性名userId,其对应的值为-1
default2.properties文件中也有一个属性名userId,其对应的值为-2
default3.properties文件中特有一个属性名userId,其对于那个的值为-3
default.properties文件先加载,此时userId的值为-1,当default3.properties文件加载时将更新原来的值,此时userId的值为-3,同理,最后加载default2.properties文件,所以userId最终值为-2
所以需要避免不同属性文件中的属性名称重名
2.ignore-resource-not-found和ignore-unresolvable两个属性是类似的作用,推荐配对使用
如果location中的文件指向了一个不存在的文件,那么也极有可能意味着有属性无法解析(虽然存在其他属性文件中存在重名,但是这个是应该避免的)
所以当ignore-resource-not-found设为true时,ignore-unresolvable也必须设为true,其实当ignore-unresolvable设为true时,ignore-resource-not-found的值true或false,并不影响异常的抛出
如果设置为ture,后属性值无法解析成功,将赋值为${属性名}
不推荐将ignore-resource-not-found和ignore-unresolvable的值设置为ture,默认为false,可以有效避免程序运行异常
3.properties-ref属性
引入其他方式引入的属性文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
该属性需要local-override配合使用,只有当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)
4.local-override属性
当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)
5.sytem-properties-mode属性
不同的sytem-properties-mode属性定义了不同的查找顺序
Environment环境:包括JDK环境,系统环境变量,Sevlet环境,Spring环境等,是Spring在3.1之后抽象的一个表示环境配置
在local-override属性值为false,sytem-properties-mode属性值为ENVIRONMENT或OVRRIDE时,查找顺序是location,然后是environment或者System.getProperty(),System.getenv()(Spring 3.1 之前)
即现加载location指向的属性文件,再加之environment指向的环境,当environment环境中存在和location指向的属性文件中同名的属性,则该属性的值将被修改,取决于environment环境中的值
如果sytem-properties-mode属性值为NEVER,则只查询location指向的属性文件
当local-override属性值为true时,最后将加载properties-ref指向的文件,如遇到同名的,该同名属性值将取决于properties-ref指向的文件中的值
所以,最终程序中获取的值将是一个综合作用后的值,一般情况下建议sytem-properties-mode属性值为NEVER避免ENVIRONMENT环境中的不可控
- Spring 读取properties文件key+value方式
- spring使用注解@Value读取.properties文件
- Spring使用程序方式读取properties文件
- Spring使用程序方式读取properties文件
- spring注解方式读取properties文件中值
- Spring读取properties方式
- Properties 文件读取方式
- Spring 读取 properties文件
- Spring 读取properties文件
- spring读取properties文件
- spring 读取properties文件
- spring读取properties文件
- spring读取properties文件
- Spring 读取properties文件
- 来段代码提提神-Java读取config.properties文件中的key=value
- 读取resources目录下properties文件的所有key和value,并封装成map
- Spring使用程序方式读取properties文件【笔记】
- 天易39----java与spring读取properties文件的方式
- json工具类总结
- 定时调度quartz简单用法
- Problem C: 一元二次方程类
- css 盒子模型
- rsh详细配置
- Spring 读取properties文件key+value方式
- Recurrent Neural Network系列3--理解RNN的BPTT算法和梯度消失
- 程序计时的小技巧:clock()和wtime()
- 找到匹配文件拷贝出来(带原始目录结构)
- Hadoop 基于protobuf 的RPC的客户端实现原理
- Thinkphp 5.0实战 仿百度糯米开发多商家电商平台
- Python数据分析:pandas包知识结构树
- {福利}位运算经典操作
- 一天搞定CSS:字体font--04