导入XML时无法从属性文件中解析占位符

时间:2014-06-24 11:17:23

标签: xml spring properties placeholder

我很难加载一个参数,该参数在带有spring context 3.2.4.RELEASE的属性文件中定义。我的web-application-context.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:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
          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-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

  <context:property-placeholder system-properties-mode="ENVIRONMENT" ignore-resource-not-found="false" ignore-unresolvable="false" location="file:${runtime.dir}/config/properties.txt" />

  <import resource="test-${myBooleanParameter}.xml" />

我的properties.txt看起来像这样:

myBooleanParameter=true

在我的web.xml中有以下代码段:

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>classpath:web-application-context.xml</param-value>
</context-param>

不幸的是我收到了这个错误:

Unexpected exception parsing XML document from class path resource [web-application-context.xml]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'myBooleanParameter' in string value "test-${myBooleanParameter}.xml"

我查看了他正在使用的来源,我认为正确的propertySource甚至没有注册PropertySource。这是propertySources中的PropertyPlaceholderHelper.parseStringValue列表:

propertySources http://i.stack.imgur.com/jkQM2.png

可以在此处找到完整的调试日志:http://tny.cz/0ec9c339

1 个答案:

答案 0 :(得分:1)

这是一个可用于实现此目的的类:

https://github.com/PE-INTERNATIONAL/org.spring.utils