Add support to ContextLoader for configuring context's parent

Closes gh-9749
pull/9771/head
Andy Wilkinson 7 years ago
parent 192d9ee18f
commit 3fb1bdef85

@ -62,6 +62,8 @@ class AbstractContextLoader<T extends ConfigurableApplicationContext, L extends
private ClassLoader classLoader; private ClassLoader classLoader;
private ApplicationContext parent;
protected AbstractContextLoader(Supplier<T> contextSupplier) { protected AbstractContextLoader(Supplier<T> contextSupplier) {
this.contextSupplier = contextSupplier; this.contextSupplier = contextSupplier;
} }
@ -115,6 +117,12 @@ class AbstractContextLoader<T extends ConfigurableApplicationContext, L extends
return self(); return self();
} }
@Override
public L parent(ApplicationContext parent) {
this.parent = parent;
return self();
}
/** /**
* Add the specified auto-configuration classes. * Add the specified auto-configuration classes.
* @param autoConfigurations the auto-configuration classes to add * @param autoConfigurations the auto-configuration classes to add
@ -226,6 +234,9 @@ class AbstractContextLoader<T extends ConfigurableApplicationContext, L extends
private T configureApplicationContext() { private T configureApplicationContext() {
T context = AbstractContextLoader.this.contextSupplier.get(); T context = AbstractContextLoader.this.contextSupplier.get();
if (this.parent != null) {
context.setParent(this.parent);
}
if (this.classLoader != null) { if (this.classLoader != null) {
Assert.isInstanceOf(DefaultResourceLoader.class, context); Assert.isInstanceOf(DefaultResourceLoader.class, context);
((DefaultResourceLoader) context).setClassLoader(this.classLoader); ((DefaultResourceLoader) context).setClassLoader(this.classLoader);

@ -134,6 +134,16 @@ public interface ContextLoader {
*/ */
ContextLoader classLoader(ClassLoader classLoader); ContextLoader classLoader(ClassLoader classLoader);
/**
* Configure the
* {@link org.springframework.context.ConfigurableApplicationContext#setParent(ApplicationContext)
* parent} of the {@link ApplicationContext}.
*
* @param parent the parent
* @return this instance
*/
ContextLoader parent(ApplicationContext parent);
/** /**
* Create and refresh a new {@link ApplicationContext} based on the current state of * Create and refresh a new {@link ApplicationContext} based on the current state of
* this loader. The context is consumed by the specified {@code consumer} and closed * this loader. The context is consumed by the specified {@code consumer} and closed

Loading…
Cancel
Save