Skip to content

Commit d5d23d7

Browse files
committed
Polish "Allow SpringApplicationBuilder to specify a ResourceLoader"
See gh-26690
1 parent 7b0ba35 commit d5d23d7

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/builder/SpringApplicationBuilder.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,35 @@ public class SpringApplicationBuilder {
9494
private boolean configuredAsChild = false;
9595

9696
public SpringApplicationBuilder(Class<?>... sources) {
97-
this.application = createSpringApplication(null, sources);
97+
this(null, sources);
9898
}
9999

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

104104
/**
105-
* Creates a new {@link org.springframework.boot.SpringApplication} instances from the
106-
* given sources. Subclasses may override in order to provide a custom subclass of
107-
* {@link org.springframework.boot.SpringApplication}
105+
* Creates a new {@link SpringApplication} instance from the given sources. Subclasses
106+
* may override in order to provide a custom subclass of {@link SpringApplication}
108107
* @param sources the sources
109-
* @return the {@link org.springframework.boot.SpringApplication} instance
108+
* @return the {@link SpringApplication} instance
110109
* @since 1.1.0
111-
* @deprecated Use {@link #createSpringApplication(ResourceLoader, Class...)} with
112-
* null resource loader
110+
* @deprecated since 2.6.0 for removal in 2.8.0 in favor of
111+
* {@link #createSpringApplication(ResourceLoader, Class...)}
113112
*/
113+
@Deprecated
114114
protected SpringApplication createSpringApplication(Class<?>... sources) {
115115
return new SpringApplication(sources);
116116
}
117117

118118
/**
119-
* Creates a new {@link org.springframework.boot.SpringApplication} instances from the
120-
* given sources. Subclasses may override in order to provide a custom subclass of
121-
* {@link org.springframework.boot.SpringApplication}
122-
* @param resourceLoader the resource loader, can be null to use default resource
123-
* loader (see
124-
* {@link org.springframework.boot.SpringApplication#SpringApplication(ResourceLoader, Class...)})
119+
* Creates a new {@link SpringApplication} instances from the given sources using the
120+
* given {@link ResourceLoader}. Subclasses may override in order to provide a custom
121+
* subclass of {@link SpringApplication}
122+
* @param resourceLoader the resource loader (can be null)
125123
* @param sources the sources
126-
* @return the {@link org.springframework.boot.SpringApplication} instance
127-
* @since 2.5.0
124+
* @return the {@link SpringApplication} instance
125+
* @since 2.6.0
128126
*/
129127
protected SpringApplication createSpringApplication(ResourceLoader resourceLoader, Class<?>... sources) {
130128
return new SpringApplication(resourceLoader, sources);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import java.net.URLClassLoader;
2121
import java.util.Collections;
2222

23+
import org.assertj.core.api.InstanceOfAssertFactories;
2324
import org.junit.jupiter.api.AfterEach;
2425
import org.junit.jupiter.api.Test;
2526

2627
import org.springframework.boot.ApplicationArguments;
2728
import org.springframework.boot.ApplicationContextFactory;
29+
import org.springframework.boot.SpringApplication;
2830
import org.springframework.boot.SpringApplicationShutdownHookInstance;
2931
import org.springframework.boot.WebApplicationType;
3032
import org.springframework.context.ApplicationContext;
@@ -41,6 +43,7 @@
4143

4244
import static org.assertj.core.api.Assertions.assertThat;
4345
import static org.mockito.ArgumentMatchers.any;
46+
import static org.mockito.Mockito.mock;
4447
import static org.mockito.Mockito.spy;
4548
import static org.mockito.Mockito.verify;
4649

@@ -310,13 +313,19 @@ void setEnvironmentPrefix() {
310313
}
311314

312315
@Test
313-
void createWithResourceLoader() {
314-
ClassLoader classLoader = new URLClassLoader(new URL[0], getClass().getClassLoader());
315-
SpringApplicationBuilder application = new SpringApplicationBuilder(new DefaultResourceLoader(classLoader),
316-
ExampleConfig.class)
317-
.contextFactory(ApplicationContextFactory.ofContextClass(SpyApplicationContext.class));
318-
this.context = application.run();
319-
assertThat(this.context.getClassLoader()).isEqualTo(classLoader);
316+
void customApplicationWithResourceLoader() {
317+
ResourceLoader resourceLoader = mock(ResourceLoader.class);
318+
SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(resourceLoader,
319+
ExampleConfig.class) {
320+
@Override
321+
protected SpringApplication createSpringApplication(ResourceLoader resourceLoader, Class<?>... sources) {
322+
return new CustomSpringApplication(resourceLoader, sources);
323+
}
324+
};
325+
SpringApplication application = applicationBuilder.build();
326+
assertThat(application).isInstanceOf(CustomSpringApplication.class)
327+
.asInstanceOf(InstanceOfAssertFactories.type(CustomSpringApplication.class))
328+
.satisfies((customApp) -> assertThat(customApp.resourceLoader).isEqualTo(resourceLoader));
320329
}
321330

322331
@Configuration(proxyBeanMethods = false)
@@ -329,6 +338,17 @@ static class ChildConfig {
329338

330339
}
331340

341+
static class CustomSpringApplication extends SpringApplication {
342+
343+
private final ResourceLoader resourceLoader;
344+
345+
CustomSpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
346+
super(resourceLoader, primarySources);
347+
this.resourceLoader = resourceLoader;
348+
}
349+
350+
}
351+
332352
static class SpyApplicationContext extends AnnotationConfigApplicationContext {
333353

334354
private final ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext());

0 commit comments

Comments
 (0)