From a83529c844f9468223c634bac7ba22b4587e0925 Mon Sep 17 00:00:00 2001 From: Maciej Miklas Date: Mon, 17 Aug 2020 14:10:49 +0200 Subject: [PATCH] Introduce createContext() factory method in AbstractGenericContextLoader Prior to this commit it was possible to configure the DefaultListableBeanFactory used by the GenericApplicationContext created by AbstractGenericContextLoader, but it was not possible to completely replace the bean factory. This commit introduces a new createContext() factory method in AbstractGenericContextLoader which indirectly allows subclasses to supply a custom DefaultListableBeanFactory implementation to the GenericApplicationContext. Closes gh-25600 --- .../support/AbstractGenericContextLoader.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java index 8cefc456fb..5729a02c58 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java @@ -112,7 +112,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader validateMergedContextConfiguration(mergedConfig); - GenericApplicationContext context = new GenericApplicationContext(); + GenericApplicationContext context = createContext(); ApplicationContext parent = mergedConfig.getParentApplicationContext(); if (parent != null) { @@ -130,6 +130,15 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader return context; } + /** + * Creates instance of application context used by this {@code ContextLoader} + * + * @return new Instance of application context + */ + protected GenericApplicationContext createContext() { + return new GenericApplicationContext(); + } + /** * Validate the supplied {@link MergedContextConfiguration} with respect to * what this context loader supports. @@ -184,7 +193,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader logger.debug(String.format("Loading ApplicationContext for locations [%s].", StringUtils.arrayToCommaDelimitedString(locations))); } - GenericApplicationContext context = new GenericApplicationContext(); + GenericApplicationContext context = createContext(); prepareContext(context); customizeBeanFactory(context.getDefaultListableBeanFactory()); createBeanDefinitionReader(context).loadBeanDefinitions(locations);