title: How to use environment variables with properties file in Spring context file
body: <p>I am trying to read in a configuration file based on a system environment variable. My environment variable is <code>FOO_ENV</code> with value <code>dev</code> and <code>dev.properties</code> contains the properties <code>bar.host</code> and <code>bar.port</code>.</p>
  
  <pre><code>&lt;context:property-placeholder /&gt; 
  &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="location" value="classpath:${FOO_ENV}.properties"&gt;&lt;/property&gt;
      &lt;property name="ignoreUnresolvablePlaceholders" value="true" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"&gt;
      &lt;property name="ignoreUnresolvablePlaceholders" value="true" /&gt;
      &lt;constructor-arg type="String" value="http://${my.host}:${my.port}/" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>When I deploy this in tomcat, I get the following error:</p>
  
  <pre><code>11:48:39.324 [localhost-startStop-14] ERROR o.s.web.context.ContextLoader - Context initialization failed
  org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'myServer' defined in ServletContext resource [/WEB-INF/my-context.xml]: 
      Could not resolve placeholder 'my.host' in string value [http://${my.host}:${my.port}]
      at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209) ~[spring-beans-3.1.2.RELEASE.jar:3.1.2.RELEASE]
  </code></pre>
  
  <p>By replacing <code>$FOO_ENV</code> with <code>dev</code> in the context file, I have determined that the properties file can be read correctly. By changing <code>FOO_ENV</code> to other names, I can show that Spring is reading the environment variable.</p>
  
  <p>It seems that the element</p>
  
  <pre><code>&lt;property name="ignoreUnresolvablePlaceholders" value="true" /&gt;
  </code></pre>
  
  <p>should allow Spring to ignore that <code>${my.host}</code> is not an environment variable, but though I've tried it in various places, I still get the same error, which indicates that <code>my.host</code> is not found.</p>