From 812e9229a5d80493308c1b68321960ad749f2955 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 18 Aug 2022 17:37:55 +0200 Subject: [PATCH] Introduce createContext() factory method in AbstractWebGenericContextLoader Prior to this commit it was possible to configure the DefaultListableBeanFactory used by the GenericWebApplicationContext created by AbstractWebGenericContextLoader, but it was not possible to completely replace the bean factory. This commit introduces a new createContext() factory method in AbstractWebGenericContextLoader which indirectly allows subclasses to supply a custom DefaultListableBeanFactory implementation to the GenericWebApplicationContext. See gh-25600 Closes gh-28983 --- .../support/AbstractGenericContextLoader.java | 7 ++++--- .../web/AbstractGenericWebContextLoader.java | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 4 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 805d55eb3683..138827a82c25 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 @@ -241,9 +241,10 @@ public final ConfigurableApplicationContext loadContext(String... locations) thr * Factory method for creating the {@link GenericApplicationContext} used by * this {@code ContextLoader}. *

The default implementation creates a {@code GenericApplicationContext} - * using the default constructor. This method may get overridden e.g. to use - * a custom context subclass or to create a {@code GenericApplicationContext} - * with a custom {@link DefaultListableBeanFactory} implementation. + * using the default constructor. This method may be overridden — for + * example, to use a custom context subclass or to create a + * {@code GenericApplicationContext} with a custom + * {@link DefaultListableBeanFactory} implementation. * @return a newly instantiated {@code GenericApplicationContext} * @since 5.2.9 */ diff --git a/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java index 0b4e7db17083..0c4aec7ac555 100644 --- a/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java @@ -157,7 +157,7 @@ private final GenericWebApplicationContext loadContext( validateMergedContextConfiguration(webMergedConfig); - GenericWebApplicationContext context = new GenericWebApplicationContext(); + GenericWebApplicationContext context = createContext(); ApplicationContext parent = mergedConfig.getParentApplicationContext(); if (parent != null) { @@ -192,6 +192,21 @@ protected void validateMergedContextConfiguration(WebMergedContextConfiguration // no-op } + /** + * Factory method for creating the {@link GenericWebApplicationContext} used + * by this {@code ContextLoader}. + *

The default implementation creates a {@code GenericWebApplicationContext} + * using the default constructor. This method may be overridden — for + * example, to use a custom context subclass or to create a + * {@code GenericWebApplicationContext} with a custom + * {@link DefaultListableBeanFactory} implementation. + * @return a newly instantiated {@code GenericWebApplicationContext} + * @since 6.0 + */ + protected GenericWebApplicationContext createContext() { + return new GenericWebApplicationContext(); + } + /** * Configures web resources for the supplied web application context (WAC). *

Implementation Details