我需要在战争之外访问属性文件。我正在通往外面的财产
文件在类路径属性文件中。项目版本为 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();
}
感谢。不幸的是它仍然无法解决。线程“main”中的异常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
答案 0 :(得分:0)
删除propertyPlaceholderConfigurer的上下文属性定义,并将其替换为:
<bean id="placeholderConfig1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:System.properties</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
</bean>
<bean id="placeholderConfig2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:${key.externalFile}</value>
</property>
</bean>