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 ApplicationContextloadContext(String... locations) SmartContextLoaderdoes not support deprecatedContextLoadermethods.loadContext(MergedContextConfiguration mergedConfig) Load a newApplicationContextbased on the suppliedMergedContextConfiguration, configure the context, and return the context in a fully refreshed state.voidprocessContextConfiguration(ContextConfigurationAttributes configAttributes) Process theContextConfigurationAttributesfor a given test class.default String[]processLocations(Class<?> clazz, String... locations) SmartContextLoaderdoes not support deprecatedContextLoadermethods.
-
Method Details
-
processContextConfiguration
Process theContextConfigurationAttributesfor a given test class.Concrete implementations may choose to modify the
locationsorclassesin the suppliedContextConfigurationAttributes, generate default configuration locations, or detect default configuration classes if the supplied values arenullor empty.Note: a
SmartContextLoadermust preemptively verify that a generated or detected default actually exists before setting the correspondinglocationsorclassesproperty in the suppliedContextConfigurationAttributes. Consequently, leaving thelocationsorclassesproperty empty signals that thisSmartContextLoaderwas not able to generate or detect defaults.- Parameters:
configAttributes- the context configuration attributes to process
-
loadContext
Load a newApplicationContextbased 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 contextsloaded 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
ApplicationContextif appropriate (seeMergedContextConfiguration.getParent()). - Set the active bean definition profiles in the context's
Environment(seeMergedContextConfiguration.getActiveProfiles()). - Add test
PropertySourcesto 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
ApplicationContextshould 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:
- Set the parent
-
processLocations
SmartContextLoaderdoes not support deprecatedContextLoadermethods. CallprocessContextConfiguration(ContextConfigurationAttributes)instead.- Specified by:
processLocationsin 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 benullor empty)- Returns:
- an array of application context resource locations
- Throws:
UnsupportedOperationException- in this implementation- Since:
- 6.0
-
loadContext
SmartContextLoaderdoes not support deprecatedContextLoadermethods.Call
loadContext(MergedContextConfiguration)instead.- Specified by:
loadContextin 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
-