Spring properties that depend on other properties
<p>I would like to have properties, that I can reference via @Value in spring beans, that can only be created dependend on other properties.
In particular I am having a property, that describes the file system location of a directory.</p>

<pre><code>myDir=/path/to/mydir
</code></pre>

<p>And by convention, there is a file in that directory, that is always called <em>myfile.txt</em>.</p>

<p>Now i want to have access to both, the directory and the file, via @Value annotations inside my beans. And sometimes I want to access them as Strings, sometimes as java.io.Files and sometimes as org.springframework.core.io.FileSystemResource (which by the way works very well out of the box!). But because of that concatenating Strings on demand is not an option.</p>

<p>So what I of course could do is just declare both, but I would end up with</p>

<pre><code>myDir=/path/to/mydir
myFile/path/to/mydir/myfile.txt
</code></pre>

<p>and I would like to avoid that.</p>

<p>So I came up with an @Configuration class, that takes the property and adds it as new PropertySource:</p>

<pre><code>@Autowired
private ConfigurableEnvironment environment;

@Value("${myDir}")
private void addCompleteFilenameAsProperty(Path myDir) {
    Path absoluteFilePath = myDir.resolve("myfile.txt");

    Map&lt;String, Object&gt; props = new HashMap&lt;&gt;();
    props.put("myFile, absoluteFilePath.toString());
    environment.getPropertySources().addFirst(new MapPropertySource("additional", props));
}
</code></pre>

<p>As you can see, in my context I even created a PropertyEditor, that can convert to <em>java.nio.file.Path</em>s.</p>

<p>Now the problem is, that for some reason, this "works on my machine" (in my IDE), but does not run on the intended target environment. There I get</p>

<pre><code>java.lang.IllegalArgumentException: Could not resolve placeholder 'myFile' in string value "${myFile}"
</code></pre>