【发布时间】:2014-06-04 16:33:30
【问题描述】:
我需要在战争之外访问一个属性文件。我正在保持通往外部财产的道路
类路径属性文件中的文件。项目版本为 Spring 2.5.1 和
很遗憾,@Configuration 和 Environemnt 类不可用。
System.properties 位于类路径中,它具有外部属性的键入口。
key.externalFile=C:\temp\external.properties
我尝试了以下方法
<context:property-placeholder location="classpath:System.properties"/>
<context:property-placeholder location="file:${key.externalFile}" />
在 external.properties 文件中它有 id
keyValue= 444;
我需要将该值注入到一个 bean 中,如下所示。
<bean id="helloWorldBean" class="com.java.snippets.enterprise.services.HelloWorld">
<property name="key" value="${keyValue} />
我收到错误无法解析字符串值“${keyValue}”中的占位符“keyValue”
我也试过了
<context:property-placeholder location="file:${key.supportiveFile}" />
<util:properties id="props" location="file:${key.supportiveFile}"/>
但它以相同的结果结束。
<bean id="helloWorldBean"
class="com.java.snippets.enterprise.services.HelloWorld">
<property name="key" value="${props.keypath}" />
</bean>
请帮助我并期待。
完整代码 1.上下文文件
<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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:property-placeholder location="classpath:constants.properties"/>
<context:property-placeholder location="file:${key.supportiveFile}"/>
<bean id="helloWorldBean"
class="com.java.snippets.enterprise.services.HelloWorld">
<property name="key" value="${keypath}" />
</bean>
</beans>
2. System property file - constants.properties
key.supportiveFile=C\:\\Users\\Kasun\\AppData\\Local\\Temp\\key.properties
3. Test Class
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld) context.getBean("helloWorldBean");
hello.sayHello();
}
谢谢。不幸的是,它仍然无法解决。线程“主”org.springframework.beans.factory.BeanInitializationException 中的异常:无法加载属性;嵌套异常是 java.io.FileNotFoundException: ${key.externalFile} (系统找不到指定的文件) 在 org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78) 在 org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:542) 在 org.springframework.context.support.AbstractApplicationContexeBeanFactoryPostProcessors(AbstractApplicationContext.java:516) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348) 在 org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:123) 在 org.springframework.context.support.ClassPathXmlApplicationContext.t.invok
【问题讨论】:
-
我明白是什么混淆了,我刚刚提到了答案中的步骤。