【问题标题】:Spring context:property-placeholder can't resovle nested variablesSpring 上下文:property-placeholder 无法解析嵌套变量
【发布时间】:2013-02-06 20:45:52
【问题描述】:

我有两个项目——project-web 和 project-service,它们都使用 Spring core 3.1.3 并具有从相应的属性文件加载属性的配置:

project-web -- 基于 Spring 集成的项目,在其 spring 配置文件中:

<context:property-placeholder location="WEB-INF/spring-integration/spring-integration.properties" ignore-resource-not-found="true" />    
<import resource="classpath*:META-INF/spring/applicationContext.xml" />

导入的地方是包含来自project-service的spring配置文件,在project-service项目中,我进行了如下配置:

<context:property-placeholder location="classpath:META-INF/application.properties, classpath:META-INF/db.properties"  ignore-resource-not-found="true"/>
<import resource="classpath:META-INF/spring/applicationContext-data.xml"/>

在我的 applicationContext-data.xml 中包含 DAO 的 Spring 配置的导入:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${db.${db.type}.driver}" />
       <property name="url" value="${db.${db.type}.url}"/>
       <property name="username" value="${db.${db.type}.username}" />
       <property name="password" value="${db.${db.type}.password}" />       
    </bean>

当我为项目服务运行单元测试时,一切都很好,所有变量都正确解析,没有任何问题。但是当我运行 project-web 时(project-service 将作为 .jar 文件包含在 project-web 的 WEB-INF/lib 文件夹中),它在启动过程中抛出错误,说 can't resolve ${db.type }:

org.springframework.beans.factory.BeanDefinitionStoreException:在类路径资源 [META-INF/spring/applicationContext-data.xml] 中定义的名称为“dataSource”的 bean 定义无效:无法解析占位符“db.type”字符串值“db.${db.type}.driver” 在 org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] 在 org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] 在 org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] ......................

注意:我不能在 project-web 中声明所有内容,因为 project-service 也会被其他项目使用。任何人都知道为什么在项目服务中单独运行但在项目网络中包含时不起作用?它无法解析嵌套变量 ${db.type}

【问题讨论】:

    标签: spring spring-integration


    【解决方案1】:

    问题是您的第一个 PropertyPlaceHolderConfigurer 正在尝试解决需要由第二个解决的占位符。

    您可以为每个前缀使用不同的前缀(例如,!{ 代替 ${ 为其中之一),或设置

    ignore-unresolvable="true"

    在第一个 - 然后它会将分辨率留给另一个。

    【讨论】:

    • 非常感谢 Gary,添加了 ignore-unresolvable="true" 属性并解决了问题。
    猜你喜欢
    • 2013-02-07
    • 2017-08-03
    • 1970-01-01
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多