类 AbstractGenericContextLoader
- java.lang.Object
- org.springframework.test.context.support.AbstractContextLoader
- org.springframework.test.context.support.AbstractGenericContextLoader
- 所有已实现的接口:
ContextLoader,SmartContextLoader
public abstract class AbstractGenericContextLoader extends AbstractContextLoader
Abstract, generic extension ofAbstractContextLoaderthat loads aGenericApplicationContext.- 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 overridingloadBeanDefinitions()as well.- 从以下版本开始:
- 2.5
- 作者:
- Sam Brannen, Juergen Hoeller, Phillip Webb
- 另请参阅:
loadContext(MergedContextConfiguration),loadContext(String...)
构造器概要
构造器 构造器 说明 AbstractGenericContextLoader()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 protected abstract BeanDefinitionReadercreateBeanDefinitionReader(GenericApplicationContext context)Factory method for creating a newBeanDefinitionReaderfor loading bean definitions into the suppliedcontext.protected voidcustomizeBeanFactory(DefaultListableBeanFactory beanFactory)Customize the internal bean factory of the ApplicationContext created by thisContextLoader.protected voidcustomizeContext(GenericApplicationContext context)Customize 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.ConfigurableApplicationContextloadContext(String... locations)Load a Spring ApplicationContext from the suppliedlocations.ConfigurableApplicationContextloadContext(MergedContextConfiguration mergedConfig)Load a Spring ApplicationContext from the suppliedMergedContextConfiguration.protected voidprepareContext(GenericApplicationContext context)Prepare theGenericApplicationContextcreated by thisContextLoader.protected voidvalidateMergedContextConfiguration(MergedContextConfiguration mergedConfig)Validate the suppliedMergedContextConfigurationwith respect to what this context loader supports.从类继承的方法 org.springframework.test.context.support.AbstractContextLoader
customizeContext, generateDefaultLocations, getResourceSuffix, getResourceSuffixes, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocations
构造器详细资料
AbstractGenericContextLoader
public AbstractGenericContextLoader()
方法详细资料
loadContext
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
Load a Spring ApplicationContext from the suppliedMergedContextConfiguration.Implementation details:
- Calls
validateMergedContextConfiguration(MergedContextConfiguration)to allow subclasses to validate the supplied configuration before proceeding. - Creates a
GenericApplicationContextinstance. - 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.
- 参数:
mergedConfig- the merged context configuration to use to load the application context- 返回:
- a new application context
- 抛出:
Exception- if context loading failed- 从以下版本开始:
- 3.1
- 另请参阅:
SmartContextLoader.loadContext(MergedContextConfiguration),GenericApplicationContext
- Calls
validateMergedContextConfiguration
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig)
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.
- 参数:
mergedConfig- the merged configuration to validate- 抛出:
IllegalStateException- if the supplied configuration is not valid for this context loader- 从以下版本开始:
- 4.0.4
loadContext
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception
Load a Spring ApplicationContext from the suppliedlocations.Implementation details:
- Creates a
GenericApplicationContextinstance. - 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.- 参数:
locations- the resource locations to use to load the application context- 返回:
- a new application context
- 抛出:
Exception- if context loading failed- 从以下版本开始:
- 2.5
- 另请参阅:
ContextLoader.loadContext(java.lang.String...),GenericApplicationContext,loadContext(MergedContextConfiguration)
- Creates a
prepareContext
protected void prepareContext(GenericApplicationContext context)
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.- 参数:
context- the context that should be prepared- 从以下版本开始:
- 2.5
- 另请参阅:
loadContext(MergedContextConfiguration),loadContext(String...),GenericApplicationContext.setAllowBeanDefinitionOverriding(boolean),GenericApplicationContext.setResourceLoader(org.springframework.core.io.ResourceLoader),AbstractApplicationContext.setId(java.lang.String),AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)
customizeBeanFactory
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory)
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.- 参数:
beanFactory- the bean factory created by thisContextLoader- 从以下版本开始:
- 2.5
- 另请参阅:
loadContext(MergedContextConfiguration),loadContext(String...),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.- 参数:
context- the context into which the bean definitions should be loadedmergedConfig- the merged context configuration- 从以下版本开始:
- 3.1
- 另请参阅:
loadContext(MergedContextConfiguration)
createBeanDefinitionReader
protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context)
Factory method for creating a newBeanDefinitionReaderfor loading bean definitions into the suppliedcontext.- 参数:
context- the context for which theBeanDefinitionReadershould be created- 返回:
- a
BeanDefinitionReaderfor the supplied context - 从以下版本开始:
- 2.5
- 另请参阅:
loadContext(String...),loadBeanDefinitions(org.springframework.context.support.GenericApplicationContext, org.springframework.test.context.MergedContextConfiguration),BeanDefinitionReader
customizeContext
protected void customizeContext(GenericApplicationContext context)
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.
- 参数:
context- the newly created application context- 从以下版本开始:
- 2.5
- 另请参阅:
loadContext(MergedContextConfiguration),loadContext(String...),AbstractContextLoader.customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)