【发布时间】:2014-06-12 10:02:42
【问题描述】:
我正在使用 Spring 3.2.9.FINAL 设置一个项目,但我无法加载一些我存储在外部文件中的属性。这就是我的 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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder
location="file:/home/myapp/settings.properties" />
<bean class="foo.Test">
<property name="property" value="${test.property}" />
</bean>
</beans>
这是我的settings.properties 文件的内容:
test.property=Hello world
foo.Test 类非常简单,只包含一个 String 属性。在我的主要方法中,我这样做:
public class App {
public static void main(String[] args) {
DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(dlbf);
reader.loadBeanDefinitions(new ClassPathResource(
"/application-context.xml", Test.class));
System.out.println(dlbf.getBean(Test.class).getProperty());
}
}
这就是我执行它时得到的:
Jun 12, 2014 12:03:38 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [application-context.xml]
${test.property}
我根据 SO 的答案尝试了几种解决方法,但似乎没有任何效果。我做错了什么?
【问题讨论】:
-
能否提供
/home/myapp/settings.properties的内容? -
就
test.property=Hello world