Spring的属性文件properties使用注意
Spring的属性文件properties使用注意
Spring 中属性文件的配置
通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码,
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
//if you want you can add more here.
</list>
</property>
</bean>
或 在Spring 3.1.x. 之后使用 PropertySourcesPlaceholderConfigurer
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
//if you want you can add more here.
</list>
</property>
</bean>
来源: http://stackoverflow.com/questions/28040889/spring-mvc-could-not-resolve-placeholder-in-string-value
更简洁的方法是使用
<context:property-placeholder location="classpath*:config/db.properties" />
问题
在一个多模块maven项目中,不同的模块需要不同的属性配置,因此使用context:property-placeholder
配置了两个properties文件:db.properties
和 email.properties
,但程序启动时遇到下述错误
caused by java.lang.illegalargumentexception could not resolve placeholder 'mail.sender.password' in string value ${mail.sender.password}
即不能处理第二个属性文件中的配置信息!
因为Spring不允许定义多个PropertyPlaceholderConfigurer
或<context:property-placeholder/>
!
spring中 context:property-placeholder 导入多个独立的 .properties配置文件?
Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
的 Bean就会停止对剩余PropertyPlaceholderConfigurer
的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer
替代PropertyPlaceholderConfigurer
了)。
换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer
(或<context:property-placeholder/>
),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。
拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer
, 因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer
Bean了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。
来源: http://blog.csdn.net/yu870646595/article/details/50979338
问题的解决方案
方案1:通配符解决、逗号分隔
使用通配符让spring一次性读取多个属性文件到一个 PropertyPlaceholderConfigurer
bean中
<context:property-placeholder location="classpath*:conf/*.properties"/>
来源: http://blog.csdn.net/yu870646595/article/details/50979338
或者使用
<context:property-placeholder location="classpath*:conf/db.properties,conf/email.properties"/>
注意: 如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值
方案2:一个PropertySourcesPlaceholderConfigurer中包含多个属性文件,和方案1原理相同
<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:email.properties</value>
</list>
</property>
方案3:使用 ignore-unresolvable
如果一定要分开定义,则在每个PropertySourcesPlaceholderConfigurer
配置中添加
<property name="ignoreUnresolvablePlaceholders" value="true"/>
或者在每个context:property-placeholder
中都加上ignore-unresolvable="true"
因为在你使用@Value("${xx}")
或在xml中使用${xx}
获取属性时,Spring会在第一个读取到的属性文件中去找,如果没有就直接抛出异常,而不会继续去第二个属性文件中找
http://stackoverflow.com/questions/18697050/multiple-spring-propertyplaceholderconfigurer-at-the-same-time
http://stackoverflow.com/questions/26657521/spring-multiple-propertyplaceholderconfigurer-in-multiple-projects-how-to-over
http://stackoverflow.com/questions/30616480/spring-value-could-not-resolve-placeholder-in-string-value
Spring的属性文件properties使用注意的更多相关文章
- Spring中属性文件properties的读取与使用
实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...
- Spring中使用属性文件properties的两种方式
实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...
- Spring MVC 属性文件读取注入到静态字段
目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用 在项目中,有些参数需要配置到属性文件xxx.pr ...
- Spring配置属性文件
在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...
- Spring对外部属性文件指定的某个属性进行加密、解密
[From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...
- Spring多资源文件properties的配置
Spring简化了加载资源文件的配置,可以通过<context:property-placeholder去加载,这个元素的写法如下: <context:property-placehold ...
- Spring Boot属性文件配置文档(全部)
This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...
随机推荐
- Python自动化运维之路-01
python的主要应用 python的擅长领域 学python有没有前途?python的语言排名 语言选择 运维会了开发后可以干什么? python的最大优势就是什么都能做. 课程概述 毕业目标 周五 ...
- c++ o2 优化
有时候,写代码的时候要卡常 这时候就要用到o2优化 #pragma GCC optimize(2) 只要把这句话加在程序开头,就可以手动开o2优化了
- TCP/UDP协议
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- Build Tree View Structure for SharePoint List Data
博客地址 http://blog.csdn.net/foxdave 此文参考自->原文链接 版权归原作者所有,我只是进行一下大致的翻译 应坛友要求,帮助验证一下功能. SharePoint列表数 ...
- Relation.js——基于pixi.js的拓展模块之人物关系图谱
出于[重构基于D3的关系图谱项目]的目的,在看完pixi.js之后,并且网上又没有现成的基于webgl的关系图谱js库,于是,本人决定自己写一个. 因为平常要工作的原因,进度可能有点慢,但是githu ...
- python切片取值和下标取值时,超出范围怎么办?
可迭代对象下标取值超出索引范围,会报错:IndexError 可迭代切片取值超出索引范围,不报错,而是返回对应的空值. a=[1,2,3,4] a[99] Traceback (most recent ...
- Python数据类型-03.序列-列表和元组
本文主要记录关于Python序列中列表和元组的定义特点和常用方法 1.序列(sequence) 1.1.序列的定义 序列是一组有顺序的元素的集合(其实是是对象的集合,后期会引入“对象”这个概念)序列包 ...
- SqlServer一些常用函数(持续更新。。。)
1. 字符串拼接: + 拼接 SELECT 'AA' + 'BB' A //AABB在2012版本sqlserver之后,可以使用cancat进行字符串拼接了. 2. 判断是否为空,并取另外的值 :I ...
- elasticsearch的插件安装
目前使用的是2.4.5版本的es 安装的时候注意以下几点 : 1.如果想所有的ip都能访问es,需要修改config下的elasticsearch.yml.修改如下 network.host=0.0. ...
- streamsets 错误记录处理
我们可以在stage 级别,或者piepline 级别进行error 处理配置 pipeline的错误记录处理 discard(丢踢) send response to Origin pipeline ...