我有一些属性文件,我想在Spring XML配置文件中提供。例如,在hello.xml
:如何在Spring beans XML文件中使用属性文件?
<bean id="theFoo" class="learnspring.Foo">
<property name="color" value="${foo.color}"/>
</bean>
在Java代码:
ApplicationContext ac = new ClassPathXmlApplicationContext("hello.xml");
File props = new File("path/to/hello.properties");
File moreProps = new File("path/to/more.properties");
// What to do here?
Foo foo = (Foo)ac.getBean("theFoo");
System.out.println(foo.getColor());
在hello.properties
:
foo.color = blue
如何让我的属性文件中提供了Spring对象定义?
更新
我移植一些旧的春天代码。 (版本2.5),看起来有点像这样:
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource(xmlFile));
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
cfg.setLocations(new Resource[] {
new ClassPathResource(propsResourcePath),
new FileSystemResource(propsFile)) });
cfg.postProcessBeanFactory(factory);
new GenericApplicationContext(factory);
这段代码被标记为过时,我有其他的问题,所以我想将它移植到了新的途径。
也许'PropertyPlaceHolderConfigurer'? http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer –
这显示了如何在XML文件中硬编码属性的路径,但如果我在Java代码中有一些'File'对象? –