spring多个context:property-placeholder不生效问题

本文探讨了在Spring框架中,当多个模块各自包含属性文件时,如何避免PropertyPlaceholderConfigurer冲突导致的Spring容器启动失败问题。文章详细分析了冲突的原因,并提出了解决方案,即统一属性文件的加载位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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模块的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}"

原因是。

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了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。 

解决方法。

属性文件加载在统一的地方做,不要分模块加载即可。 

A模块a.application.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.application.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配置文件 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载数据库配置文件 --> <context:property-placeholder location="classpath:config/db.properties" /> <!-- 修改后的数据源配置(关键修改点) --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <!-- 1. 更新驱动类名 --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 2. 确保URL包含时区参数和时区支持 --> <property name="url" value="${jdbc.url}?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池配置建议调整 --> <property name="maxTotal" value="${jdbc.maxTotal}" /> <property name="maxIdle" value="${jdbc.maxIdle}" /> <property name="initialSize" value="${jdbc.initialSize}" /> </bean> <!-- 其他保持不变的配置 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:config/mybatis-config.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis.mapper" /> <property name="sqlSess
03-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值