类 AbstractDelegatingSmartContextLoader
- java.lang.Object
- org.springframework.test.context.support.AbstractDelegatingSmartContextLoader
- 所有已实现的接口:
ContextLoader
,SmartContextLoader
public abstract class AbstractDelegatingSmartContextLoader extends Object implements SmartContextLoader
AbstractDelegatingSmartContextLoader
serves as an abstract base class for implementations of theSmartContextLoader
SPI that delegate to a set of candidate SmartContextLoaders (i.e., one that supports XML configuration files or Groovy scripts and one that supports annotated classes) to determine which context loader is appropriate for a given test class's configuration. Each candidate is given a chance to process theContextConfigurationAttributes
for each class in the test class hierarchy that is annotated with@ContextConfiguration
, and the candidate that supports the merged, processed configuration will be used to actually load the context.Any reference to an XML-based loader can be interpreted to mean a context loader that supports only XML configuration files or one that supports both XML configuration files and Groovy scripts simultaneously.
Placing an empty
@ContextConfiguration
annotation on a test class signals that default resource locations (e.g., XML configuration files or Groovy scripts) or default configuration classes should be detected. Furthermore, if a specificContextLoader
orSmartContextLoader
is not explicitly declared via@ContextConfiguration
, a concrete subclass ofAbstractDelegatingSmartContextLoader
will be used as the default loader, thus providing automatic support for either path-based resource locations (e.g., XML configuration files and Groovy scripts) or annotated classes, but not both simultaneously.As of Spring 3.2, a test class may optionally declare neither path-based resource locations nor annotated classes and instead declare only application context initializers. In such cases, an attempt will still be made to detect defaults, but their absence will not result in an exception.
- 从以下版本开始:
- 3.2
- 作者:
- Sam Brannen, Phillip Webb
- 另请参阅:
SmartContextLoader
构造器概要
构造器 构造器 说明 AbstractDelegatingSmartContextLoader()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 protected abstract SmartContextLoader
getAnnotationConfigLoader()
Get the delegateSmartContextLoader
that supports annotated classes.protected abstract SmartContextLoader
getXmlLoader()
Get the delegateSmartContextLoader
that supports XML configuration files and/or Groovy scripts.ApplicationContext
loadContext(String... locations)
AbstractDelegatingSmartContextLoader
does not support theContextLoader.loadContext(String...)
method.ApplicationContext
loadContext(MergedContextConfiguration mergedConfig)
Delegates to an appropriate candidateSmartContextLoader
to load anApplicationContext
.void
processContextConfiguration(ContextConfigurationAttributes configAttributes)
Delegates to candidateSmartContextLoaders
to process the suppliedContextConfigurationAttributes
.String[]
processLocations(Class<?> clazz, String... locations)
AbstractDelegatingSmartContextLoader
does not support theContextLoader.processLocations(Class, String...)
method.
构造器详细资料
AbstractDelegatingSmartContextLoader
public AbstractDelegatingSmartContextLoader()
方法详细资料
getXmlLoader
protected abstract SmartContextLoader getXmlLoader()
Get the delegateSmartContextLoader
that supports XML configuration files and/or Groovy scripts.
getAnnotationConfigLoader
protected abstract SmartContextLoader getAnnotationConfigLoader()
Get the delegateSmartContextLoader
that supports annotated classes.
processLocations
public final String[] processLocations(Class<?> clazz, String... locations)
AbstractDelegatingSmartContextLoader
does not support theContextLoader.processLocations(Class, String...)
method. CallprocessContextConfiguration(ContextConfigurationAttributes)
instead.- 指定者:
processLocations
在接口中ContextLoader
- 参数:
clazz
- the class with which the locations are associated: used to determine how to process the supplied locationslocations
- the unmodified locations to use for loading the application context (can benull
or empty)- 返回:
- an array of application context resource locations
- 抛出:
UnsupportedOperationException
- in this implementation
loadContext
public final ApplicationContext loadContext(String... locations) throws Exception
AbstractDelegatingSmartContextLoader
does not support theContextLoader.loadContext(String...)
method. CallloadContext(MergedContextConfiguration)
instead.- 指定者:
loadContext
在接口中ContextLoader
- 参数:
locations
- the resource locations to use to load the application context- 返回:
- a new application context
- 抛出:
UnsupportedOperationException
- in this implementationException
- if context loading failed
processContextConfiguration
public void processContextConfiguration(ContextConfigurationAttributes configAttributes)
Delegates to candidateSmartContextLoaders
to process the suppliedContextConfigurationAttributes
.Delegation is based on explicit knowledge of the implementations of the default loaders for XML configuration files and Groovy scripts and annotated classes. Specifically, the delegation algorithm is as follows:
- If the resource locations or annotated classes in the supplied
ContextConfigurationAttributes
are not empty, the appropriate candidate loader will be allowed to process the configuration as is, without any checks for detection of defaults. - Otherwise, the XML-based loader will be allowed to process the configuration in order to detect default resource locations. If the XML-based loader detects default resource locations, an
info
message will be logged. - Subsequently, the annotation-based loader will be allowed to process the configuration in order to detect default configuration classes. If the annotation-based loader detects default configuration classes, an
info
message will be logged.
- 指定者:
processContextConfiguration
在接口中SmartContextLoader
- 参数:
configAttributes
- the context configuration attributes to process- 抛出:
IllegalArgumentException
- if the supplied configuration attributes arenull
, or if the supplied configuration attributes include both resource locations and annotated classesIllegalStateException
- if the XML-based loader detects default configuration classes; if the annotation-based loader detects default resource locations; if neither candidate loader detects defaults for the supplied context configuration; or if both candidate loaders detect defaults for the supplied context configuration
- If the resource locations or annotated classes in the supplied
loadContext
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
Delegates to an appropriate candidateSmartContextLoader
to load anApplicationContext
.Delegation is based on explicit knowledge of the implementations of the default loaders for XML configuration files and Groovy scripts and annotated classes. Specifically, the delegation algorithm is as follows:
- If the resource locations in the supplied
MergedContextConfiguration
are not empty and the annotated classes are empty, the XML-based loader will load theApplicationContext
. - If the annotated classes in the supplied
MergedContextConfiguration
are not empty and the resource locations are empty, the annotation-based loader will load theApplicationContext
.
- 指定者:
loadContext
在接口中SmartContextLoader
- 参数:
mergedConfig
- the merged context configuration to use to load the application context- 返回:
- a new application context
- 抛出:
IllegalArgumentException
- if the supplied merged configuration isnull
IllegalStateException
- if neither candidate loader is capable of loading anApplicationContext
from the supplied merged context configurationException
- if context loading failed- 另请参阅:
SmartContextLoader.processContextConfiguration(ContextConfigurationAttributes)
,#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry)
,MergedContextConfiguration.getActiveProfiles()
,ConfigurableApplicationContext.getEnvironment()
- If the resource locations in the supplied