这个在spring中配置文件中是非常常用的。

context:property-placeholder大大的方便了我们数据库的配置。

只需要在spring的配置文件里添加一句:<context:property-placeholder?location="classpath:jdbc.properties"/>?即可,这里location值为参数配置文件的位置,参数配置文件通常放在src目录下,而参数配置文件的格式跟java通用的参数配置文件相同,即键值对的形式,例如:

  1. #jdbc配置
  2. test.jdbc.driverClassName=com.mysql.jdbc.Driver
  3. test.jdbc.url=jdbc:mysql://localhost:3306/test
  4. test.jdbc.username=root
  5. test.jdbc.password=root

这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource

在配置文件里这么定义bean:

<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${test.jdbc.driverClassName}"/>
<property name="url" value="${test.jdbc.url}"/>
<property name="username" value="${test.jdbc.username}"/>
<property name="password" value="${test.jdbc.password}"/>
</bean>

这样修改起来也方便,也统一的这个规范。

另外需要注意的是,如果遇到下面着着这种问题:

A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:

A模块的Spring配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  8. <context:property-placeholder location="classpath*:conf/conf_a.properties"/>
  9. <bean class="com.xxx.aaa.Bean1"
  10. p:driverClassName="${modulea.jdbc.driverClassName}"
  11. p:url="${modulea.jdbc.url}"
  12. p:username="${modulea.jdbc.username}"
  13. p:password="${modulea.jdbc.password}"/>
  14. </beans>

conf/conf_a.properties:

  1. modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
  2. modulea.jdbc.username=cartan
  3. modulea.jdbc.password=superman
  4. modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8

B模块的Spring配置文件如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  8. <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
  9. <bean class="com.xxx.bbb.Bean1"
  10. p:driverClassName="${moduleb.jdbc.driverClassName}"
  11. p:url="${moduleb.jdbc.url}"
  12. p:username="${moduleb.jdbc.username}"
  13. p:password="${moduleb.jdbc.password}"/>
  14. </beans>

conf/conf_b.properties:

  1. moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
  2. moduleb.jdbc.username=cartan
  3. moduleb.jdbc.password=superman
  4. moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8

单独运行A模块,或单独运行B模块都是正常的,但将A和B两个模块集成后运行,Spring容器就启动不了了:
Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"
原因:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描

spring容器中最多只能定义一个context:property-placeholder,不然就出现那种个错误,那如何来解决上面的问题呢?

A和B模块去掉

  1. <context:property-placeholder location="classpath*:conf/conf_b.properties"/>

然后重新写个xml:

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:p="http://www.springframework.org/schema/p"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    8. <context:property-placeholder location="classpath*:conf/conf*.properties"/>
    9. <import resource="a.xml"/>
    10. <import resource="b.xml"/>
    11. </beans>

context:property-placeholder的更多相关文章

  1. spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

    这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...

  2. spring in action 学习十二:property placeholder 注解的方式实现避免注入外部属性硬代码化

    这里的注解是指@PropertySource这个注解.用@PropertySource这个注解加载.properties文件. 案例的目录结构如下: student.properties的代码如下: ...

  3. 应用中有多个Spring Property PlaceHolder导致@Value只能获取到默认值

    背景 工作中负责的一套计费系统需要开发一个新通知功能,在扣费等事件触发后发送MQ,然后消费MQ发送邮件或短信通知给客户.因为有多套环境,测试时需要知道是从哪套环境发出的邮件,又不想维护多套通知模板,因 ...

  4. Netty实现高性能RPC服务器优化篇之消息序列化

    在本人写的前一篇文章中,谈及有关如何利用Netty开发实现,高性能RPC服务器的一些设计思路.设计原理,以及具体的实现方案(具体参见:谈谈如何使用Netty开发实现高性能的RPC服务器).在文章的最后 ...

  5. Spring JSR-250注解

    Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...

  6. Spring Boot 环境变量读取 和 属性对象的绑定

    网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...

  7. [Camel Basics]

    Define routes: Either using Spring xml or Java DSL. Spring xml: <camelContext> <routeBuilde ...

  8. @Resource注解

    原文地址:http://blog.sina.com.cn/s/blog_a795a96f01016if1.html   @Resource 注解被用来激活一个命名资源(named resource)的 ...

  9. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

  10. 译:Spring框架参考文档之IoC容器(未完成)

    6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...

随机推荐

  1. PHP 中的BOM BUG

    对于PHP,一个小小让我不敢置信的事情很多,包括引用变量哪么迟钝,普通变量哪么牛B我己经很意外,甚至现在竟然出现了BOM头的BUG. 在PHP中,会引用很多小文件,include或require,哪么 ...

  2. 【BZOJ】【3759】Hungergame饥饿游戏

    博弈论/高斯消元 如果没有打开箱子这个操作,那么就是一个很裸的Nim游戏…… 但是有了打开箱子这个操作,就变得蛋疼了T_T 首先我们可以想到一种直接的做法:打开所有箱子,当然如果此时所有a[i]的xo ...

  3. javamail邮件发送报错解决方案

    如果你用myEclipse进行开发的话,运行时可能会出现以下的错误:Exception in thread "main" java.lang.NoClassDefFoundErro ...

  4. C# 读取枚举描述信息实例

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  5. PHP之session相关实例教程与经典代码

    ·php 中cookie和session的用法比较 ·phpmyadmin报错:Cannot start session without errors问题 ·php中cookie与session应用学 ...

  6. running android lint has encountered a problem

    最近写学习android编程的的时候,每次保存.java文件的时候,总会跳出如下错误 解决:

  7. Silverlight弹出层(转载)

    ChildWindow为Silverlight中的弹出子窗口 可以在项目新建子窗口文件: 相互传值: //父窗体向子窗体传值,需要在ChildWindow中构造函数进行传值ChildWindowTes ...

  8. 02 - Tomcat配置

    Tomcat配置 本文内容 介绍 Windows UNIX daemon 1.介绍 首选看Tomcat目录下的RUNNING.TXT 2.Windows平台下 可以选择下载windows instal ...

  9. Project Euler 92:Square digit chains 平方数字链

    题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...

  10. linux上应用程序的执行机制

    linux上应用程序的执行机制 执行文件是如何在shell中被"执行"的.本文中尽可能少用一些源码,免得太过于无 聊,主要讲清这个过程,感兴趣的同学可以去查看相应的源码了解更多的信 ...