我可以使用Spring的util:属性和context:property-placeholder吗?

时间:2017-10-09 12:13:20

标签: spring xml-configuration

我在spring applicationContext.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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/util 
           http://www.springframework.org/schema/util/spring-util.xsd">

    <context:property-placeholder location="file:///absolute/path/to/foo.properties"/>

    <context:property-placeholder location="file:///absolute/path/to/bar.properties"/>
    <util:properties id="baz" location="file:///absolute/path/to/bar.properties"/>

我可以将调试断点放在加载属性文件的位置,并查看属性文件的值确实已加载。

但在我的bean中,@Value("#{baz.myProp}")已解决,而@Value("${myProp}")PropertyPlaceholderHelper中爆炸,因为PropertySourcesPropertyResolver无法找到myProp

DEBUG [...] - Searching for key 'myProp' in [environmentProperties]
DEBUG [...] - Searching for key 'myProp' in [systemProperties]
DEBUG [...] - Searching for key 'myProp' in [systemEnvironment]
DEBUG [...] - Could not find key 'myProp' in any property source. Returning [null]
DEBUG [...] - Searching for key 'myProp' in [localProperties]
DEBUG [...] - Could not find key 'myProp' in any property source. Returning [null]

为什么会这样,如何使其工作(使用$属性占位符解析而不是# SpEL语法?)

通过context:property-placeholder加载的属性是否被util:properties丢弃/覆盖(只能通过baz引用访问)?

1 个答案:

答案 0 :(得分:0)

Turns out这就是每个context:property-placeholder行所需的全部内容,让每个人都有机会参与解析该属性:ignore-unresolvable="true" ...