Browse Source

Ensure ClassLoader is set in BeanFactory

pull/97/merge
Dave Syer 13 years ago
parent
commit
97cb7f0967
  1. 12
      spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
  2. 45
      spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java

12
spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

@ -53,6 +53,7 @@ import org.springframework.core.env.MapPropertySource; @@ -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 { @@ -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());
}
}
}

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

@ -16,6 +16,9 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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;
}
}
}
}
Loading…
Cancel
Save