【发布时间】:2015-11-10 17:13:55
【问题描述】:
我有几个 bean 的 Spring 上下文,例如:
<bean id="anyBean" class="com.my.app.AnyBean"
p:test_user="${any1}"
p:test_pass="${any2}">
</bean>
要解析这些占位符(${any1} 和 ${any2}),我使用:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="my.properties" />
它工作正常,但我需要从主类的 'main' 方法中定义 'my.properties' 的位置。像这样:
ApplicationContext context = new ClassPathXmlApplicationContext(my_xml_config.xml);
PropertyPlaceholderConfigurer ppc = context.getBean("propertyPlaceholderConfigurer", PropertyPlaceholderConfigurer.class);
Resource resource = context.getResource("path/to/my.properties");
ppc.setLocation(resource);
但是当我尝试启动它时:
线程“main”中的异常 org.springframework.beans.factory.BeanDefinitionStoreException: 类路径中定义的名称为“AnyBean”的无效 bean 定义 资源 [my_xml_config.xml]:无法解析占位符“any1” 字符串值“${any1}”
您能否提示有什么方法可以解决这个问题?
【问题讨论】:
标签: java spring properties