0

I need to access a property file outside war. I am keeping path to the outside property

file in a class path property file. The Project version is Spring 2.5.1 and

@Configuration and Environemnt classes are not available unfortunately.

System.properties is in the class path it has key entry to the outside property.

key.externalFile=C:\temp\external.properties

I tried with below approach

            <context:property-placeholder location="classpath:System.properties"/>
            <context:property-placeholder location="file:${key.externalFile}" />

In the external.properties file it has id

        keyValue= 444; 

I need to inject this value to a bean as below.

        <bean id="helloWorldBean"   class="com.java.snippets.enterprise.services.HelloWorld">
<property name="key" value="${keyValue} />

I am getting the error Could not resolve placeholder 'keyValue' in string value "${keyValue}"

I also tried out with

<context:property-placeholder location="file:${key.supportiveFile}" />  

 <util:properties id="props" location="file:${key.supportiveFile}"/>

but it ended with the same results.

<bean id="helloWorldBean"
    class="com.java.snippets.enterprise.services.HelloWorld">
     <property name="key" value="${props.keypath}" />
</bean>

Please help me and looking forward.

Complete Code 1. Context file

      <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();


} 

Thanks. Unfortunately it still cannot resolve. Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ${key.externalFile} (The system cannot find the file specified) 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) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:123) at org.springframework.context.support.ClassPathXmlApplicationContext.t.invok

Kasun
  • 561
  • 11
  • 22

1 Answers1

0

Remove the context attribute definitions of propertyPlaceholderConfigurer and replace them with:

    <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>
Prasad
  • 3,785
  • 2
  • 14
  • 23
  • Thanks , i am getting the above exception. Is this working for spring 2.5.? – Kasun Apr 21 '14 at 02:58
  • org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ${key.externalFile} (The system cannot find the file specified) at – Kasun Apr 21 '14 at 03:12