Class AbstractContextLoader
- All Implemented Interfaces:
ContextLoader, SmartContextLoader
- Direct Known Subclasses:
AbstractGenericContextLoader, AbstractGenericWebContextLoader
ContextLoader SPI. Provides a
Template Method based approach for processing
resource locations.
AbstractContextLoader also provides a basis for all concrete implementations
of the SmartContextLoader SPI. For backwards compatibility with the
ContextLoader SPI,
processContextConfiguration(ContextConfigurationAttributes) delegates
to processLocations(Class, String...).
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller, Phillip Webb
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcustomizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Customize theConfigurableApplicationContextcreated by thisContextLoaderafter bean definitions have been loaded into the context but before the context has been refreshed.protected String[]generateDefaultLocations(Class<?> clazz) Generate the default classpath resource locations array based on the supplied class.protected abstract StringGet the suffix to append toApplicationContextresource locations when detecting default locations.protected String[]Get the suffixes to append toApplicationContextresource locations when detecting default locations.protected booleanDetermine whether default resource locations should be generated if thelocationsprovided toprocessLocations(Class, String...)arenullor empty.protected String[]modifyLocations(Class<?> clazz, String... locations) Generate a modified version of the supplied locations array and return it.protected voidprepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Prepare theConfigurableApplicationContextcreated by thisSmartContextLoaderbefore bean definitions are read.voidprocessContextConfiguration(ContextConfigurationAttributes configAttributes) The default implementation processes locations analogous toprocessLocations(Class, String...), using thedeclaring classas the test class and theresource locationsretrieved from the suppliedconfiguration attributesas the locations to process.final String[]processLocations(Class<?> clazz, String... locations) If the suppliedlocationsarenullor empty andisGenerateDefaultLocations()returnstrue, default locations will begenerated(i.e., detected) for the specifiedclassand the configured resource suffixes; otherwise, the suppliedlocationswill be modified if necessary and returned.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface SmartContextLoader
loadContext, loadContext
-
Constructor Details
-
AbstractContextLoader
public AbstractContextLoader()
-
-
Method Details
-
processContextConfiguration
The default implementation processes locations analogous toprocessLocations(Class, String...), using thedeclaring classas the test class and theresource locationsretrieved from the suppliedconfiguration attributesas the locations to process. The processed locations are thensetin the supplied configuration attributes.Can be overridden in subclasses — for example, to process annotated classes instead of resource locations.
- Specified by:
processContextConfigurationin interfaceSmartContextLoader- Parameters:
configAttributes- the context configuration attributes to process- Since:
- 3.1
- See Also:
-
prepareContext
protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Prepare theConfigurableApplicationContextcreated by thisSmartContextLoaderbefore bean definitions are read.The default implementation:
- Sets the active bean definition profiles from the supplied
MergedContextConfigurationin theEnvironmentof the context. - Adds
PropertySourcesfor all resource locations and inlined properties from the suppliedMergedContextConfigurationto theEnvironmentof the context. - Determines what (if any) context initializer classes have been supplied
via the
MergedContextConfigurationand instantiates and invokes each with the given application context.
- Parameters:
context- the newly created application contextmergedConfig- the merged context configuration- Since:
- 3.2
- See Also:
- Sets the active bean definition profiles from the supplied
-
customizeContext
protected void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Customize theConfigurableApplicationContextcreated by thisContextLoaderafter bean definitions have been loaded into the context but before the context has been refreshed.The default implementation delegates to all
context customizersthat have been registered with the suppliedmergedConfig.- Parameters:
context- the newly created application contextmergedConfig- the merged context configuration- Since:
- 4.3
-
processLocations
If the suppliedlocationsarenullor empty andisGenerateDefaultLocations()returnstrue, default locations will begenerated(i.e., detected) for the specifiedclassand the configured resource suffixes; otherwise, the suppliedlocationswill be modified if necessary and returned.- Specified by:
processLocationsin interfaceContextLoader- Specified by:
processLocationsin interfaceSmartContextLoader- Parameters:
clazz- the class with which the locations are associated: to be used when generating default locationslocations- the unmodified locations to use for loading the application context (can benullor empty)- Returns:
- a processed array of application context resource locations
- Since:
- 2.5
- See Also:
-
generateDefaultLocations
Generate the default classpath resource locations array based on the supplied class.For example, if the supplied class is
com.example.MyTest, the generated locations will contain a single string with a value of"classpath:com/example/MyTest<suffix>", where<suffix>is the value of the first configured resource suffix for which the generated location actually exists in the classpath.The implementation of this method adheres to the contract defined in the
SmartContextLoaderSPI. Specifically, this method will preemptively verify that the generated default location actually exists. If it does not exist, this method will log a warning and return an empty array.Subclasses can override this method to implement a different default location generation strategy.
- Parameters:
clazz- the class for which the default locations are to be generated- Returns:
- an array of default application context resource locations
- Since:
- 2.5
- See Also:
-
modifyLocations
Generate a modified version of the supplied locations array and return it.The default implementation simply delegates to
TestContextResourceUtils.convertToClasspathResourcePaths(Class, String...).Subclasses can override this method to implement a different location modification strategy.
- Parameters:
clazz- the class with which the locations are associatedlocations- the resource locations to be modified- Returns:
- an array of modified application context resource locations
- Since:
- 2.5
-
isGenerateDefaultLocations
protected boolean isGenerateDefaultLocations()Determine whether default resource locations should be generated if thelocationsprovided toprocessLocations(Class, String...)arenullor empty.The semantics of this method have been overloaded to include detection of either default resource locations or default configuration classes. Consequently, this method can also be used to determine whether default configuration classes should be detected if the
classespresent in the configuration attributes supplied toprocessContextConfiguration(ContextConfigurationAttributes)arenullor empty.Can be overridden by subclasses to change the default behavior.
- Returns:
- always
trueby default - Since:
- 2.5
-
getResourceSuffixes
Get the suffixes to append toApplicationContextresource locations when detecting default locations.The default implementation simply wraps the value returned by
getResourceSuffix()in a single-element array, but this can be overridden by subclasses in order to support multiple suffixes.- Returns:
- the resource suffixes; never
nullor empty - Since:
- 4.1
- See Also:
-
getResourceSuffix
Get the suffix to append toApplicationContextresource locations when detecting default locations.Subclasses must provide an implementation of this method that returns a single suffix. Alternatively subclasses may provide a no-op implementation of this method and override
getResourceSuffixes()in order to provide multiple custom suffixes.- Returns:
- the resource suffix; never
nullor empty - Since:
- 2.5
- See Also:
-