title: How to find file path (if exists) of PropertySource object
body: <pre><code>public MyObject(Environment env) {
      for(Iterator&lt;PropertySource&lt;?&gt;&gt; it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) {
          PropertySource&lt;?&gt; source = it.next();
          if (source instanceof MapPropertySource &amp;&amp; source.getSource() instanceof Properties {
              //add new property
              //get path to property file if exists and save some new properties/override existing.
  </code></pre>
  
  <p>Please consider the code above. <strong>I'd like to know if anyone can suggest to me a nice clean way to retrieve the file path or stream of a PropertySource object.</strong> </p>
  
  <p>In my particular case, in my Environment, there are two PropertySource objects that I'm interested in. One is an externally located file, one is internal. They are defined in my SpringBootApplication class as so :-</p>
  
  <pre><code>@SpringBootApplication
  @PropertySources({ 
      @PropertySource(value = { "classpath:internal.properties" }),
      @PropertySource(value = { "file:${application.properties}" }) })
  public class MyApplication extends SpringBootServletInitializer {...
  </code></pre>
  
  <p>The PropertySource object has a getName() method, which looks to contain the information I need, but, not in a friendly way.</p>
  
  <p>getName on a PropertySource with a classpath shows :-</p>
  
  <pre><code>class path resource [internal.properties]
  </code></pre>
  
  <p>getName on a PropertySource with a file path shows :-</p>
  
  <pre><code>URL [file:C:/Dev/conf/application.properties]
  </code></pre>
  
  <p>The information between the brackets is almost usable. The file one is a valid URL, the classpath one isn't.</p>
  
  <p>One solution is to just tokenise these strings and build the url string since I have enough information. I can do this and will do this if no better option is suggested. So I was wondering if anyone is aware of any useful Spring utility classes or any other better way to retrieve a valid URL/Stream/filepath from a PropertySource object.</p>
  
  <p><strong>Final notes</strong></p>
  
  <ul>
  <li>I cannot look up the environment variable manually or any paths. This needs to be generic and derived from the PropertySource object.</li>
  </ul>
  
  <p>Much appreciated.</p>