多数的鲜为人知方法都是因为有着罕见的应用,就比如说Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spring配置期间替换使用属性值。接下来让我们逐渐的深入其配置。

基本的使用方法是:(1)

  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. </bean>

其中classpath是引用src目录下的文件写法。

当存在多个Properties文件时,配置就需使用locations了:(2)

  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. </list>
  7. </property>
  8. </bean>

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

  1. <bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. <property name="order" value="1" />
  3. <property name="ignoreUnresolvablePlaceholders" value="true" />
  4. <property name="location">
  5. <value>classpath:/spring/include/dbQuery.properties</value>
  6. </property>
  7. </bean>
  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

至此你已经了解到了如何使用PropertyPlaceholderConfigurer,如何使用多个Properties文件,以及如何配置多个PropertyPlaceholderConfigurer来分解工程中分散的Properties文件。至于PropertyPlaceholderConfigurer还有更多的扩展应用

解决办法:

1) 检查配置的文件路径,文件内容是否正确

2) 当配置了多个PropertyPlaceholderConfigurer时,需要增加一些配置项。

<!-- 将多个配置文件读取到容器中,交给Spring管理 -->

<bean id="robotAuthPropertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="order" value="1" />

<property name="ignoreUnresolvablePlaceholders" value="true" />

<property name="locations">

<list>

<value>classpath*:local_redis.properties</value>

<value>classpath*:druid_db.properties</value>

</list>

</property>

</bean>

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

Spring学习总结(20)——Spring加载多个项目properties配置文件问题解决的更多相关文章

  1. Spring学习笔记(1)——资源加载

    <!-- 占坑,迟点补充底层原理 --> Spring支持4种资源的地址前缀 (1)从类路径中加载资源——classpath: classpath:和classpath:/是等价的,都是相 ...

  2. eclipse创建spring boot项目加载不到application.properties配置文件

    在配置文件application.properties中修改了端口号,但重启服务后发现端口号并没有跟着改变,发现是项目启动时没有加载application.properties文件导致 解决:项目-& ...

  3. spring源码学习之bean的加载(一)

    对XML文件的解析基本上已经大致的走了一遍,虽然没有能吸收多少,但是脑子中总是有些印象的,接下来看下spring中的bean的加载,这个比xml解析复杂的多.这个加载,在我们使用的时候基本上是:Bea ...

  4. spring启动component-scan类扫描加载过程(转)

    文章转自 http://www.it165.net/pro/html/201406/15205.html 有朋友最近问到了 spring 加载类的过程,尤其是基于 annotation 注解的加载过程 ...

  5. 抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗?

    原文链接:抛开 Spring ,你知道 MyBatis 加载 Mapper 的底层原理吗? 大家都知道,利用 Spring 整合 MyBatis,我们可以直接利用 @MapperScan 注解或者 @ ...

  6. Spring Boot自定义配置与加载

    Spring Boot自定义配置与加载 application.properties主要用来配置数据库连接.日志相关配置等.除了这些配置内容之外,还可以自定义一些配置项,如: my.config.ms ...

  7. 配置Spring的用于解决懒加载问题的过滤器

    <?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" ...

  8. Struts配置文件以Spring的方式实现自定义加载

    在使用struts时,我们需要在web.xml中配置过滤器,同时我们需要配置struts的配置文件路径来加载项目中struts的相关配置信息.如果我们不配置路径的话,Struts会有一些默认的加载路径 ...

  9. Spring学习1:Spring基本特性

    http://longliqiang88.github.io/2015/08/14/Spring%E5%AD%A6%E4%B9%A01%EF%BC%9ASpring%E5%9F%BA%E6%9C%AC ...

随机推荐

  1. Linux环境下mysql的root密码忘记解决方法(2种)

    方法一: 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以 ...

  2. C-C++字符输出时遇到字符'\n','\0'区别

    #include "iostream" #include "stdio.h" #include "stdio_ext.h" #include ...

  3. (转)!注意:PreTranslateMessage弹出框出错

    dlg.DoModal()截住了界面消息,所以返回时原来的pMsg的内容已经更改了,消息,窗口句柄都不在是if以前的值了,而且窗口句柄应该是对话框里的子窗口的句柄,所以调用CFrameWnd::Pre ...

  4. Python + selenium之unitest(2)

    unittest单元测试框架中重要的概念: 1.Test Case 一个Test Case实例就是一个测试用例.在一个完整的测试流程中,包括测试前准备环境的搭建(setUp),实现测试过程的代码(ru ...

  5. div高度不能自适应(子级使用float浮动,父级div高度不能自适应)

    1.问题截图: 2.问题描述: 由于地址.公司名长度的不定性,所以每一条地址所在的父级div高度不定,但是需要设置一个最小的高度min-height:48px;但是当内容增加的时候,父级div高度却不 ...

  6. jquery绑定事件的系统参数传递方法

    如果是传递的事件自带函数,,可使用以下语法(以鼠标移动事件为例): init: function () { $(document).on("mousemove",loginOper ...

  7. 一些常用的HTML标签

    由于本人目前没有系统学习,日常碰见哪个有用就记下来. pre标签 可定义预格式化的文本,在pre元素中的文本会保留空格和换行符.比如我们展示源代码的时候,只要放一个pre标签,然后把代码直接复制.粘贴 ...

  8. CPP-基础:new int[]跟int()的区别

    1. new int[] 是创建一个int型数组,数组大小是在[]中指定,例如: int * p = new int[10]; //p执行一个长度为10的int数组.2. new int()是创建一个 ...

  9. 修改broadcom 4322无线网卡ID教程,不再显示第三方无线网卡

    本帖最后由 hellokingabc 于 2016-1-11 03:07 编辑 黑苹果已经基本完美,但是无线网卡总是出现问题,经常断网,经过搜索,原因在于无线网卡在OSX系统下显示为第三方无线网卡,只 ...

  10. Spring框架配置文件中有两个相同名字的bean,最后会覆盖掉一个bean

    问题容易出现在多个人合作的项目中,定义bean的名字的时候发生重复. 可以配置当bean定义重复的时候抛出异常,结束程序,强制提示更改重复的bean.