Issue
I just updated my app to use Spring Boot 2.7.1 vs 2.1.1.RELEASE. My app seems to be running OK, but only after I create the two directories 'spring' and 'spring/config' in whatever the current directory is when Spring Boot starts. If either of these directories doesn't exist, I get the error:
org.springframework.boot.context.config.ConfigDataLocationNotFoundException: Config data location '/home/fetch/spring/' cannot be found
Why is Spring requiring that these directories exist? Is there something I've done to cause it to do so? What can I do to prevent this behavior?
I can have my code create these two directories before initalizing Spring, but that seems super wonky. I want to understand why this is happening. TIA!
PS: I see where this is happening in the Spring Boot code. It happens in this method:
ConfigDataEnvironment.checkMandatoryLocations
This method builds a list of mandatory config locations, removes any of them that were able to be found, and then complains if the list is not empty. So the question is why these directories end up on Spring Boot's "mandatory locations" list.
Solution
I found the code that was the origin of the problem (in one of my company's libraries):
String homeDir = System.getProperty("user.home");
String value = "{H}/spring/config/,{H}/spring/,{H}/".replace("{H}", homeDir);
System.setProperty("spring.config.additional-location", value);
It appears that something changed in Spring. This code didn't used to cause Spring to complain if the two directories specified in the spring.config.additional-location
system property didn't exist. Now it does. Thankfully, Spring provides a way to mark these directories as optional, like this:
String homeDir = System.getProperty("user.home");
String value = "optional:{H}/spring/config/,optional:{H}/spring/,optional:{H}/".replace("{H}", homeDir);
System.setProperty("spring.config.additional-location", value);
This code gives me back the behavior I had with the older version of Spring.
Answered By - CryptoFool
Answer Checked By - Senaida (JavaFixing Volunteer)