diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 7e1a8338514..a3cf7b5c5ad 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -53,6 +53,7 @@ import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.env.StandardEnvironment; +import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.SpringFactoriesLoader; @@ -442,8 +443,15 @@ public class SpringApplication { } } - if (context instanceof GenericApplicationContext && this.resourceLoader != null) { - ((GenericApplicationContext) context).setResourceLoader(this.resourceLoader); + if (this.resourceLoader != null) { + if (context instanceof GenericApplicationContext) { + ((GenericApplicationContext) context) + .setResourceLoader(this.resourceLoader); + } + if (context instanceof DefaultResourceLoader) { + ((DefaultResourceLoader) context).setClassLoader(this.resourceLoader + .getClassLoader()); + } } } diff --git a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index 4fe3210c1bc..33ffeab5b70 100644 --- a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -16,6 +16,9 @@ package org.springframework.boot.builder; +import java.net.URL; +import java.net.URLClassLoader; + import org.junit.After; import org.junit.Test; import org.springframework.context.ApplicationContext; @@ -23,6 +26,8 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.StaticApplicationContext; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.ResourceLoader; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -77,6 +82,31 @@ public class SpringApplicationBuilderTests { any(ApplicationContext.class)); } + @Test + public void contextWithClassLoader() throws Exception { + SpringApplicationBuilder application = new SpringApplicationBuilder( + ExampleConfig.class).contextClass(SpyApplicationContext.class); + ClassLoader classLoader = new URLClassLoader(new URL[0], getClass() + .getClassLoader()); + application.resourceLoader(new DefaultResourceLoader(classLoader)); + this.context = application.run(); + assertThat(((SpyApplicationContext) this.context).getClassLoader(), + is(equalTo(classLoader))); + } + + @Test + public void parentContextWithClassLoader() throws Exception { + SpringApplicationBuilder application = new SpringApplicationBuilder( + ChildConfig.class).contextClass(SpyApplicationContext.class); + ClassLoader classLoader = new URLClassLoader(new URL[0], getClass() + .getClassLoader()); + application.resourceLoader(new DefaultResourceLoader(classLoader)); + application.parent(ExampleConfig.class); + this.context = application.run(); + assertThat(((SpyApplicationContext) this.context).getResourceLoader() + .getClassLoader(), is(equalTo(classLoader))); + } + @Test public void parentFirstCreation() throws Exception { SpringApplicationBuilder application = new SpringApplicationBuilder( @@ -127,7 +157,8 @@ public class SpringApplicationBuilderTests { public static class SpyApplicationContext extends AnnotationConfigApplicationContext { - ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext()); + private ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext()); + private ResourceLoader resourceLoader; @Override public void setParent(ApplicationContext parent) { @@ -138,5 +169,15 @@ public class SpringApplicationBuilderTests { return this.applicationContext; } + @Override + public void setResourceLoader(ResourceLoader resourceLoader) { + super.setResourceLoader(resourceLoader); + this.resourceLoader = resourceLoader; + } + + public ResourceLoader getResourceLoader() { + return this.resourceLoader; + } + } -} +} \ No newline at end of file