是否可以配置Spring PropertyPlaceholderConfigurer以从中读取
properties.xml,通过Apache Commons Configuration吗?
最佳答案
我在seanizer和springmodule的帮助下找到了解决方案
<!-- Composite configuration -->
<bean id="configuration" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<property name="configurations">
<list>
<!-- User specifics -->
<bean class="org.apache.commons.configuration.XMLConfiguration">
<constructor-arg type="java.net.URL" value="file:cfg.xml" />
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="configuration"/>
</bean>
<bean id="testConfig" class="uvst.cfg.TestConfiguration">
<property name="domain" value="${some.prop}"></property>
</bean>
TestConfiguration类
public class TestConfiguration {
private String domain;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
}
jUnit测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/applicationContextTest.xml" })
public class ApacheCommonCfg2Spring extends AbstractJUnit4SpringContextTests {
private TestConfiguration tcfg;
@Test
public void configuration(){
tcfg = this.applicationContext.getBean("testConfig", TestConfiguration.class);
System.out.println(tcfg.getDomain());
}
}
Springmodule相当老,似乎不再被维护,但是它可以在Spring 3.0.3中使用。
随时复制和粘贴!