【发布时间】:2026-01-31 21:40:01
【问题描述】:
我正在使用 Spring 版本 4.0.6.RELEASE 并尝试从属性文件中读取 spring 并使用已解析的属性之一为另一个属性文件提供位置。我的 spring.xml 文件有以下内容:
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>classpath:version.properties</value>
<!- The below file contains the another file location -->
<value>file:${catalina.base}/conf/instance.properties</value>
<value>${another.file.location}</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
instance.properties 包含:
account.id=BlahBlahBlah
another.file.location=file:/Users/beardman/.myapp/local.properties
和 /Users/beardman/.myapp/local.properties 包含:
magic.number=3
database.endpoint=blah
我不断收到以下警告:
WARN [main] o.s.b.f.c.PropertyPlaceholderConfigurer Could not load properties from ServletContext resource [/${another.file.location}]: Could not open ServletContext resource [/${another.file.location}]
在调试我的代码时,我可以看到 account.id 被正确注入,但我永远无法让 magic.number 或 database.endpoint 显示出来。如何让 spring 使用 instance.properties 文件中的已解析属性作为 another.file.location 的值?
编辑:添加属性文件内容
【问题讨论】:
-
看起来您的路径值设置不正确或不存在
-
这是一个很好的观点@CodeFox,但我已经验证了这些文件确实存在于它们的正确位置,并且我实际上从 instance.properties 文件中获取了属性,而我的 local.properties 文件中没有任何内容。跨度>
标签: java spring properties configuration config