Interface SmartContextLoader
- All Superinterfaces:
ContextLoader
- All Known Subinterfaces:
AotContextLoader
- All Known Implementing Classes:
AbstractContextLoader
,AbstractDelegatingSmartContextLoader
,AbstractGenericContextLoader
,AbstractGenericWebContextLoader
,AnnotationConfigContextLoader
,AnnotationConfigWebContextLoader
,DelegatingSmartContextLoader
,GenericGroovyXmlContextLoader
,GenericGroovyXmlWebContextLoader
,GenericXmlContextLoader
,GenericXmlWebContextLoader
,WebDelegatingSmartContextLoader
ApplicationContext
for an integration
test managed by the Spring TestContext Framework.
The SmartContextLoader
SPI supersedes the ContextLoader
SPI
introduced in Spring 2.5: a SmartContextLoader
can choose to process
resource locations, component classes, or a combination of both. Furthermore, a
SmartContextLoader
can configure the context that it
loads based on any
properties available in the provided MergedContextConfiguration
.
For example, active bean definition profiles can be configured for the context
based on MergedContextConfiguration.getActiveProfiles()
.
See the Javadoc for @ContextConfiguration
for a definition of component classes.
Clients of a SmartContextLoader
should call
processContextConfiguration()
prior to calling
loadContext()
. This gives a
SmartContextLoader
the opportunity to provide custom support for
modifying resource locations or detecting default resource locations or
default configuration classes. The results of
processContextConfiguration()
should be merged for all classes in the
hierarchy of the root test class and then supplied to
loadContext()
.
NOTE: As of Spring Framework 6.0, SmartContextLoader
no longer
supports methods defined in the ContextLoader
SPI.
Concrete implementations must provide a public
no-args constructor.
Spring provides the following SmartContextLoader
implementations.
- Since:
- 3.1
- Author:
- Sam Brannen
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault ApplicationContext
loadContext
(String... locations) SmartContextLoader
does not support deprecatedContextLoader
methods.loadContext
(MergedContextConfiguration mergedConfig) Load a newApplicationContext
based on the suppliedMergedContextConfiguration
, configure the context, and return the context in a fully refreshed state.void
processContextConfiguration
(ContextConfigurationAttributes configAttributes) Process theContextConfigurationAttributes
for a given test class.default String[]
processLocations
(Class<?> clazz, String... locations) SmartContextLoader
does not support deprecatedContextLoader
methods.
-
Method Details
-
processContextConfiguration
Process theContextConfigurationAttributes
for a given test class.Concrete implementations may choose to modify the
locations
orclasses
in the suppliedContextConfigurationAttributes
, generate default configuration locations, or detect default configuration classes if the supplied values arenull
or empty.Note: a
SmartContextLoader
must preemptively verify that a generated or detected default actually exists before setting the correspondinglocations
orclasses
property in the suppliedContextConfigurationAttributes
. Consequently, leaving thelocations
orclasses
property empty signals that thisSmartContextLoader
was not able to generate or detect defaults.- Parameters:
configAttributes
- the context configuration attributes to process
-
loadContext
Load a newApplicationContext
based on the suppliedMergedContextConfiguration
, configure the context, and return the context in a fully refreshed state.Concrete implementations should register annotation configuration processors with bean factories of
application contexts
loaded by thisSmartContextLoader
. Beans will therefore automatically be candidates for annotation-based dependency injection using@Autowired
,@Resource
, and@Inject
. In addition, concrete implementations should perform the following actions.- Set the parent
ApplicationContext
if appropriate (seeMergedContextConfiguration.getParent()
). - Set the active bean definition profiles in the context's
Environment
(seeMergedContextConfiguration.getActiveProfiles()
). - Add test
PropertySources
to theEnvironment
(seeMergedContextConfiguration.getPropertySourceLocations()
,MergedContextConfiguration.getPropertySourceProperties()
, andTestPropertySourceUtils
). - Invoke
ApplicationContextInitializers
(seeMergedContextConfiguration.getContextInitializerClasses()
). - Invoke
ContextCustomizers
(seeMergedContextConfiguration.getContextCustomizers()
). - Register a JVM shutdown hook for the
ApplicationContext
. Unless the context gets closed early, all context instances will be automatically closed on JVM shutdown. This allows for freeing of external resources held by beans within the context — for example, temporary files.
As of Spring Framework 6.0, any exception thrown while attempting to load an
ApplicationContext
should be wrapped in aContextLoadException
. Concrete implementations should therefore contain a try-catch block similar to the following.ApplicationContext context = // create context try { // configure and refresh context } catch (Exception ex) { throw new ContextLoadException(context, ex); }
- Parameters:
mergedConfig
- the merged context configuration to use to load the application context- Returns:
- a new application context
- Throws:
ContextLoadException
- if context loading failedException
- See Also:
-
processContextConfiguration(ContextConfigurationAttributes)
#loadContextForAotProcessing(MergedContextConfiguration)
AnnotationConfigUtils.registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry)
ConfigurableApplicationContext.getEnvironment()
- Set the parent
-
processLocations
SmartContextLoader
does not support deprecatedContextLoader
methods. CallprocessContextConfiguration(ContextConfigurationAttributes)
instead.- Specified by:
processLocations
in interfaceContextLoader
- Parameters:
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)- Returns:
- an array of application context resource locations
- Throws:
UnsupportedOperationException
- in this implementation- Since:
- 6.0
-
loadContext
SmartContextLoader
does not support deprecatedContextLoader
methods.Call
loadContext(MergedContextConfiguration)
instead.- Specified by:
loadContext
in interfaceContextLoader
- Parameters:
locations
- the resource locations to use to load the application context- Returns:
- a new application context
- Throws:
UnsupportedOperationException
- in this implementationException
- if context loading failed- Since:
- 6.0
-