【问题标题】:Access a property file outside war- Spring 2.5.1 version访问战争之外的属性文件- Spring 2.5.1 版本
【发布时间】: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

【问题讨论】:

  • 我明白是什么混淆了,我刚刚提到了答案中的步骤。

标签: spring spring2.x


【解决方案1】:

去掉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>

【讨论】:

  • 谢谢,我得到了上述异常。这适用于春季 2.5 吗?
  • org.springframework.beans.factory.BeanInitializationException:无法加载属性;嵌套异常是 java.io.FileNotFoundException: ${key.externalFile} (系统找不到指定的文件) at
猜你喜欢
  • 1970-01-01
  • 2011-03-18
  • 2013-03-25
  • 2011-12-16
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2010-10-20
相关资源
最近更新 更多