Class AbstractGenericContextLoader
- All Implemented Interfaces:
AotContextLoader,ContextLoader,SmartContextLoader
- Direct Known Subclasses:
AnnotationConfigContextLoader,GenericXmlContextLoader
AbstractContextLoader that loads a
GenericApplicationContext.
- If instances of concrete subclasses are invoked via the
ContextLoaderSPI, the context will be loaded from the locations provided toloadContext(String...). - If instances of concrete subclasses are invoked via the
SmartContextLoaderSPI, the context will be loaded from theMergedContextConfigurationprovided toloadContext(MergedContextConfiguration). In such cases, aSmartContextLoaderwill decide whether to load the context from locations or annotated classes.
Concrete subclasses must provide an appropriate implementation of
createBeanDefinitionReader(),
potentially overriding loadBeanDefinitions()
as well.
- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller, Phillip Webb
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract BeanDefinitionReaderFactory method for creating a newBeanDefinitionReaderfor loading bean definitions into the suppliedcontext.protected GenericApplicationContextFactory method for creating theGenericApplicationContextused by thisContextLoader.protected voidcustomizeBeanFactory(DefaultListableBeanFactory beanFactory) Customize the internal bean factory of the ApplicationContext created by thisContextLoader.protected voidCustomize theGenericApplicationContextcreated by thisContextLoaderafter bean definitions have been loaded into the context but before the context is refreshed.protected voidloadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) Load bean definitions into the suppliedcontextfrom the locations or classes in the suppliedMergedContextConfiguration.loadContext(String... locations) Deprecated.final ApplicationContextloadContext(MergedContextConfiguration mergedConfig) Load aGenericApplicationContextfor the suppliedMergedContextConfiguration.loadContextForAotProcessing(MergedContextConfiguration mergedConfig) Load aGenericApplicationContextfor AOT build-time processing based on the suppliedMergedContextConfiguration.loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) Load aGenericApplicationContextfor AOT run-time execution based on the suppliedMergedContextConfigurationandApplicationContextInitializer.protected voidprepareContext(GenericApplicationContext context) Prepare theGenericApplicationContextcreated by thisContextLoader.protected voidvalidateMergedContextConfiguration(MergedContextConfiguration mergedConfig) Validate the suppliedMergedContextConfigurationwith respect to what this context loader supports.Methods inherited from class org.springframework.test.context.support.AbstractContextLoader
customizeContext, generateDefaultLocations, getResourceSuffix, getResourceSuffixes, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocationsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.test.context.SmartContextLoader
processContextConfiguration, processLocations
-
Field Details
-
logger
-
-
Constructor Details
-
AbstractGenericContextLoader
public AbstractGenericContextLoader()
-
-
Method Details
-
loadContext
public final ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception Load aGenericApplicationContextfor the suppliedMergedContextConfiguration.Implementation details:
- Calls
validateMergedContextConfiguration(MergedContextConfiguration)to allow subclasses to validate the supplied configuration before proceeding. - Calls
createContext()to create aGenericApplicationContextinstance. - If the supplied
MergedContextConfigurationreferences a parent configuration, the correspondingApplicationContextwill be retrieved and set as the parent for the context created by this method. - Calls
prepareContext(GenericApplicationContext)for backwards compatibility with theContextLoaderSPI. - Calls
AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)to allow for customizing the context before bean definitions are loaded. - Calls
customizeBeanFactory(DefaultListableBeanFactory)to allow for customizing the context'sDefaultListableBeanFactory. - Delegates to
loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)to populate the context from the locations or classes in the suppliedMergedContextConfiguration. - Delegates to
AnnotationConfigUtilsforregisteringannotation configuration processors. - Calls
customizeContext(GenericApplicationContext)to allow for customizing the context before it is refreshed. - Calls
AbstractContextLoader.customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)to allow for customizing the context before it is refreshed. Refreshesthe context and registers a JVM shutdown hook for it.
- Specified by:
loadContextin interfaceSmartContextLoader- 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:
- Calls
-
loadContextForAotProcessing
public final GenericApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception Load aGenericApplicationContextfor AOT build-time processing based on the suppliedMergedContextConfiguration.In contrast to
loadContext(MergedContextConfiguration), this method does not refresh theApplicationContextor register a JVM shutdown hook for it. Otherwise, this method implements behavior identical toloadContext(MergedContextConfiguration).- Specified by:
loadContextForAotProcessingin interfaceAotContextLoader- Parameters:
mergedConfig- the merged context configuration to use to load the application context- Returns:
- a new application context
- Throws:
Exception- if context loading failed- Since:
- 6.0
- See Also:
-
loadContextForAotRuntime
public final GenericApplicationContext loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception Load aGenericApplicationContextfor AOT run-time execution based on the suppliedMergedContextConfigurationandApplicationContextInitializer.- Specified by:
loadContextForAotRuntimein interfaceAotContextLoader- Parameters:
mergedConfig- the merged context configuration to use to load the application contextinitializer- theApplicationContextInitializerthat should be applied to the context in order to recreate bean definitions- Returns:
- a new application context
- Throws:
Exception- if context loading failed- Since:
- 6.0
- See Also:
-
validateMergedContextConfiguration
Validate the suppliedMergedContextConfigurationwith respect to what this context loader supports.The default implementation is a no-op but can be overridden by subclasses as appropriate.
- Parameters:
mergedConfig- the merged configuration to validate- Throws:
IllegalStateException- if the supplied configuration is not valid for this context loader- Since:
- 4.0.4
-
loadContext
@Deprecated(since="6.0") public final ConfigurableApplicationContext loadContext(String... locations) throws Exception Deprecated.as of Spring Framework 6.0, in favor ofloadContext(MergedContextConfiguration)Load a Spring ApplicationContext from the suppliedlocations.Implementation details:
- Calls
createContext()to create aGenericApplicationContextinstance. - Calls
prepareContext(GenericApplicationContext)to allow for customizing the context before bean definitions are loaded. - Calls
customizeBeanFactory(DefaultListableBeanFactory)to allow for customizing the context'sDefaultListableBeanFactory. - Delegates to
createBeanDefinitionReader(GenericApplicationContext)to create aBeanDefinitionReaderwhich is then used to populate the context from the specified locations. - Delegates to
AnnotationConfigUtilsforregisteringannotation configuration processors. - Calls
customizeContext(GenericApplicationContext)to allow for customizing the context before it is refreshed. Refreshesthe context and registers a JVM shutdown hook for it.
Note: this method does not provide a means to set active bean definition profiles for the loaded context. See
loadContext(MergedContextConfiguration)andAbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)for an alternative.- Specified by:
loadContextin interfaceContextLoader- Specified by:
loadContextin interfaceSmartContextLoader- Parameters:
locations- the resource locations to use to load the application context- Returns:
- a new application context
- Throws:
Exception- if context loading failed- Since:
- 2.5
- See Also:
- Calls
-
createContext
Factory method for creating theGenericApplicationContextused by thisContextLoader.The default implementation creates a
GenericApplicationContextusing the default constructor. This method may be overridden — for example, to use a custom context subclass or to create aGenericApplicationContextwith a customDefaultListableBeanFactoryimplementation.- Returns:
- a newly instantiated
GenericApplicationContext - Since:
- 5.2.9
-
prepareContext
Prepare theGenericApplicationContextcreated by thisContextLoader. Called before bean definitions are read.The default implementation is empty. Can be overridden in subclasses to customize
GenericApplicationContext's standard settings.- Parameters:
context- the context that should be prepared- Since:
- 2.5
- See Also:
-
loadContext(MergedContextConfiguration)GenericApplicationContext.setAllowBeanDefinitionOverriding(boolean)GenericApplicationContext.setResourceLoader(org.springframework.core.io.ResourceLoader)AbstractApplicationContext.setId(java.lang.String)AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)
-
customizeBeanFactory
Customize the internal bean factory of the ApplicationContext created by thisContextLoader.The default implementation is empty but can be overridden in subclasses to customize
DefaultListableBeanFactory's standard settings.- Parameters:
beanFactory- the bean factory created by thisContextLoader- Since:
- 2.5
- See Also:
-
loadContext(MergedContextConfiguration)DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)DefaultListableBeanFactory.setAllowEagerClassLoading(boolean)AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(boolean)
-
loadBeanDefinitions
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) Load bean definitions into the suppliedcontextfrom the locations or classes in the suppliedMergedContextConfiguration.The default implementation delegates to the
BeanDefinitionReaderreturned bycreateBeanDefinitionReader(GenericApplicationContext)toloadthe bean definitions.Subclasses must provide an appropriate implementation of
createBeanDefinitionReader(GenericApplicationContext). Alternatively subclasses may provide a no-op implementation ofcreateBeanDefinitionReader()and override this method to provide a custom strategy for loading or registering bean definitions.- Parameters:
context- the context into which the bean definitions should be loadedmergedConfig- the merged context configuration- Since:
- 3.1
- See Also:
-
createBeanDefinitionReader
protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) Factory method for creating a newBeanDefinitionReaderfor loading bean definitions into the suppliedcontext.- Parameters:
context- the context for which theBeanDefinitionReadershould be created- Returns:
- a
BeanDefinitionReaderfor the supplied context - Since:
- 2.5
- See Also:
-
customizeContext
Customize theGenericApplicationContextcreated by thisContextLoaderafter bean definitions have been loaded into the context but before the context is refreshed.The default implementation is empty but can be overridden in subclasses to customize the application context.
- Parameters:
context- the newly created application context- Since:
- 2.5
- See Also:
-
loadContext(MergedContextConfiguration)