Class AbstractDelegatingSmartContextLoader
- All Implemented Interfaces:
AotContextLoader, ContextLoader, SmartContextLoader
- Direct Known Subclasses:
DelegatingSmartContextLoader, WebDelegatingSmartContextLoader
AbstractDelegatingSmartContextLoader serves as an abstract base class
for implementations of the SmartContextLoader SPI that delegate to a
set of candidate SmartContextLoaders (i.e., one that supports XML
configuration files or Groovy scripts and one that supports annotated classes)
to determine which context loader is appropriate for a given test class's
configuration. Each candidate is given a chance to
process the
ContextConfigurationAttributes for each class in the test class hierarchy
and enclosing class hierarchy that is annotated with
@ContextConfiguration, and the candidate that
supports the merged, processed configuration will be used to actually
load the context.
Any reference to an XML-based loader can be interpreted to mean a context loader that supports only XML configuration files or one that supports both XML configuration files and Groovy scripts simultaneously.
Placing an empty @ContextConfiguration annotation on a test class signals
that default resource locations (for example, XML configuration files or Groovy scripts)
or default
configuration classes
should be detected. Furthermore, if a specific ContextLoader or
SmartContextLoader is not explicitly declared via
@ContextConfiguration, a concrete subclass of
AbstractDelegatingSmartContextLoader will be used as the default loader,
thus providing automatic support for either path-based resource locations
(for example, XML configuration files and Groovy scripts) or annotated classes,
but not both simultaneously.
A test class may optionally declare neither path-based resource locations nor annotated classes and instead declare only application context initializers. In such cases, an attempt will still be made to detect defaults, but their absence will not result in an exception.
As of Spring Framework 6.0, this class implements AotContextLoader.
Consequently, the candidate getXmlLoader() and
getAnnotationConfigLoader() must also implement AotContextLoader
in order to provide AOT support.
- Since:
- 3.2
- Author:
- Sam Brannen, Phillip Webb
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract SmartContextLoaderGet the delegateSmartContextLoaderthat supports annotated classes.protected abstract SmartContextLoaderGet the delegateSmartContextLoaderthat supports XML configuration files and/or Groovy scripts.final ApplicationContextloadContext(MergedContextConfiguration mergedConfig) Delegates to an appropriate candidateSmartContextLoaderto load anApplicationContext.final ApplicationContextloadContextForAotProcessing(MergedContextConfiguration mergedConfig) Deprecated, for removal: This API element is subject to removal in a future version.final ApplicationContextloadContextForAotProcessing(MergedContextConfiguration mergedConfig, RuntimeHints runtimeHints) Delegates to an appropriate candidateSmartContextLoaderto load anApplicationContextfor AOT processing.final ApplicationContextloadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) Delegates to an appropriate candidateSmartContextLoaderto load anApplicationContextfor AOT run-time execution.voidprocessContextConfiguration(ContextConfigurationAttributes configAttributes) Delegates to candidateSmartContextLoadersto process the suppliedContextConfigurationAttributes.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface SmartContextLoader
loadContext, processLocations
-
Constructor Details
-
AbstractDelegatingSmartContextLoader
public AbstractDelegatingSmartContextLoader()
-
-
Method Details
-
getXmlLoader
Get the delegateSmartContextLoaderthat supports XML configuration files and/or Groovy scripts. -
getAnnotationConfigLoader
Get the delegateSmartContextLoaderthat supports annotated classes. -
processContextConfiguration
Delegates to candidateSmartContextLoadersto process the suppliedContextConfigurationAttributes.Delegation is based on explicit knowledge of the implementations of the default loaders for XML configuration files and Groovy scripts and annotated classes. Specifically, the delegation algorithm is as follows:
- If the resource locations or annotated classes in the supplied
ContextConfigurationAttributesare not empty, the appropriate candidate loader will be allowed to process the configuration as is, without any checks for detection of defaults. - Otherwise, the XML-based loader will be allowed to process
the configuration in order to detect default resource locations. If
the XML-based loader detects default resource locations,
an
infomessage will be logged. - Subsequently, the annotation-based loader will be allowed to
process the configuration in order to detect default configuration classes.
If the annotation-based loader detects default configuration
classes, an
infomessage will be logged.
- Specified by:
processContextConfigurationin interfaceSmartContextLoader- Parameters:
configAttributes- the context configuration attributes to process- Throws:
IllegalArgumentException- if the supplied configuration attributes arenull, or if the supplied configuration attributes include both resource locations and annotated classesIllegalStateException- if the XML-based loader detects default configuration classes; if the annotation-based loader detects default resource locations; if neither candidate loader detects defaults for the supplied context configuration; or if both candidate loaders detect defaults for the supplied context configuration
- If the resource locations or annotated classes in the supplied
-
loadContext
public final ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception Delegates to an appropriate candidateSmartContextLoaderto load anApplicationContext.Delegation is based on explicit knowledge of the implementations of the default loaders for XML configuration files and Groovy scripts and annotated classes. Specifically, the delegation algorithm is as follows:
- If the resource locations in the supplied
MergedContextConfigurationare not empty and the annotated classes are empty, the XML-based loader will load theApplicationContext. - If the annotated classes in the supplied
MergedContextConfigurationare not empty and the resource locations are empty, the annotation-based loader will load theApplicationContext.
- Specified by:
loadContextin interfaceSmartContextLoader- Parameters:
mergedConfig- the merged context configuration to use to load the application context- Returns:
- a new application context
- Throws:
IllegalStateException- if neither candidate loader is capable of loading anApplicationContextfrom the supplied merged context configurationException- See Also:
- If the resource locations in the supplied
-
loadContextForAotProcessing
@Deprecated(since="6.2.4", forRemoval=true) public final ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception Deprecated, for removal: This API element is subject to removal in a future version.as of Spring Framework 6.2.4, in favor ofloadContextForAotProcessing(MergedContextConfiguration, RuntimeHints); to be removed in Spring Framework 8.0Delegates to an appropriate candidateSmartContextLoaderto load anApplicationContextfor AOT processing.Delegation is based on explicit knowledge of the implementations of the default loaders. See
loadContext(MergedContextConfiguration)for details.- Specified by:
loadContextForAotProcessingin interfaceAotContextLoader- Parameters:
mergedConfig- the merged context configuration to use to load the application context- Returns:
- a new application context
- Throws:
IllegalArgumentException- if the supplied merged configuration isnullIllegalStateException- if neither candidate loader is capable of loading anApplicationContextfrom the supplied merged context configurationException- Since:
- 6.0
- See Also:
-
loadContextForAotProcessing
public final ApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig, RuntimeHints runtimeHints) throws Exception Delegates to an appropriate candidateSmartContextLoaderto load anApplicationContextfor AOT processing.Delegation is based on explicit knowledge of the implementations of the default loaders. See
loadContext(MergedContextConfiguration)for details.- Specified by:
loadContextForAotProcessingin interfaceAotContextLoader- Parameters:
mergedConfig- the merged context configuration to use to load the application contextruntimeHints- the runtime hints- Returns:
- a new application context
- Throws:
IllegalArgumentException- if the supplied merged configuration isnullIllegalStateException- if neither candidate loader is capable of loading anApplicationContextfrom the supplied merged context configurationException- Since:
- 6.2.4
- See Also:
-
loadContextForAotRuntime
public final ApplicationContext loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception Delegates to an appropriate candidateSmartContextLoaderto load anApplicationContextfor AOT run-time execution.Delegation is based on explicit knowledge of the implementations of the default loaders. See
loadContext(MergedContextConfiguration)for details.- Specified by:
loadContextForAotRuntimein interfaceAotContextLoader- Parameters:
mergedConfig- the merged context configuration to use to load the application contextinitializer- theApplicationContextInitializerthat should be applied to the context in order to recreate bean definitions- Returns:
- a new application context
- Throws:
IllegalStateException- if neither candidate loader is capable of loading anApplicationContextfrom the supplied merged context configurationException- Since:
- 6.0
- See Also:
-
loadContextForAotProcessing(MergedContextConfiguration, RuntimeHints); to be removed in Spring Framework 8.0