Spring 读取properties文件key+value方式
<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/context
http://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
的扫描,即只能存在一个实例!
<context:property-placeholder location="" file-encoding="" ignore-resource-not-found="" ignore-unresolvable="" properties-ref="" local-override="" system-properties-mode="" order=""
/>
- 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属性
引入其他方式引入的属性文件
<bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>classpath:default2.properties</value></list></property>
</bean>
- 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方式相关推荐
- java中读取properties文件内容五种方式
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- Sprig的EL表达式和读取Properties文件教程
Spring EL表达式 --> #{XXXXX} -----XML配置文件 <!-- Spring EL表达式--><bean id="userService&qu ...
- 五种方式让你在java中读取properties文件内容不再是难题
2019独角兽企业重金招聘Python工程师标准>>> 方式1.通过context:property-placeholder加载配置文件jdbc.properties中的内容 < ...
- SpringMVC通过注解方式读取properties文件中的值
为什么80%的码农都做不了架构师?>>> 本方法是结合Java配置及XML配置来完成. 首先定义XML配置文件 app.xml: <?xml version=" ...
- Spring 如何读取properties文件内容
http://hi.baidu.com/alizv/blog/item/d8cb2af4094662dbf3d38539.html 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置 ...
- spring使用@Value注解读取.properties文件时出现中文乱码问题的解决
spring使用@Value注解读取.properties文件时出现中文乱码问题的解决 参考文章: (1)spring使用@Value注解读取.properties文件时出现中文乱码问题的解决 (2) ...
- spring使用@Value标签读取*.properties文件的中文乱码问题的解决
spring使用@Value标签读取*.properties文件的中文乱码问题的解决 参考文章: (1)spring使用@Value标签读取*.properties文件的中文乱码问题的解决 (2)ht ...
- java读取mysql数据库配置文件_java读取properties文件的方法
Java 读写Properties配置文件 Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实 ...
- 读取properties文件
为什么80%的码农都做不了架构师?>>> Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通 ...
最新文章
- 昆仑网(D×××)去中心化虚拟互联网基础功能图片式介绍,请全屏看。
- Java动态追踪技术探究 1
- html盒子模型 1209
- sql server sysobjects 中type 和xtype
- SpringMvc上传文件遇到重复读取InputStream的问题
- 举例说明计算机网络协议,第二章计算机网络协议与体系结构.ppt
- matlab自适应遗传算法代码,自适应遗传算法MATLAB代码
- Delphi教程推荐
- faststone capture截图,怎么截不出图?关于FastStone Capture无法截图、FastStone Capture捕捉矩形区域之后没反应、
- wine qq2011beta4
- 软件工程毕业设计课题(80)微信小程序毕业设计PHP电影视频播放小程序系统设计与实现
- 【Web安全笔记】之【6.0 内网渗透】
- 转载~高德地图绘制图形并得到面积
- 复旦情商课魅力女教师上课实录
- 同行评审流程与撰写审稿意见要点
- pip执行指令后报语法错误sys.stderr.write(f”ERROR: {exc}”)解决办法
- 我国企业履行社会责任的措施
- hackthebox-Tracks-Beginner_Track-Lame
- HTML(HBuilder)作业题4- 隔行换色(jquery)
- Mac中Git如何忽略.DS_Store文件
热门文章
- 【JZOJ4246】san【最短路】【搜索】
- 服务器装系统步骤图解win7,win7安装系统图解教程
- 注意力机制-多头注意力
- Linux下的32位C程序,linux系统c语言生成.so文件,生成64位可执行文件,在64位系统中运行32位的可执行文件...
- reactos操作系统实现 2
- ArcEngine 10.2 画圆形
- ROS2与C++入门教程-在C++包里增加python支持
- 远程主机关闭了一个现有连接python_python 远程主机强迫关闭了一个现有的连接 socket 超时设置 errno 10054 | 学步园...
- Pomodoro方法
- Method threw ‘feign.codec.DecodeException‘ exception.