关于<context:property-placeholder>的一个有趣现象「建议收藏」

spring配置文件中 的讲解,案例说明!

大家好,又见面了,我是全栈君。

先来看下A和B两个模块
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块
A模块的Spring配置文件如下:

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

其配置文件位于类路径conf/conf_a.properties中:

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

B模块

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

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

其配置文件位于类路径conf/conf_b.properties中:

moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver  
moduleb.jdbc.username=cartan  
moduleb.jdbc.password=superman  
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}”

到底出了啥问题

随便搜索了一下,还发现很多人遇到这个问题,这个就是来自stackoverflow的问题:
http://stackoverflow.com/questions/7940452/spring-application-context-not-able-to-load-property-placeholder-properties

可惜啊,好像都没有人给出正确的解决。

那究竟是什么问题呢?也想了很久哦….终于回想起来了(写书时读过Spring源码),原来是Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。

<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。

拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer,因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。

咋解决呢?

定位问题需要9999元钱,解决问题只需要1元钱 。
属性文件加载在统一的地方做,不要分模块加载即可。

A模块a.xml:


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

B模块b.xml:

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

集成:

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

进一步思考

为什么啊?Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,而外面有更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用<context:property-placeholder/>引用到JAR中的属性文件,只要你要外而的Spring配置文件中显示提供一个<context:property-placeholder/>指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。

想了一想,Spring这样做是利大于弊的。

转载原文地址:关于的一个有趣现象

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/121228.html原文链接:https://javaforall.net

(0)
上一篇 2022年2月25日 下午2:00
下一篇 2022年2月25日 下午3:00


相关推荐

  • 阿里批准林俊旸辞职,谷歌DeepMind向千问员工抛橄榄枝

    阿里批准林俊旸辞职,谷歌DeepMind向千问员工抛橄榄枝

    2026年3月12日
    2
  • intellij idea激活成功教程版安装步骤_IntelliJ IDEA激活成功教程版

    intellij idea激活成功教程版安装步骤_IntelliJ IDEA激活成功教程版参考文章:1、里面有idea2018网盘下载地址idea2018激活成功教程版下载解压完:2、详细安装图解:IntelliJIDEA下载安装(含注册码)傻瓜式安装即可哈哈,安装完run配置一路点击next,这里配置的功能,以后也可以修改,所以不用担心。…

    2022年10月9日
    4
  • PyQuery常用用法总结

    PyQuery常用用法总结什么是PyQueryPyQuery是一个类似于jQuery的解析网页工具,使用lxml操作xml和html文档,它的语法和jQuery很像。和XPATH,BeautifulSoup比起来,PyQuery更加灵活,提供增加节点的class信息,移除某个节点,提取文本信息等功能。初始化PyQuery对象html文档的所有操作都需要PyQuery对象来完成,初始化PyQuery对象主要有三种方式…

    2022年5月20日
    42
  • “ASP.default_aspx”并不包含“DropDownList1_SelectedIndexChanged”的定义,其解决方法。[通俗易懂]

    “ASP.default_aspx”并不包含“DropDownList1_SelectedIndexChanged”的定义,其解决方法。[通俗易懂]“ASP.default_aspx”并不包含“DropDownList1_SelectedIndexChanged”的定义,其解决方法。在使用DropDownList控件的DataBind方法,将ArrayList数组绑定在DropDownList空间中,执行程序,出现错误:…出现错误的原因:…解决方法:…

    2022年7月18日
    23
  • 快慢指针及其应用

    快慢指针及其应用算法学习笔记 快慢指针及其应用 前言一 什么是快慢指针二 快慢指针的应用 1 引入库 2 读入数据总结前言 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 计算机算法是一门非常有意思的科学 经常研究算法能激发我们大脑的潜能 使我们的大脑变得越来越灵活 思考问题变得更加敏捷和全面 本文是我学习快慢指针相关知识的一个读书笔记 包含快慢指针的应用例子 及快慢指针在这个例子中的应用为什么会正确的数学证明 在算法方面 我也是一个初学者 以前也没有深入的 专门的

    2026年3月17日
    2
  • SchedulerFactoryBean初始化监听

    SchedulerFactoryBean初始化监听SchedulerFactoryBean初始化监听今天碰到一个问题,使用的是Quartz动态控制定时器的运行,功能已经完善,但是每次上线定时项目的时候,总要重启,一重启,所有定时任务自动就停止了,就会跟数据库对应的定时器状态不一致,在网上找了半天,找到了关于SchedulerFactoryBean初始化监听的东西,网上的文章大部分是SchedulerFactoryBean初始化解析,全部去研究…

    2022年5月24日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号