【发布时间】:2018-03-20 15:32:26
【问题描述】:
我在 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 引用访问)?
【问题讨论】: