Add support to ContextLoader for configuring context's parent
Closes gh-9749
This commit is contained in:
@@ -62,6 +62,8 @@ class AbstractContextLoader<T extends ConfigurableApplicationContext, L extends
|
||||
|
||||
private ClassLoader classLoader;
|
||||
|
||||
private ApplicationContext parent;
|
||||
|
||||
protected AbstractContextLoader(Supplier<T> contextSupplier) {
|
||||
this.contextSupplier = contextSupplier;
|
||||
}
|
||||
@@ -115,6 +117,12 @@ class AbstractContextLoader<T extends ConfigurableApplicationContext, L extends
|
||||
return self();
|
||||
}
|
||||
|
||||
@Override
|
||||
public L parent(ApplicationContext parent) {
|
||||
this.parent = parent;
|
||||
return self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the specified auto-configuration classes.
|
||||
* @param autoConfigurations the auto-configuration classes to add
|
||||
@@ -226,6 +234,9 @@ class AbstractContextLoader<T extends ConfigurableApplicationContext, L extends
|
||||
|
||||
private T configureApplicationContext() {
|
||||
T context = AbstractContextLoader.this.contextSupplier.get();
|
||||
if (this.parent != null) {
|
||||
context.setParent(this.parent);
|
||||
}
|
||||
if (this.classLoader != null) {
|
||||
Assert.isInstanceOf(DefaultResourceLoader.class, context);
|
||||
((DefaultResourceLoader) context).setClassLoader(this.classLoader);
|
||||
|
||||
@@ -134,6 +134,16 @@ public interface ContextLoader {
|
||||
*/
|
||||
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
|
||||
* this loader. The context is consumed by the specified {@code consumer} and closed
|
||||
|
||||
Reference in New Issue
Block a user