title: Using Properties in spring config
body: <p>I have a property configuration depending on my environment, like this:</p>
  
  <pre><code> &lt;bean id="someConfigurer" 
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;  
          &lt;list&gt; 
              &lt;value&gt;classpath:properties/database.${my.env}.properties&lt;/value&gt;
              &lt;value&gt;classpath:properties/morestuff.${my.env}.properties&lt;/value&gt;
              [..]
          &lt;/list&gt;  
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Now I can keep different property files in my project, for example <em>database.prod.properties</em> or <em>database.qual.properties</em>. This works fine, if I start my application with -Dmy.env=foobar.</p>
  
  <p>What happens if no environment is supplied? The application won't start, because of a FileNotFoundException thrown by the PropertyPlaceholderConfigurer. I don't want to keep a copy of all property files as a fallback. What I want is to set a environment as fallback, if the system property is not set.</p>
  
  <p>I tried to solve this with a second PropertyPlaceholderConfigurer like:</p>
  
  <pre><code>&lt;bean id="fallbackPropertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;  
          &lt;list&gt;  
              &lt;value&gt;classpath:properties/default-env.properties&lt;/value&gt;
          &lt;/list&gt;  
      &lt;/property&gt;
      &lt;property name="order" value="0" /&gt;
      &lt;property name="ignoreUnresolvablePlaceholders" value="true"/&gt;
      &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/&gt;
  &lt;/bean&gt;   
  </code></pre>
  
  <p><em>default-env.properties</em> consists of just one property: <code>my.env=qual</code>.</p>
  
  <p>The order is set to '0' to be sure, that this bean is evaluated first. I still get the following exception:</p>
  
  <pre><code>DEBUG  o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'my.env' in [systemProperties]
  DEBUG  o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'my.env' in [systemEnvironment]
  DEBUG  o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'my.env' in any property source. Returning [null]
  DEBUG  o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'someConfigurer'
  INFO   o.s.b.f.c.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/default-env.properties]
  INFO   o.s.b.f.c.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/database.${my.env}.properties]
  INFO   o.s.b.f.s.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@435a3a: defining beans [fallbackPropertyConfigurer,someConfigurer,[..],org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
  Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [properties/database.${my.env}.properties] cannot be opened because it does not exist
      at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
      [..]
  </code></pre>
  
  <p>If I comment out the PropertyPlaceholderConfigurer to get rid of the errors I can use <code>%{my.env}</code> in other beans.</p>
  
  <p>Can anybody explain this behavior?</p>