Skip to content

Commit 711820e

Browse files
committed
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
1 parent 69d87f1 commit 711820e

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

spring-test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,10 @@ public final ConfigurableApplicationContext loadContext(String... locations) thr
200200
* Factory method for creating the {@link GenericApplicationContext} used by
201201
* this {@code ContextLoader}.
202202
* <p>The default implementation creates a {@code GenericApplicationContext}
203-
* using the default constructor. This method may get overridden e.g. to use
204-
* a custom context subclass or to create a {@code GenericApplicationContext}
205-
* with a custom {@link DefaultListableBeanFactory} implementation.
203+
* using the default constructor. This method may be overridden &mdash; for
204+
* example, to use a custom context subclass or to create a
205+
* {@code GenericApplicationContext} with a custom
206+
* {@link DefaultListableBeanFactory} implementation.
206207
* @return a newly instantiated {@code GenericApplicationContext}
207208
* @since 5.2.9
208209
*/

spring-test/src/main/java/org/springframework/test/context/web/AbstractGenericWebContextLoader.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -114,7 +114,7 @@ public final ConfigurableApplicationContext loadContext(MergedContextConfigurati
114114

115115
validateMergedContextConfiguration(webMergedConfig);
116116

117-
GenericWebApplicationContext context = new GenericWebApplicationContext();
117+
GenericWebApplicationContext context = createContext();
118118

119119
ApplicationContext parent = mergedConfig.getParentApplicationContext();
120120
if (parent != null) {
@@ -145,6 +145,21 @@ protected void validateMergedContextConfiguration(WebMergedContextConfiguration
145145
// no-op
146146
}
147147

148+
/**
149+
* Factory method for creating the {@link GenericWebApplicationContext} used
150+
* by this {@code ContextLoader}.
151+
* <p>The default implementation creates a {@code GenericWebApplicationContext}
152+
* using the default constructor. This method may be overridden &mdash; for
153+
* example, to use a custom context subclass or to create a
154+
* {@code GenericWebApplicationContext} with a custom
155+
* {@link DefaultListableBeanFactory} implementation.
156+
* @return a newly instantiated {@code GenericWebApplicationContext}
157+
* @since 5.2.23
158+
*/
159+
protected GenericWebApplicationContext createContext() {
160+
return new GenericWebApplicationContext();
161+
}
162+
148163
/**
149164
* Configures web resources for the supplied web application context (WAC).
150165
* <h4>Implementation Details</h4>

0 commit comments

Comments
 (0)