When creating a SpringApplication in their application's main method, users can apply customizations to meet their needs. When these same customisations are also needed in their application's tests, reusing the same customizations isn't particularly easy. Without sub-classing SpringBootContextLoader, you can't get hold of the SpringApplication without jumping through some hoops. It's possible via a listener and the ApplicationEnvironmentPreparedEvent but that feels harder than it should.

Related: https://github.com/spring-projects/spring-boot/issues/22379, https://github.com/spring-projects/spring-boot/issues/15077

Comment From: snicoll

This looks like what we're doing for the war-based deployment stuff. What if your Spring Boot Application could implement an additional interface that provides a hook point with SpringApplicationBuilder? When this is present the callback would be invoked to customize the application. The added-benefit is that SpringBootContextLoader would do too when running an integration test.

The downside is that an existing application class would have to be rearranged a bit to move the customization of the builder in a dedicated method. That sounds like reasonable for those who care about such customizations.

Comment From: philwebb

One possible idea we discussed was something like this:

public interface SpringApplicationCustomizer {
    void customize(SpringApplication application)
}
@SpringApplication(customizer=Foo.class)
public class MyApp {
}

Comment From: OLibutzki

As #24583 got closed as duplicate I would like to expand this issue's scope to support context hierarchies.

Comment From: wilkinsona

@OLibutzki Any customizations that are made to a SpringApplication, either directly or via SpringApplicationBuilder, are in scope for this issue. That includes context hierarchies.