Announcement Announcement Module
Collapse
No announcement yet.
Use of a env. variable as a path to a prop. file: how to simplify the definition? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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>

    Comment


    • #3
      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.

      Comment


      • #4
        thnax Mark, it works, of course

        Comment


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


          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.

          Comment


          • #6
            that nice way works only in Spring 2++.

            Comment


            • #7
              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?

              Comment


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

                Comment


                • #9
                  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>

                  Comment

                  Working...
                  X