Skip to content

Commit

Permalink
Merge pull request #26690 from jaminh
Browse files Browse the repository at this point in the history
* pr/26690:
  Polish "Allow SpringApplicationBuilder to specify a ResourceLoader"
  Allow SpringApplicationBuilder to specify a ResourceLoader

Closes gh-26690
  • Loading branch information
snicoll committed Jun 14, 2021
2 parents 41e00e1 + d5d23d7 commit 5985234
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,40 @@ public class SpringApplicationBuilder {
private boolean configuredAsChild = false;

public SpringApplicationBuilder(Class<?>... sources) {
this.application = createSpringApplication(sources);
this(null, sources);
}

public SpringApplicationBuilder(ResourceLoader resourceLoader, Class<?>... sources) {
this.application = createSpringApplication(resourceLoader, sources);
}

/**
* Creates a new {@link org.springframework.boot.SpringApplication} instances from the
* given sources. Subclasses may override in order to provide a custom subclass of
* {@link org.springframework.boot.SpringApplication}
* Creates a new {@link SpringApplication} instance from the given sources. Subclasses
* may override in order to provide a custom subclass of {@link SpringApplication}
* @param sources the sources
* @return the {@link org.springframework.boot.SpringApplication} instance
* @return the {@link SpringApplication} instance
* @since 1.1.0
* @deprecated since 2.6.0 for removal in 2.8.0 in favor of
* {@link #createSpringApplication(ResourceLoader, Class...)}
*/
@Deprecated
protected SpringApplication createSpringApplication(Class<?>... sources) {
return new SpringApplication(sources);
}

/**
* Creates a new {@link SpringApplication} instances from the given sources using the
* given {@link ResourceLoader}. Subclasses may override in order to provide a custom
* subclass of {@link SpringApplication}
* @param resourceLoader the resource loader (can be null)
* @param sources the sources
* @return the {@link SpringApplication} instance
* @since 2.6.0
*/
protected SpringApplication createSpringApplication(ResourceLoader resourceLoader, Class<?>... sources) {
return new SpringApplication(resourceLoader, sources);
}

/**
* Accessor for the current application context.
* @return the current application context (or null if not yet running)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import java.net.URLClassLoader;
import java.util.Collections;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationContextFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationShutdownHookInstance;
import org.springframework.boot.WebApplicationType;
import org.springframework.context.ApplicationContext;
Expand All @@ -41,6 +43,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -309,6 +312,22 @@ void setEnvironmentPrefix() {
assertThat(builder.application().getEnvironmentPrefix()).isEqualTo("test");
}

@Test
void customApplicationWithResourceLoader() {
ResourceLoader resourceLoader = mock(ResourceLoader.class);
SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(resourceLoader,
ExampleConfig.class) {
@Override
protected SpringApplication createSpringApplication(ResourceLoader resourceLoader, Class<?>... sources) {
return new CustomSpringApplication(resourceLoader, sources);
}
};
SpringApplication application = applicationBuilder.build();
assertThat(application).isInstanceOf(CustomSpringApplication.class)
.asInstanceOf(InstanceOfAssertFactories.type(CustomSpringApplication.class))
.satisfies((customApp) -> assertThat(customApp.resourceLoader).isEqualTo(resourceLoader));
}

@Configuration(proxyBeanMethods = false)
static class ExampleConfig {

Expand All @@ -319,6 +338,17 @@ static class ChildConfig {

}

static class CustomSpringApplication extends SpringApplication {

private final ResourceLoader resourceLoader;

CustomSpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
super(resourceLoader, primarySources);
this.resourceLoader = resourceLoader;
}

}

static class SpyApplicationContext extends AnnotationConfigApplicationContext {

private final ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext());
Expand Down

0 comments on commit 5985234

Please sign in to comment.