设置' connection-properties-prefix'在spring servlet上下文中使用property-placeholder

时间:2014-05-05 18:01:43

标签: spring oracle spring-mvc servlets

我在dispatch-servlet.xml文件中有以下内容......

...


<context:property-placeholder/>

<orcl:pooling-datasource id="dataSource" properties-location="resources/db/orcl.properties"
   connection-properties-prefix="${optout.env}" />

...

当servlet加载时,我看到以下错误:

  

严重:Servlet / optout引发了load()异常   org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:没有必需的'url'属性定义的pooling-datasource,属性文件不包含“url”条目
  违规资源:ServletContext资源[/WB-INF/dispatch-servlet.xml]

经过一些调试后,“PoolingDataSourceBeanDefinitionParser”从元素节点读取${optout.env}作为字符串,没有进行属性替换

为什么Spring没有替换配置文件中的${...}

1 个答案:

答案 0 :(得分:1)

所以经过一段漫长的开发周期后,我找到了一个解决方案,......

以下内容可行。

在dispact-servlet.xml文件中:

...

<orcl:pooling-datasource id="dataSource" 
    url="${${optout.env}.url}" 
    username="${${optout.env}.user}" 
    password="${${optout.env}.password}" />

<context:property-placeholder location="resources/db/orcl.properties"/>

...

Spring现在解析$ {optout.env}占位符分别正确地替换'test.url','test.user','test.password',...