Class AnnotationConfigContextLoader
- java.lang.Object
- org.springframework.test.context.support.AbstractContextLoader
- org.springframework.test.context.support.AbstractGenericContextLoader
- org.springframework.test.context.support.AnnotationConfigContextLoader
- All Implemented Interfaces:
ContextLoader
,SmartContextLoader
public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
Concrete implementation ofAbstractGenericContextLoader
that loads bean definitions from annotated classes.See the Javadoc for
@ContextConfiguration
for a definition of annotated class.Note:
AnnotationConfigContextLoader
supports annotated classes rather than the String-based resource locations defined by the legacyContextLoader
API. Thus, althoughAnnotationConfigContextLoader
extendsAbstractGenericContextLoader
,AnnotationConfigContextLoader
does not support any String-based methods defined byAbstractContextLoader
orAbstractGenericContextLoader
. Consequently,AnnotationConfigContextLoader
should chiefly be considered aSmartContextLoader
rather than aContextLoader
.
Constructor Summary
Constructors Constructor Description AnnotationConfigContextLoader()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected BeanDefinitionReader
createBeanDefinitionReader(GenericApplicationContext context)
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
.protected Class<?>[]
detectDefaultConfigurationClasses(Class<?> declaringClass)
Detect the default configuration classes for the supplied test class.protected String[]
generateDefaultLocations(Class<?> clazz)
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
.protected String
getResourceSuffix()
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
.protected void
loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig)
Register classes in the suppliedcontext
from the classes in the suppliedMergedContextConfiguration
.protected String[]
modifyLocations(Class<?> clazz, String... locations)
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
.void
processContextConfiguration(ContextConfigurationAttributes configAttributes)
Process annotated classes in the suppliedContextConfigurationAttributes
.protected void
validateMergedContextConfiguration(MergedContextConfiguration mergedConfig)
Ensure that the suppliedMergedContextConfiguration
does not containlocations
.Methods inherited from class org.springframework.test.context.support.AbstractGenericContextLoader
customizeBeanFactory, customizeContext, loadContext, loadContext, prepareContext
Methods inherited from class org.springframework.test.context.support.AbstractContextLoader
customizeContext, getResourceSuffixes, isGenerateDefaultLocations, prepareContext, processLocations
Constructor Detail
AnnotationConfigContextLoader
public AnnotationConfigContextLoader()
Method Detail
processContextConfiguration
public void processContextConfiguration(ContextConfigurationAttributes configAttributes)
Process annotated classes in the suppliedContextConfigurationAttributes
.If the annotated classes are
null
or empty andAbstractContextLoader.isGenerateDefaultLocations()
returnstrue
, thisSmartContextLoader
will attempt todetect default configuration classes
. If defaults are detected they will beset
in the supplied configuration attributes. Otherwise, properties in the supplied configuration attributes will not be modified.- Specified by:
processContextConfiguration
in interfaceSmartContextLoader
- Overrides:
processContextConfiguration
in classAbstractContextLoader
- Parameters:
configAttributes
- the context configuration attributes to process- See Also:
SmartContextLoader.processContextConfiguration(ContextConfigurationAttributes)
,AbstractContextLoader.isGenerateDefaultLocations()
,detectDefaultConfigurationClasses(Class)
detectDefaultConfigurationClasses
protected Class<?>[] detectDefaultConfigurationClasses(Class<?> declaringClass)
Detect the default configuration classes for the supplied test class.The default implementation simply delegates to
AnnotationConfigContextLoaderUtils.detectDefaultConfigurationClasses(Class)
.- Parameters:
declaringClass
- the test class that declared@ContextConfiguration
- Returns:
- an array of default configuration classes, potentially empty but never
null
- See Also:
AnnotationConfigContextLoaderUtils
modifyLocations
protected String[] modifyLocations(Class<?> clazz, String... locations)
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
. Consequently, this method is not supported.- Overrides:
modifyLocations
in classAbstractContextLoader
- 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
- Throws:
UnsupportedOperationException
- in this implementation- See Also:
AbstractContextLoader.modifyLocations(java.lang.Class<?>, java.lang.String...)
generateDefaultLocations
protected String[] generateDefaultLocations(Class<?> clazz)
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
. Consequently, this method is not supported.- Overrides:
generateDefaultLocations
in classAbstractContextLoader
- Parameters:
clazz
- the class for which the default locations are to be generated- Returns:
- an array of default application context resource locations
- Throws:
UnsupportedOperationException
- in this implementation- See Also:
AbstractContextLoader.generateDefaultLocations(java.lang.Class<?>)
getResourceSuffix
protected String getResourceSuffix()
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
. Consequently, this method is not supported.- Specified by:
getResourceSuffix
in classAbstractContextLoader
- Returns:
- the resource suffix; never
null
or empty - Throws:
UnsupportedOperationException
- in this implementation- See Also:
AbstractContextLoader.getResourceSuffix()
validateMergedContextConfiguration
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig)
Ensure that the suppliedMergedContextConfiguration
does not containlocations
.- Overrides:
validateMergedContextConfiguration
in classAbstractGenericContextLoader
- Parameters:
mergedConfig
- the merged configuration to validate- Since:
- 4.0.4
- See Also:
AbstractGenericContextLoader.validateMergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration)
loadBeanDefinitions
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig)
Register classes in the suppliedcontext
from the classes in the suppliedMergedContextConfiguration
.Each class must represent an annotated class. An
AnnotatedBeanDefinitionReader
is used to register the appropriate bean definitions.Note that this method does not call
createBeanDefinitionReader(org.springframework.context.support.GenericApplicationContext)
sinceAnnotatedBeanDefinitionReader
is not an instance ofBeanDefinitionReader
.- Overrides:
loadBeanDefinitions
in classAbstractGenericContextLoader
- Parameters:
context
- the context in which the annotated classes should be registeredmergedConfig
- the merged configuration from which the classes should be retrieved- See Also:
AbstractGenericContextLoader.loadBeanDefinitions(org.springframework.context.support.GenericApplicationContext, org.springframework.test.context.MergedContextConfiguration)
createBeanDefinitionReader
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context)
AnnotationConfigContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
. Consequently, this method is not supported.- Specified by:
createBeanDefinitionReader
in classAbstractGenericContextLoader
- Parameters:
context
- the context for which theBeanDefinitionReader
should be created- Returns:
- a
BeanDefinitionReader
for the supplied context - Throws:
UnsupportedOperationException
- in this implementation- See Also:
loadBeanDefinitions(org.springframework.context.support.GenericApplicationContext, org.springframework.test.context.MergedContextConfiguration)
,AbstractGenericContextLoader.createBeanDefinitionReader(org.springframework.context.support.GenericApplicationContext)