我需要在战争之外访问一个属性文件。我正在保持通往外部财产的道路
类路径属性文件中的文件。项目版本是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} (系统找不到指定的文件) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78) at org.springframework。 context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:542) at org.springframework.context.support.AbstractApplicationContexeBeanFactoryPostProcessors(AbstractApplicationContext.java:516) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)在 org.springframework.context.support。