public abstract class AbstractContextLoader extends Object implements SmartContextLoader
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...)
.
generateDefaultLocations(java.lang.Class<?>)
,
getResourceSuffixes()
,
modifyLocations(java.lang.Class<?>, java.lang.String...)
Constructor and Description |
---|
AbstractContextLoader() |
Modifier and Type | Method and Description |
---|---|
protected String[] |
generateDefaultLocations(Class<?> clazz)
Generate the default classpath resource locations array based on the
supplied class.
|
protected abstract String |
getResourceSuffix()
Get the suffix to append to
ApplicationContext resource
locations when detecting default locations. |
protected String[] |
getResourceSuffixes()
Get the suffixes to append to
ApplicationContext resource
locations when detecting default locations. |
protected boolean |
isGenerateDefaultLocations()
Determine whether or not default resource locations should be
generated if the
locations provided to
processLocations(Class, String...) are null 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 the
ConfigurableApplicationContext created by this
SmartContextLoader before bean definitions are read. |
void |
processContextConfiguration(ContextConfigurationAttributes configAttributes)
For backwards compatibility with the
ContextLoader SPI, the
default implementation simply delegates to processLocations(Class, String...) ,
passing it the declaring class and resource locations retrieved from the supplied
configuration attributes . |
String[] |
processLocations(Class<?> clazz,
String... locations)
If the supplied
locations are null or empty
and isGenerateDefaultLocations() returns true ,
default locations will be generated (i.e., detected) for the specified class
and the configured resource suffixes;
otherwise, the supplied locations will be
modified if necessary and returned. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
loadContext
loadContext
public void processContextConfiguration(ContextConfigurationAttributes configAttributes)
ContextLoader
SPI, the
default implementation simply delegates to processLocations(Class, String...)
,
passing it the declaring class
and resource locations
retrieved from the supplied
configuration attributes
. The
processed locations are then
set
in
the supplied configuration attributes.
Can be overridden in subclasses — for example, to process annotated classes instead of resource locations.
processContextConfiguration
in interface SmartContextLoader
configAttributes
- the context configuration attributes to processprocessLocations(Class, String...)
protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig)
ConfigurableApplicationContext
created by this
SmartContextLoader
before bean definitions are read.
The default implementation:
MergedContextConfiguration
in the
Environment
of the
context.PropertySources
for all
resource locations and
inlined properties from the supplied MergedContextConfiguration
to the Environment
of the context.MergedContextConfiguration
and instantiates and
invokes each with the
given application context.context
- the newly created application contextmergedConfig
- the merged context configurationTestPropertySourceUtils.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)
public final String[] processLocations(Class<?> clazz, String... locations)
locations
are null
or empty
and isGenerateDefaultLocations()
returns true
,
default locations will be generated
(i.e., detected) for the specified class
and the configured resource suffixes;
otherwise, the supplied locations
will be
modified if necessary and returned.processLocations
in interface ContextLoader
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 be null
or empty)isGenerateDefaultLocations()
,
generateDefaultLocations(Class)
,
modifyLocations(Class, String...)
,
ContextLoader.processLocations(Class, String...)
,
processContextConfiguration(ContextConfigurationAttributes)
protected String[] generateDefaultLocations(Class<?> clazz)
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.
clazz
- the class for which the default locations are to be generatedgetResourceSuffixes()
protected String[] modifyLocations(Class<?> clazz, String... locations)
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.
clazz
- the class with which the locations are associatedlocations
- the resource locations to be modifiedprotected boolean isGenerateDefaultLocations()
locations
provided to
processLocations(Class, String...)
are null
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 or not default configuration classes should be
detected if the classes
present in the
configuration attributes
supplied
to processContextConfiguration(ContextConfigurationAttributes)
are null
or empty.
Can be overridden by subclasses to change the default behavior.
true
by defaultprotected abstract String getResourceSuffix()
ApplicationContext
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.
null
or emptygenerateDefaultLocations(Class)
,
getResourceSuffixes()
protected String[] getResourceSuffixes()
ApplicationContext
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.
null
or emptygenerateDefaultLocations(Class)