Results 1 to 9 of 9

Thread: Use of a env. variable as a path to a prop. file: how to simplify the definition?

  1. #1
    Join Date
    Dec 2005
    Posts
    269

    Default Use of a env. variable as a path to a prop. file: how to simplify the definition?

    hi all,
    in the system, that I am supporting, the majority of config params are stored in a config.properties file (which is a pretty common way). The path to that file is stored as an environment variable, like

    export SYSTEM_CONFIG=/home/me/config.properties

    now, when I tried to use the property values from the file in placeholders, it works. The only thing I need to perfrom is to add a prefix "file:" to denote an absolute path in the file system.

    Beans' definition looks like

    Code:
      <bean id="propFilePath" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
          <bean class="java.lang.String"><constructor-arg value="file:"/></bean>
        </property>
        <property name="targetMethod" value="concat"/>
        <property name="arguments">
          <list>
            <bean class="java.lang.System" factory-method="getenv">
              <constructor-arg value="SYSTEM_CONFIG"/>
            </bean>
          </list>
        </property>
      </bean>
    
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
          <list>
            <ref bean="propFilePath"/>
          </list>
        </property>
      </bean>
    where the "propFilePath" bean is doing nothing more, than concatenates "file:" and the result of System.getenv( "SYSTEM_CONFIG" ).

    But this def look too complex...

    are there any ways to simplify it? apart from parent contexts ?

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    You can do the following:

    Code:
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
          <bean class="org.springframework.core.io.FileSystemResource">
            <constructor-arg>
              <bean class="java.lang.System" factory-method="getenv">
                <constructor-arg value="SYSTEM_CONFIG"/>
              </bean>
            </constructor-arg>
          </bean>
        </property>
      </bean>

  3. #3
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    Actually, sorry this is much better...

    Code:
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:/${SYSTEM_CONFIG}"/>
      </bean>
    ...since propertyPlaceholderConfigurer can access system env vars - and can also apply to its own placeholder.

  4. #4
    Join Date
    Dec 2005
    Posts
    269

    Default

    thnax Mark, it works, of course

  5. #5
    Join Date
    Aug 2004
    Posts
    230

    Default

    I'm not seeing this behavior. When PropertyPlaceholderConfigurer.loadProperties() runs it does make any attempt to resolve variables in the location names.


    Quote Originally Posted by Mark Fisher View Post
    Code:
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="file:/${SYSTEM_CONFIG}"/>
      </bean>
    ...since propertyPlaceholderConfigurer can access system env vars - and can also apply to its own placeholder.
    Barry Kaplan (memelet)

  6. #6
    Join Date
    Dec 2005
    Posts
    269

    Default

    that nice way works only in Spring 2++.

  7. #7
    Join Date
    Mar 2009
    Posts
    10

    Default

    Hi, I am using Spring 2.5.6 and it found that it work for me at windows but not on Linux.

    Below is my config:

    Code:
    <beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <beans:property name="locations">
            	<beans:list>
            		<beans:value>file:/${CONFIG_HOME}</beans:value>
            		<beans:value>classpath:core.properties</beans:value>
            		<beans:value>classpath:plugin.properties</beans:value>
            	</beans:list>
            </beans:property>
            <beans:property name="ignoreUnresolvablePlaceholders" value="true"/>
            <beans:property name="searchSystemEnvironment" value="true"/>
            <beans:property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        </beans:bean>
    below is the exception:

    Code:
    org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: /${CONFIG_HOME} (No such file or directory)
            at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
            at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:553)
            at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:527)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:362)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
            at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
            at com.unified.cgw.CGW.main(CGW.java:23)
    Caused by: java.io.FileNotFoundException: /${CONFIG_HOME} (No such file or directory)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:106)
            at java.io.FileInputStream.<init>(FileInputStream.java:66)
            at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
            at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
            at org.springframework.core.io.UrlResource.getInputStream(UrlResource.java:123)
            at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:182)
            at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
            at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
            ... 6 more
    Any idea what went wrong with my configuration?

  8. #8
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Error message is clear enough - '/${CONFIG_HOME} (No such file or directory)'

  9. #9
    Join Date
    Mar 2009
    Posts
    10

    Default

    I solve the problem by changing the configuration to

    Code:
    <beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <beans:property name="searchSystemEnvironment" value="true"/>
            <beans:property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
            <beans:property name="locations">
            	<beans:list>
            		<beans:value>file:/${CONFIG_HOME}</beans:value>
            		<beans:value>classpath:core.properties</beans:value>
            		<beans:value>classpath:plugin.properties</beans:value>
            	</beans:list>
            </beans:property>
            <beans:property name="ignoreUnresolvablePlaceholders" value="true"/>
            
        </beans:bean>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •