Spring:设置一个简单的 PropertyPlaceholderConfigurer 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/557626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: Setting up a simple PropertyPlaceholderConfigurer example
提问by Jake
The solution to this is probably very simple, but I'm not sure what I'm missing. Here's what I have, and PropertyPlaceholderConfigurerwon't replace the ${...}.
对此的解决方案可能非常简单,但我不确定我错过了什么。这是我所拥有的,PropertyPlaceholderConfigurer不会替换${...}.
/* ---- org/company/springtest/Test.java: ---- */
package org.company.springtest;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main( String... args ) {
Resource res = new FileSystemResource("conf/xml/context2.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(res);
TestApp app = (TestApp) beanFactory.getBean("testApp");
app.print();
}
}
/* ---- org/company/springtest/TestApp.java: ---- */
package org.company.springtest;
import org.springframework.beans.factory.annotation.Required;
public class TestApp {
private String m_message;
public void setMessage( String message ) {
m_message = message;
}
public void print() {
System.out.println(m_message);
}
}
/* ---- conf/xml/context2.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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="file:conf/xml/test.properties" />
</bean>
<bean id="testApp" class="org.company.springtest.TestApp">
<property name="message" value="${test.message}"/>
</bean>
</beans>
/* ---- conf/xml/test.properties: ---- */
test.message=Hello world!
The following is the output when running Test:
以下是运行测试时的输出:
Feb 17, 2009 11:23:06 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\eclipse\workspace\SpringTest\conf\xml\context2.xml]
${test.message}
It looks like the Configurer is not replacing the property values...
看起来配置器没有替换属性值......
回答by cliff.meyers
Perhaps try using an ApplicationContextinstead of a BeanFactory?
也许尝试使用 aApplicationContext代替 a BeanFactory?
回答by toolkit
回答by Dhilip
Instead of Resource res = File.....use the below
而不是Resource res = File.....使用下面的
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/xml/context2.xml");
TestApp test = (TestApp) ctx.getBean("testApp");
test.print();
You will get the result.
你会得到结果。
For Resourceit will not read the Property which we have specified in the path.
因为Resource它不会读取我们在路径中指定的属性。

