Introduce configurable refresh support in SmartContextLoader SPI
Prior to this commit, the contract of the loadContext() method in the
SmartContextLoader SPI required that the ApplicationContext be returned
in a fully refreshed state with a JVM shutdown hook registered for it.
However, in order to support AOT processing within the Spring
TestContext Framework (TCF), we need a way to signal to
SmartContextLoader implementations that they should load the test's
ApplicationContext without refreshing it or registering a JVM shutdown
hook.
To address this issue, this commit:
- Introduces a new loadContext(MergedContextConfiguration, boolean)
method. The boolean `refresh` flag controls whether the returned
ApplicationContext should be refreshed and have a JVM shutdown hook
registered for it.
- Deprecates the existing loadContext(MergedContextConfiguration)
method in favor of loadContext(MergedContextConfiguration, boolean).
- Removes all use of the deprecated method within the spring-test
module, excluding the exception mentioned below.
Note that loadContext(MergedContextConfiguration, boolean) is
implemented as an interface `default` method which delegates to the
deprecated loadContext(MergedContextConfiguration) method for backward
compatibility. When migrating a SmartContextLoader to Spring Framework
6.0, implementations that directly implement the SmartContextLoader SPI
(instead of extending AbstractGenericContextLoader or
AbstractGenericWebContextLoader) will need to override the new
loadContext(MergedContextConfiguration, boolean) method in order to
honor the `refresh` flag for AOT processing support. See the
implementation in AbstractGenericContextLoader for an example of how
this can be achieved.
Closes gh-28906
@ -96,7 +96,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
@@ -96,7 +96,7 @@ public class DefaultCacheAwareContextLoaderDelegate implements CacheAwareContext
@ -175,6 +175,18 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@@ -175,6 +175,18 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@ -191,12 +203,16 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@@ -191,12 +203,16 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@ -209,7 +225,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@@ -209,7 +225,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
// Determine if each loader can load a context from the mergedConfig. If it
@ -217,7 +233,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@@ -217,7 +233,7 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
// ACIs or customizers were declared, then delegate to the annotation config
@ -235,13 +251,14 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@@ -235,13 +251,14 @@ public abstract class AbstractDelegatingSmartContextLoader implements SmartConte
@ -94,15 +104,22 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@@ -94,15 +104,22 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
logger.debug(String.format("Loading ApplicationContext for merged context configuration [%s].",
mergedConfig));
@ -124,8 +141,10 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@@ -124,8 +141,10 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
customizeContext(context);
customizeContext(context,mergedConfig);
context.refresh();
context.registerShutdownHook();
if(refresh){
context.refresh();
context.registerShutdownHook();
}
returncontext;
}
@ -166,15 +185,15 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@@ -166,15 +185,15 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@ -219,7 +238,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@@ -219,7 +238,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@ -236,7 +255,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@@ -236,7 +255,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@ -261,7 +280,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@@ -261,7 +280,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@ -288,7 +307,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@@ -288,7 +307,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
@ -64,6 +64,18 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
@@ -64,6 +64,18 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
@ -94,12 +106,19 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
@@ -94,12 +106,19 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
()->String.format("Cannot load WebApplicationContext from non-web merged context configuration %s. "+
"Consider annotating your test class with @WebAppConfiguration.",mergedConfig));
@ -125,8 +144,12 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa
@@ -125,8 +144,12 @@ public abstract class AbstractGenericWebContextLoader extends AbstractContextLoa