Spring的属性文件properties使用注意

Spring 中属性文件的配置

通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码,

  1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. <property name="locations">
  3. <list>
  4. <value>classpath:db.properties</value>
  5. //if you want you can add more here.
  6. </list>
  7. </property>
  8. </bean>

或 在Spring 3.1.x. 之后使用 PropertySourcesPlaceholderConfigurer

  1. <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  2. <property name="locations">
  3. <list>
  4. <value>classpath:db.properties</value>
  5. //if you want you can add more here.
  6. </list>
  7. </property>
  8. </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.propertiesemail.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原理相同

  1. <bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  2. <property name="locations">
  3. <list>
  4. <value>classpath:db.properties</value>
  5. <value>classpath:email.properties</value>
  6. </list>
  7. </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使用注意的更多相关文章

  1. Spring中属性文件properties的读取与使用

    实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理.例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.propert ...

  2. 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer

     spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...

  3. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  4. Spring中使用属性文件properties的两种方式

    实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...

  5. Spring MVC 属性文件读取注入到静态字段

    目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用   在项目中,有些参数需要配置到属性文件xxx.pr ...

  6. Spring配置属性文件

    在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...

  7. Spring对外部属性文件指定的某个属性进行加密、解密

    [From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...

  8. Spring多资源文件properties的配置

    Spring简化了加载资源文件的配置,可以通过<context:property-placeholder去加载,这个元素的写法如下: <context:property-placehold ...

  9. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

随机推荐

  1. 控制反转(IOC)模式

    控制反转(Inversion of Control):提倡实现松耦合层.组件和类的设计原则,颠倒程序的控制流程.IoC使用分离执行特定问题处理代码的概念: IoC意味着将你设计好的对象交给容器控制,而 ...

  2. 古董留念 - Microsoft Office 4.2中文版

    Office 4.2是Office 95的前一个版本,最适合运行在Windows 3.x上,但即使是最新的Windows 7 32位版也是可以安装它的(不信你可以试试)! 原版以软盘为载体,安装一次需 ...

  3. cn_office_Professional_Plus_2010_vol_with_sp2-【x86+x64仅2.45GB】

    用官方sp2补丁在cn_office_Professional_Plus_2010_vol基础上集成,无其他任何添加及修改! 文件: cn_office_Professional_Plus_2010_ ...

  4. jQuery 参数详解

    url: 要求为String类型的参数,发送请求的地址.如果没有填写, 默认发送当前页的url type: 要求为String类型的参数,请求方式(post或get)默认为get. 注意其他http请 ...

  5. MQTT再学习 -- 搭建MQTT服务器及测试

    最近在搞 PM2.5 采集,需要用到 MQTT 传输协议.协议部分看了几天的,讲的七七八八.本身在 intel 上有 写好的MQTT 的源码,现在的工作其实也就是移植到单片机上或者DM368板卡上.不 ...

  6. 阿里历年经典Java面试题汇总

    Volatile的特征: A.禁止指令重排(有例外) B.可见性 Volatile的内存语义: 当写一个volatile变量时,JMM会把线程对应的本地内存中的共享变量值刷新到主内存. 当读一个vol ...

  7. Linux wget auto login and backup database

    #!/bin/bash # 这是一份本来打算采用自动备份数据的代码,由于测试过程中出现了无法连接的问题,导致不能测试, # 于是最后放弃了这份代码的进一步的开发,但是记录还是有必要的 login_ur ...

  8. HDU2870 最大窗口面积(单调队列优化)

    HDU1506 HDU2870 HDU4328 以前做的是单调队列的方法,现在试一试DP 注意的是对于i,向左延伸的L[i]到i不一定是单调的,比如1 3 2 1 2,对于i=4,L[i]=1而是2所 ...

  9. Python的安装和使用ubuntu下

    ubuntu下Python的安装和使用 https://www.cnblogs.com/luckyalan/p/6703590.html ubuntu下Python的安装和使用 1 文章介绍 本文介绍 ...

  10. LG4717 【模板】快速沃尔什变换

    题意 题目描述 给定长度为\(2^n\)两个序列\(A,B\),设\(C_i=\sum_{j\oplus k}A_jB_k\)分别当\(\oplus\)是or,and,xor时求出C 输入输出格式 输 ...