Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application Startup Fails For Existing, Non-Optional Config Location, Resulting In ConfigDataLocationNotFoundException #24499

Closed
mwftapi opened this issue Dec 14, 2020 · 1 comment

Comments

@mwftapi
Copy link

@mwftapi mwftapi commented Dec 14, 2020

Since upgrading from Spring Boot 2.4.0 to Spring Boot 2.4.1 a existing, non-optional config location will stop our application from starting up by throwing a ConfigDataLocationNotFoundException: Config data location '[PATH]' cannot be found exception.

I provided a sample application for reproduction: Demo_Application_For_Reproduction.zip

In this demo application a config location will be created in the users home directory under ~/additional/config/location/[CURRENT_MILLIS] and within it a dummy config file named application-test.properties is created.

This config location will than be registered as non-optional using the spring.config.additional-location application property upon configuring the Spring Boot application prior to its startup:

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    configureApplication(new SpringApplicationBuilder()).run(args);
  }

  @Override
  protected SpringApplicationBuilder configure(
      SpringApplicationBuilder builder) {
    return configureApplication(builder);
  }

  private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
    var additionalConfigLocation = createAdditionalConfigLocation();
    createDummyConfigFile(additionalConfigLocation);
    return builder.sources(DemoApplication.class).properties(
        Map.of("spring.config.additional-location", additionalConfigLocation.toString() + "/")
    );
  }

  private static Path createAdditionalConfigLocation() {
    var additionalConfigLocation = Path
        .of(System.getProperty("user.home"),
            "/additional/config/location/" + System.currentTimeMillis());
    try {
      Files.createDirectories(additionalConfigLocation);
    } catch (IOException e) {
      throw new RuntimeException(
          "Could not create additional config location at " + additionalConfigLocation.toString(),
          e);
    }
    return additionalConfigLocation;
  }

  private static void createDummyConfigFile(Path parent) {
    var dummyConfigFile = Path.of(parent.toAbsolutePath().toString(),
        "application-test.properties");
    try {
      Files.createFile(dummyConfigFile);
      Files.writeString(dummyConfigFile, "config.property=value");
    } catch (IOException e) {
      throw new RuntimeException(
          "Could not create dummy config file at " + dummyConfigFile.toString(),
          e);
    }
  }
}

According to the latest Spring Boot Reference Documentation the optional: prefix is only required if a config location might not be existing in order to prevent receiving the ConfigDataLocationNotFoundException:

By default, when a specified config data location does not exist, Spring Boot will throw a ConfigDataLocationNotFoundException and your application will not start.

If you want to specify a location, but you don’t mind if it doesn’t always exist, you can use the optional: prefix. You can use this prefix with the spring.config.location and spring.config.additional-location properties, as well as with spring.config.import declarations.

So this above provided application should startup which it did with Spring Boot 2.4.0 but which it does no longer with Spring Boot 2.4.1 resulting in the above mentioned exception.

But as soon as we start the application with test as active profile, thus triggering the search for and parsing of the application-test.properties file within the newly created and added config location, the registered additional config location is being found and the application starts as expected.

I think that the commit 3dc03ac2752a06e015fc8ae7a6eba483b2cc863e could have introduced this behaviour.

@philwebb
Copy link
Member

@philwebb philwebb commented Dec 15, 2020

@mwftapi Thanks for the excellent report and the sample application. Your suspicion is correct, 3dc03ac did cause the problem. I've pushed a fix which should hopefully sort it for 2.4.2. In the meantime, you could prefix your location with optional:.

@philwebb philwebb modified the milestones: 2.4.x, 2.4.2 Dec 15, 2020
philwebb added a commit that referenced this issue Dec 15, 2020
Add an additional test to ensure that empty property files do not
throw ConfigDataLocationNotFoundException exceptions.

See gh-24499
See gh-24515
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants