Ensure ClassLoader is set in BeanFactory

This commit is contained in:
Dave Syer 2013-10-23 15:05:12 -04:00
parent 0498617411
commit 97cb7f0967
2 changed files with 53 additions and 4 deletions

View File

@ -53,6 +53,7 @@ import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource; import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
@ -442,8 +443,15 @@ public class SpringApplication {
} }
} }
if (context instanceof GenericApplicationContext && this.resourceLoader != null) { if (this.resourceLoader != null) {
((GenericApplicationContext) context).setResourceLoader(this.resourceLoader); if (context instanceof GenericApplicationContext) {
((GenericApplicationContext) context)
.setResourceLoader(this.resourceLoader);
}
if (context instanceof DefaultResourceLoader) {
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader
.getClassLoader());
}
} }
} }

View File

@ -16,6 +16,9 @@
package org.springframework.boot.builder; package org.springframework.boot.builder;
import java.net.URL;
import java.net.URLClassLoader;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.ApplicationContext; 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.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.StaticApplicationContext; 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.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
@ -77,6 +82,31 @@ public class SpringApplicationBuilderTests {
any(ApplicationContext.class)); 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 @Test
public void parentFirstCreation() throws Exception { public void parentFirstCreation() throws Exception {
SpringApplicationBuilder application = new SpringApplicationBuilder( SpringApplicationBuilder application = new SpringApplicationBuilder(
@ -127,7 +157,8 @@ public class SpringApplicationBuilderTests {
public static class SpyApplicationContext extends AnnotationConfigApplicationContext { public static class SpyApplicationContext extends AnnotationConfigApplicationContext {
ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext()); private ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext());
private ResourceLoader resourceLoader;
@Override @Override
public void setParent(ApplicationContext parent) { public void setParent(ApplicationContext parent) {
@ -138,5 +169,15 @@ public class SpringApplicationBuilderTests {
return this.applicationContext; return this.applicationContext;
} }
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
super.setResourceLoader(resourceLoader);
this.resourceLoader = resourceLoader;
}
public ResourceLoader getResourceLoader() {
return this.resourceLoader;
}
} }
} }