类 GenericApplicationContext
- java.lang.Object
- org.springframework.core.io.DefaultResourceLoader
- org.springframework.context.support.AbstractApplicationContext
- org.springframework.context.support.GenericApplicationContext
- 所有已实现的接口:
Closeable,AutoCloseable,BeanFactory,HierarchicalBeanFactory,ListableBeanFactory,BeanDefinitionRegistry,ApplicationContext,ApplicationEventPublisher,ConfigurableApplicationContext,Lifecycle,MessageSource,AliasRegistry,EnvironmentCapable,ResourceLoader,ResourcePatternResolver
- 直接已知子类:
AnnotationConfigApplicationContext,GenericGroovyApplicationContext,GenericWebApplicationContext,GenericXmlApplicationContext,ResourceAdapterApplicationContext,StaticApplicationContext
public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry
Generic ApplicationContext implementation that holds a single internalDefaultListableBeanFactoryinstance and does not assume a specific bean definition format. Implements theBeanDefinitionRegistryinterface in order to allow for applying any bean definition readers to it.Typical usage is to register a variety of bean definitions via the
BeanDefinitionRegistryinterface and then callAbstractApplicationContext.refresh()to initialize those beans with application context semantics (handlingApplicationContextAware, auto-detectingBeanFactoryPostProcessors, etc).In contrast to other ApplicationContext implementations that create a new internal BeanFactory instance for each refresh, the internal BeanFactory of this context is available right from the start, to be able to register bean definitions on it.
AbstractApplicationContext.refresh()may only be called once.Usage example:
GenericApplicationContext ctx = new GenericApplicationContext(); XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx); xmlReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml")); PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx); propReader.loadBeanDefinitions(new ClassPathResource("otherBeans.properties")); ctx.refresh(); MyBean myBean = (MyBean) ctx.getBean("myBean"); ...For the typical case of XML bean definitions, simply useClassPathXmlApplicationContextorFileSystemXmlApplicationContext, which are easier to set up - but less flexible, since you can just use standard resource locations for XML bean definitions, rather than mixing arbitrary bean definition formats. The equivalent in a web environment isXmlWebApplicationContext.For custom application context implementations that are supposed to read special bean definition formats in a refreshable manner, consider deriving from the
AbstractRefreshableApplicationContextbase class.- 从以下版本开始:
- 1.1.2
- 作者:
- Juergen Hoeller, Chris Beams
- 另请参阅:
registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition),AbstractApplicationContext.refresh(),XmlBeanDefinitionReader,PropertiesBeanDefinitionReader
嵌套类概要
从类继承的嵌套类/接口 org.springframework.core.io.DefaultResourceLoader
DefaultResourceLoader.ClassPathContextResource
字段概要
从类继承的字段 org.springframework.context.support.AbstractApplicationContext
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, LIFECYCLE_PROCESSOR_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME
从接口继承的字段 org.springframework.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX
从接口继承的字段 org.springframework.context.ConfigurableApplicationContext
CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, ENVIRONMENT_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SHUTDOWN_HOOK_THREAD_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME
从接口继承的字段 org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
从接口继承的字段 org.springframework.core.io.support.ResourcePatternResolver
CLASSPATH_ALL_URL_PREFIX
构造器概要
构造器 构造器 说明 GenericApplicationContext()Create a new GenericApplicationContext.GenericApplicationContext(DefaultListableBeanFactory beanFactory)Create a new GenericApplicationContext with the given DefaultListableBeanFactory.GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent)Create a new GenericApplicationContext with the given DefaultListableBeanFactory.GenericApplicationContext(ApplicationContext parent)Create a new GenericApplicationContext with the given parent.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected voidcancelRefresh(BeansException ex)Cancel this context's refresh attempt, resetting theactiveflag after an exception got thrown.protected voidcloseBeanFactory()Not much to do: We hold a single internal BeanFactory that will never get released.AutowireCapableBeanFactorygetAutowireCapableBeanFactory()Return this context's internal bean factory as AutowireCapableBeanFactory, if already available.BeanDefinitiongetBeanDefinition(String beanName)Return the BeanDefinition for the given bean name.ConfigurableListableBeanFactorygetBeanFactory()Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).ClassLoadergetClassLoader()Return the ClassLoader to load class path resources with.DefaultListableBeanFactorygetDefaultListableBeanFactory()Return the underlying bean factory of this context, available for registering bean definitions.ResourcegetResource(String location)This implementation delegates to this context's ResourceLoader if set, falling back to the default superclass behavior else.Resource[]getResources(String locationPattern)This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior else.booleanisAlias(String beanName)Determine whether the given name is defined as an alias (as opposed to the name of an actually registered component).booleanisBeanNameInUse(String beanName)Determine whether the given bean name is already in use within this registry, i.e. whether there is a local bean or alias registered under this name.protected voidrefreshBeanFactory()Do nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's).voidregisterAlias(String beanName, String alias)Given a name, register an alias for it.<T> voidregisterBean(Class<T> beanClass, Object... constructorArgs)Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process.<T> voidregisterBean(Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers)Register a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression).<T> voidregisterBean(Class<T> beanClass, BeanDefinitionCustomizer... customizers)Register a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression).<T> voidregisterBean(String beanName, Class<T> beanClass, Object... constructorArgs)Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process.<T> voidregisterBean(String beanName, Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers)Register a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression).<T> voidregisterBean(String beanName, Class<T> beanClass, BeanDefinitionCustomizer... customizers)Register a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression).voidregisterBeanDefinition(String beanName, BeanDefinition beanDefinition)Register a new bean definition with this registry.voidremoveAlias(String alias)Remove the specified alias from this registry.voidremoveBeanDefinition(String beanName)Remove the BeanDefinition for the given name.voidsetAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former.voidsetAllowCircularReferences(boolean allowCircularReferences)Set whether to allow circular references between beans - and automatically try to resolve them.voidsetClassLoader(ClassLoader classLoader)Specify the ClassLoader to load class path resources with, ornullfor using the thread context class loader at the time of actual resource access.voidsetParent(ApplicationContext parent)Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.voidsetResourceLoader(ResourceLoader resourceLoader)Set a ResourceLoader to use for this context.从类继承的方法 org.springframework.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, assertBeanFactoryActive, close, containsBean, containsBeanDefinition, containsLocalBean, createEnvironment, destroy, destroyBeans, doClose, findAnnotationOnBean, finishBeanFactoryInitialization, finishRefresh, getAliases, getApplicationListeners, getApplicationName, getBean, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanProvider, getBeanProvider, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getEnvironment, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getResourcePatternResolver, getStartupDate, getType, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, initPropertySources, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, isTypeMatch, obtainFreshBeanFactory, onClose, onRefresh, postProcessBeanFactory, prepareBeanFactory, prepareRefresh, publishEvent, publishEvent, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, resetCommonCaches, setDisplayName, setEnvironment, setId, start, stop, toString
从类继承的方法 org.springframework.core.io.DefaultResourceLoader
addProtocolResolver, clearResourceCaches, getProtocolResolvers, getResourceByPath, getResourceCache
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
从接口继承的方法 org.springframework.core.AliasRegistry
getAliases
从接口继承的方法 org.springframework.beans.factory.support.BeanDefinitionRegistry
containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames
从接口继承的方法 org.springframework.context.ConfigurableApplicationContext
addProtocolResolver
构造器详细资料
GenericApplicationContext
public GenericApplicationContext()
Create a new GenericApplicationContext.
GenericApplicationContext
public GenericApplicationContext(DefaultListableBeanFactory beanFactory)
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.- 参数:
beanFactory- the DefaultListableBeanFactory instance to use for this context- 另请参阅:
registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition),AbstractApplicationContext.refresh()
GenericApplicationContext
public GenericApplicationContext(@Nullable ApplicationContext parent)
Create a new GenericApplicationContext with the given parent.- 参数:
parent- the parent application context- 另请参阅:
registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition),AbstractApplicationContext.refresh()
GenericApplicationContext
public GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent)
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.- 参数:
beanFactory- the DefaultListableBeanFactory instance to use for this contextparent- the parent application context- 另请参阅:
registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition),AbstractApplicationContext.refresh()
方法详细资料
setParent
public void setParent(@Nullable ApplicationContext parent)
Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.- 指定者:
setParent在接口中ConfigurableApplicationContext- 覆盖:
setParent在类中AbstractApplicationContext- 参数:
parent- the parent context- 另请参阅:
ConfigurableBeanFactory.setParentBeanFactory(org.springframework.beans.factory.BeanFactory)
setAllowBeanDefinitionOverriding
public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding)
Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former. If not, an exception will be thrown. Default is "true".
setAllowCircularReferences
public void setAllowCircularReferences(boolean allowCircularReferences)
Set whether to allow circular references between beans - and automatically try to resolve them.Default is "true". Turn this off to throw an exception when encountering a circular reference, disallowing them completely.
setResourceLoader
public void setResourceLoader(ResourceLoader resourceLoader)
Set a ResourceLoader to use for this context. If set, the context will delegate allgetResourcecalls to the given ResourceLoader. If not set, default resource loading will apply.The main reason to specify a custom ResourceLoader is to resolve resource paths (without URL prefix) in a specific fashion. The default behavior is to resolve such paths as class path locations. To resolve resource paths as file system locations, specify a FileSystemResourceLoader here.
You can also pass in a full ResourcePatternResolver, which will be autodetected by the context and used for
getResourcescalls as well. Else, default resource pattern matching will apply.
getResource
public Resource getResource(String location)
This implementation delegates to this context's ResourceLoader if set, falling back to the default superclass behavior else.- 指定者:
getResource在接口中ResourceLoader- 覆盖:
getResource在类中DefaultResourceLoader- 参数:
location- the resource location- 返回:
- a corresponding Resource handle (never
null) - 另请参阅:
setResourceLoader(org.springframework.core.io.ResourceLoader)
getResources
public Resource[] getResources(String locationPattern) throws IOException
This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior else.- 指定者:
getResources在接口中ResourcePatternResolver- 覆盖:
getResources在类中AbstractApplicationContext- 参数:
locationPattern- the location pattern to resolve- 返回:
- the corresponding Resource objects
- 抛出:
IOException- in case of I/O errors- 另请参阅:
setResourceLoader(org.springframework.core.io.ResourceLoader)
setClassLoader
public void setClassLoader(@Nullable ClassLoader classLoader)
从类复制的说明:DefaultResourceLoaderSpecify the ClassLoader to load class path resources with, ornullfor using the thread context class loader at the time of actual resource access.The default is that ClassLoader access will happen using the thread context class loader at the time of this ResourceLoader's initialization.
getClassLoader
@Nullable public ClassLoader getClassLoader()
从类复制的说明:DefaultResourceLoaderReturn the ClassLoader to load class path resources with.Will get passed to ClassPathResource's constructor for all ClassPathResource objects created by this resource loader.
- 指定者:
getClassLoader在接口中ResourceLoader- 覆盖:
getClassLoader在类中DefaultResourceLoader- 返回:
- the ClassLoader (only
nullif even the system ClassLoader isn't accessible) - 另请参阅:
ClassPathResource
refreshBeanFactory
protected final void refreshBeanFactory() throws IllegalStateException
Do nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's).- 指定者:
refreshBeanFactory在类中AbstractApplicationContext- 抛出:
IllegalStateException- if already initialized and multiple refresh attempts are not supported- 另请参阅:
registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)
cancelRefresh
protected void cancelRefresh(BeansException ex)
从类复制的说明:AbstractApplicationContextCancel this context's refresh attempt, resetting theactiveflag after an exception got thrown.- 覆盖:
cancelRefresh在类中AbstractApplicationContext- 参数:
ex- the exception that led to the cancellation
closeBeanFactory
protected final void closeBeanFactory()
Not much to do: We hold a single internal BeanFactory that will never get released.
getBeanFactory
public final ConfigurableListableBeanFactory getBeanFactory()
Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).- 指定者:
getBeanFactory在接口中ConfigurableApplicationContext- 指定者:
getBeanFactory在类中AbstractApplicationContext- 返回:
- this application context's internal bean factory (never
null) - 另请参阅:
AbstractApplicationContext.refreshBeanFactory(),AbstractApplicationContext.closeBeanFactory()
getDefaultListableBeanFactory
public final DefaultListableBeanFactory getDefaultListableBeanFactory()
Return the underlying bean factory of this context, available for registering bean definitions.NOTE: You need to call
AbstractApplicationContext.refresh()to initialize the bean factory and its contained beans with application context semantics (autodetecting BeanFactoryPostProcessors, etc).- 返回:
- the internal bean factory (as DefaultListableBeanFactory)
getAutowireCapableBeanFactory
public AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException
从类复制的说明:AbstractApplicationContextReturn this context's internal bean factory as AutowireCapableBeanFactory, if already available.- 指定者:
getAutowireCapableBeanFactory在接口中ApplicationContext- 覆盖:
getAutowireCapableBeanFactory在类中AbstractApplicationContext- 返回:
- the AutowireCapableBeanFactory for this context
- 抛出:
IllegalStateException- if the context does not support theAutowireCapableBeanFactoryinterface, or does not hold an autowire-capable bean factory yet (e.g. ifrefresh()has never been called), or if the context has been closed already- 另请参阅:
AbstractApplicationContext.getBeanFactory()
registerBeanDefinition
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException
从接口复制的说明:BeanDefinitionRegistryRegister a new bean definition with this registry. Must support RootBeanDefinition and ChildBeanDefinition.- 指定者:
registerBeanDefinition在接口中BeanDefinitionRegistry- 参数:
beanName- the name of the bean instance to registerbeanDefinition- definition of the bean instance to register- 抛出:
BeanDefinitionStoreException- if the BeanDefinition is invalidBeanDefinitionOverrideException- if there is already a BeanDefinition for the specified bean name and we are not allowed to override it- 另请参阅:
GenericBeanDefinition,RootBeanDefinition,ChildBeanDefinition
removeBeanDefinition
public void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException
从接口复制的说明:BeanDefinitionRegistryRemove the BeanDefinition for the given name.- 指定者:
removeBeanDefinition在接口中BeanDefinitionRegistry- 参数:
beanName- the name of the bean instance to register- 抛出:
NoSuchBeanDefinitionException- if there is no such bean definition
getBeanDefinition
public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException
从接口复制的说明:BeanDefinitionRegistryReturn the BeanDefinition for the given bean name.- 指定者:
getBeanDefinition在接口中BeanDefinitionRegistry- 参数:
beanName- name of the bean to find a definition for- 返回:
- the BeanDefinition for the given name (never
null) - 抛出:
NoSuchBeanDefinitionException- if there is no such bean definition
isBeanNameInUse
public boolean isBeanNameInUse(String beanName)
从接口复制的说明:BeanDefinitionRegistryDetermine whether the given bean name is already in use within this registry, i.e. whether there is a local bean or alias registered under this name.- 指定者:
isBeanNameInUse在接口中BeanDefinitionRegistry- 参数:
beanName- the name to check- 返回:
- whether the given bean name is already in use
registerAlias
public void registerAlias(String beanName, String alias)
从接口复制的说明:AliasRegistryGiven a name, register an alias for it.- 指定者:
registerAlias在接口中AliasRegistry- 参数:
beanName- the canonical namealias- the alias to be registered
removeAlias
public void removeAlias(String alias)
从接口复制的说明:AliasRegistryRemove the specified alias from this registry.- 指定者:
removeAlias在接口中AliasRegistry- 参数:
alias- the alias to remove
isAlias
public boolean isAlias(String beanName)
从接口复制的说明:AliasRegistryDetermine whether the given name is defined as an alias (as opposed to the name of an actually registered component).- 指定者:
isAlias在接口中AliasRegistry- 参数:
beanName- the name to check- 返回:
- whether the given name is an alias
registerBean
public <T> void registerBean(Class<T> beanClass, Object... constructorArgs)
Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process.- 参数:
beanClass- the class of the beanconstructorArgs- custom argument values to be fed into Spring's constructor resolution algorithm, resolving either all arguments or just specific ones, with the rest to be resolved through regular autowiring (may benullor empty)- 从以下版本开始:
- 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
registerBean
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, Object... constructorArgs)
Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process.- 参数:
beanName- the name of the bean (may benull)beanClass- the class of the beanconstructorArgs- custom argument values to be fed into Spring's constructor resolution algorithm, resolving either all arguments or just specific ones, with the rest to be resolved through regular autowiring (may benullor empty)- 从以下版本开始:
- 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
registerBean
public final <T> void registerBean(Class<T> beanClass, BeanDefinitionCustomizer... customizers)
Register a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression).- 参数:
beanClass- the class of the bean (resolving a public constructor to be autowired, possibly simply the default constructor)customizers- one or more callbacks for customizing the factory'sBeanDefinition, e.g. setting a lazy-init or primary flag- 从以下版本开始:
- 5.0
- 另请参阅:
registerBean(String, Class, Supplier, BeanDefinitionCustomizer...)
registerBean
public final <T> void registerBean(@Nullable String beanName, Class<T> beanClass, BeanDefinitionCustomizer... customizers)
Register a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression).- 参数:
beanName- the name of the bean (may benull)beanClass- the class of the bean (resolving a public constructor to be autowired, possibly simply the default constructor)customizers- one or more callbacks for customizing the factory'sBeanDefinition, e.g. setting a lazy-init or primary flag- 从以下版本开始:
- 5.0
- 另请参阅:
registerBean(String, Class, Supplier, BeanDefinitionCustomizer...)
registerBean
public final <T> void registerBean(Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers)
Register a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression).- 参数:
beanClass- the class of the beansupplier- a callback for creating an instance of the beancustomizers- one or more callbacks for customizing the factory'sBeanDefinition, e.g. setting a lazy-init or primary flag- 从以下版本开始:
- 5.0
- 另请参阅:
registerBean(String, Class, Supplier, BeanDefinitionCustomizer...)
registerBean
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers)
Register a bean from the given bean class, using the given supplier for obtaining a new instance (typically declared as a lambda expression or method reference), optionally customizing its bean definition metadata (again typically declared as a lambda expression).This method can be overridden to adapt the registration mechanism for all
registerBeanmethods (since they all delegate to this one).- 参数:
beanName- the name of the bean (may benull)beanClass- the class of the beansupplier- a callback for creating an instance of the bean (in case ofnull, resolving a public constructor to be autowired instead)customizers- one or more callbacks for customizing the factory'sBeanDefinition, e.g. setting a lazy-init or primary flag- 从以下版本开始:
- 5.0