Skip to content

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

Closed
@mwftapi

Description

@mwftapi

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: regressionA regression from a previous release

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions