类 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 theContextLoaderSPI. Provides a Template Method based approach forprocessingresource locations.As of Spring 3.1,
AbstractContextLoaderalso provides a basis for all concrete implementations of theSmartContextLoaderSPI. For backwards compatibility with theContextLoaderSPI,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 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 StringgetResourceSuffix()Get the suffix to append toApplicationContextresource locations when detecting default locations.protected String[]getResourceSuffixes()Get the suffixes to append toApplicationContextresource locations when detecting default locations.protected booleanisGenerateDefaultLocations()Determine whether or not 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)For backwards compatibility with theContextLoaderSPI, the default implementation simply delegates toprocessLocations(Class, String...), passing it thedeclaring classandresource locationsretrieved from the suppliedconfiguration attributes.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.从类继承的方法 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 theContextLoaderSPI, the default implementation simply delegates toprocessLocations(Class, String...), passing it thedeclaring classandresource locationsretrieved from the suppliedconfiguration attributes. The processed locations are thensetin 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 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.
- 参数:
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 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.- 参数:
context- the newly created application contextmergedConfig- the merged context configuration- 从以下版本开始:
- 4.3
processLocations
public 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.- 指定者:
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 benullor 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
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.
- 参数:
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 thelocationsprovided toprocessLocations(Class, String...)arenullor 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
classespresent in theconfiguration attributessupplied toprocessContextConfiguration(ContextConfigurationAttributes)arenullor empty.Can be overridden by subclasses to change the default behavior.
- 返回:
- always
trueby default - 从以下版本开始:
- 2.5
getResourceSuffixes
protected String[] 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.- 返回:
- the resource suffixes; never
nullor empty - 从以下版本开始:
- 4.1
- 另请参阅:
generateDefaultLocations(Class)
getResourceSuffix
protected abstract String 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.- 返回:
- the resource suffix; never
nullor empty - 从以下版本开始:
- 2.5
- 另请参阅:
generateDefaultLocations(Class),getResourceSuffixes()