类 AbstractContextLoader
- java.lang.Object
- org.springframework.test.context.support.AbstractContextLoader
- 所有已实现的接口:
ContextLoader
,SmartContextLoader
public abstract class AbstractContextLoader extends Object implements SmartContextLoader
Abstract application context loader that provides a basis for all concrete implementations of theContextLoader
SPI. Provides a Template Method based approach forprocessing
resource locations.As of Spring 3.1,
AbstractContextLoader
also provides a basis for all concrete implementations of theSmartContextLoader
SPI. For backwards compatibility with theContextLoader
SPI,processContextConfiguration(ContextConfigurationAttributes)
delegates toprocessLocations(Class, String...)
.- 从以下版本开始:
- 2.5
- 作者:
- Sam Brannen, Juergen Hoeller, Phillip Webb
- 另请参阅:
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)
构造器概要
构造器 构造器 说明 AbstractContextLoader()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 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.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 toApplicationContext
resource locations when detecting default locations.protected String[]
getResourceSuffixes()
Get the suffixes to append toApplicationContext
resource locations when detecting default locations.protected boolean
isGenerateDefaultLocations()
Determine whether or not 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)
For backwards compatibility with theContextLoader
SPI, the default implementation simply delegates toprocessLocations(Class, String...)
, passing it thedeclaring class
andresource locations
retrieved from the suppliedconfiguration attributes
.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.从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.test.context.ContextLoader
loadContext
从接口继承的方法 org.springframework.test.context.SmartContextLoader
loadContext
构造器详细资料
AbstractContextLoader
public AbstractContextLoader()
方法详细资料
processContextConfiguration
public void processContextConfiguration(ContextConfigurationAttributes configAttributes)
For backwards compatibility with theContextLoader
SPI, the default implementation simply delegates toprocessLocations(Class, String...)
, passing it thedeclaring class
andresource locations
retrieved from the suppliedconfiguration attributes
. 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.
- 指定者:
processContextConfiguration
在接口中SmartContextLoader
- 参数:
configAttributes
- the context configuration attributes to process- 从以下版本开始:
- 3.1
- 另请参阅:
processLocations(Class, String...)
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.
- 参数:
context
- the newly created application contextmergedConfig
- the merged context configuration- 从以下版本开始:
- 3.2
- 另请参阅:
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
.- 参数:
context
- the newly created application contextmergedConfig
- the merged context configuration- 从以下版本开始:
- 4.3
processLocations
public 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.- 指定者:
processLocations
在接口中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 benull
or empty)- 返回:
- a processed array of application context resource locations
- 从以下版本开始:
- 2.5
- 另请参阅:
isGenerateDefaultLocations()
,generateDefaultLocations(Class)
,modifyLocations(Class, String...)
,ContextLoader.processLocations(Class, String...)
,processContextConfiguration(ContextConfigurationAttributes)
generateDefaultLocations
protected String[] generateDefaultLocations(Class<?> clazz)
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.
- 参数:
clazz
- the class for which the default locations are to be generated- 返回:
- an array of default application context resource locations
- 从以下版本开始:
- 2.5
- 另请参阅:
getResourceSuffixes()
modifyLocations
protected String[] modifyLocations(Class<?> clazz, String... locations)
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.
- 参数:
clazz
- the class with which the locations are associatedlocations
- the resource locations to be modified- 返回:
- an array of modified application context resource locations
- 从以下版本开始:
- 2.5
isGenerateDefaultLocations
protected boolean isGenerateDefaultLocations()
Determine whether or not 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 or not 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.
- 返回:
- always
true
by default - 从以下版本开始:
- 2.5
getResourceSuffixes
protected String[] 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.- 返回:
- the resource suffixes; never
null
or empty - 从以下版本开始:
- 4.1
- 另请参阅:
generateDefaultLocations(Class)
getResourceSuffix
protected abstract String 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.- 返回:
- the resource suffix; never
null
or empty - 从以下版本开始:
- 2.5
- 另请参阅:
generateDefaultLocations(Class)
,getResourceSuffixes()