Class AbstractContextLoader
- All Implemented Interfaces:
ContextLoader
,SmartContextLoader
- Direct Known Subclasses:
AbstractGenericContextLoader
,AbstractGenericWebContextLoader
ContextLoader
SPI. Provides a
Template Method based approach for processing
resource locations.
As of Spring 3.1, 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:
-
generateDefaultLocations(java.lang.Class<?>)
getResourceSuffixes()
modifyLocations(java.lang.Class<?>, java.lang.String...)
prepareContext(org.springframework.context.ConfigurableApplicationContext, org.springframework.test.context.MergedContextConfiguration)
customizeContext(org.springframework.context.ConfigurableApplicationContext, org.springframework.test.context.MergedContextConfiguration)
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
customizeContext
(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Customize theConfigurableApplicationContext
created by thisContextLoader
after 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 String
Get the suffix to append toApplicationContext
resource locations when detecting default locations.protected String[]
Get the suffixes to append toApplicationContext
resource locations when detecting default locations.protected boolean
Determine whether default resource locations should be generated if thelocations
provided toprocessLocations(Class, String...)
arenull
or empty.protected String[]
modifyLocations
(Class<?> clazz, String... locations) Generate a modified version of the supplied locations array and return it.protected void
prepareContext
(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Prepare theConfigurableApplicationContext
created by thisSmartContextLoader
before bean definitions are read.void
processContextConfiguration
(ContextConfigurationAttributes configAttributes) The default implementation processes locations analogous toprocessLocations(Class, String...)
, using thedeclaring class
as the test class and theresource locations
retrieved from the suppliedconfiguration attributes
as the locations to process.final String[]
processLocations
(Class<?> clazz, String... locations) If the suppliedlocations
arenull
or empty andisGenerateDefaultLocations()
returnstrue
, default locations will begenerated
(i.e., detected) for the specifiedclass
and the configured resource suffixes; otherwise, the suppliedlocations
will be modified if necessary and returned.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.test.context.SmartContextLoader
loadContext, loadContext
-
Constructor Details
-
AbstractContextLoader
public AbstractContextLoader()
-
-
Method Details
-
processContextConfiguration
The default implementation processes locations analogous toprocessLocations(Class, String...)
, using thedeclaring class
as the test class and theresource locations
retrieved from the suppliedconfiguration attributes
as the locations to process. The processed locations are thenset
in the supplied configuration attributes.Can be overridden in subclasses — for example, to process annotated classes instead of resource locations.
- Specified by:
processContextConfiguration
in interfaceSmartContextLoader
- Parameters:
configAttributes
- the context configuration attributes to process- Since:
- 3.1
- See Also:
-
prepareContext
protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Prepare theConfigurableApplicationContext
created by thisSmartContextLoader
before bean definitions are read.The default implementation:
- Sets the active bean definition profiles from the supplied
MergedContextConfiguration
in theEnvironment
of the context. - Adds
PropertySources
for all resource locations and inlined properties from the suppliedMergedContextConfiguration
to theEnvironment
of the context. - Determines what (if any) context initializer classes have been supplied
via the
MergedContextConfiguration
and 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:
-
TestPropertySourceUtils.addPropertiesFilesToEnvironment(org.springframework.context.ConfigurableApplicationContext, java.lang.String...)
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(org.springframework.context.ConfigurableApplicationContext, java.lang.String...)
ApplicationContextInitializer.initialize(ConfigurableApplicationContext)
SmartContextLoader.loadContext(MergedContextConfiguration)
ConfigurableApplicationContext.setId(java.lang.String)
- Sets the active bean definition profiles from the supplied
-
customizeContext
protected void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) Customize theConfigurableApplicationContext
created by thisContextLoader
after bean definitions have been loaded into the context but before the context has been refreshed.The default implementation delegates to all
context customizers
that have been registered with the suppliedmergedConfig
.- Parameters:
context
- the newly created application contextmergedConfig
- the merged context configuration- Since:
- 4.3
-
processLocations
If the suppliedlocations
arenull
or empty andisGenerateDefaultLocations()
returnstrue
, default locations will begenerated
(i.e., detected) for the specifiedclass
and the configured resource suffixes; otherwise, the suppliedlocations
will be modified if necessary and returned.- Specified by:
processLocations
in interfaceContextLoader
- Specified by:
processLocations
in 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 benull
or 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.As of Spring 3.1, the implementation of this method adheres to the contract defined in the
SmartContextLoader
SPI. 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(java.lang.Class<?>, java.lang.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 thelocations
provided toprocessLocations(Class, String...)
arenull
or empty.As of Spring 3.1, 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
classes
present in theconfiguration attributes
supplied toprocessContextConfiguration(ContextConfigurationAttributes)
arenull
or empty.Can be overridden by subclasses to change the default behavior.
- Returns:
- always
true
by default - Since:
- 2.5
-
getResourceSuffixes
Get the suffixes to append toApplicationContext
resource 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
null
or empty - Since:
- 4.1
- See Also:
-
getResourceSuffix
Get the suffix to append toApplicationContext
resource 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
null
or empty - Since:
- 2.5
- See Also:
-