类 GenericApplicationContext
- 所有已实现的接口:
BeanFactory,HierarchicalBeanFactory,BeanDefinitionRegistry,DependencyInjectorProvider,ApplicationContext,ApplicationEventPublisher,ConfigurableApplicationContext,Lifecycle,MessageSource,cn.taketoday.core.AliasRegistry,cn.taketoday.core.env.EnvironmentCapable,cn.taketoday.core.io.PatternResourceLoader,cn.taketoday.core.io.ResourceLoader,Closeable,AutoCloseable
StandardBeanFactory
instance and does not assume a specific bean definition format. Implements
the BeanDefinitionRegistry
interface in order to allow for applying any bean definition readers to it.
Typical usage is to register a variety of bean definitions via the
BeanDefinitionRegistry
interface and then call AbstractApplicationContext.refresh() to initialize those beans
with application context semantics (handling
ApplicationContextAware, auto-detecting
BeanFactoryPostProcessors,
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 custom application context implementations that are supposed to read
special bean definition formats in a refreshable manner, consider deriving
from the AbstractRefreshableApplicationContext base class.
- 从以下版本开始:
- 4.0
- 作者:
- Juergen Hoeller, Chris Beams, Harry Yang, TODAY 2021/10/1 16:25
- 另请参阅:
-
嵌套类概要
从类继承的嵌套类/接口 cn.taketoday.core.io.DefaultResourceLoader
cn.taketoday.core.io.DefaultResourceLoader.ClassPathContextResource从接口继承的嵌套类/接口 cn.taketoday.context.ApplicationContext
ApplicationContext.State -
字段概要
字段从类继承的字段 cn.taketoday.context.support.AbstractApplicationContext
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, LIFECYCLE_PROCESSOR_BEAN_NAME, log, MESSAGE_SOURCE_BEAN_NAME从接口继承的字段 cn.taketoday.context.ApplicationContext
APPLICATION_NAME从接口继承的字段 cn.taketoday.beans.factory.BeanFactory
FACTORY_BEAN_PREFIX从接口继承的字段 cn.taketoday.context.ConfigurableApplicationContext
CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SHUTDOWN_HOOK_THREAD_NAME从接口继承的字段 cn.taketoday.core.io.PatternResourceLoader
CLASSPATH_ALL_URL_PREFIX从接口继承的字段 cn.taketoday.core.io.ResourceLoader
CLASSPATH_URL_PREFIX -
构造器概要
构造器构造器说明Create a new GenericApplicationContext.GenericApplicationContext(StandardBeanFactory beanFactory) Create a new GenericApplicationContext with the given StandardBeanFactory.GenericApplicationContext(StandardBeanFactory beanFactory, ApplicationContext parent) Create a new GenericApplicationContext with the given StandardBeanFactory.Create a new GenericApplicationContext with the given parent. -
方法概要
修饰符和类型方法说明protected voidCancel this context's refresh attempt, after an exception got thrown.protected final voidNot much to do: We hold a single internal BeanFactory that will never get released.booleancontainsBeanDefinition(Class<?> type) booleancontainsBeanDefinition(Class<?> type, boolean equals) booleancontainsBeanDefinition(String beanName) protected BootstrapContextReturn the DefinitionLoadingContext to use for loading this contextgetAliasList(String name) Expose AutowireCapableBeanFactory functionality for this context.getBeanDefinition(Class<?> beanClass) getBeanDefinition(String beanName) intString[]Subclasses must return their internal bean factory here.cn.taketoday.core.io.ResourcegetResource(String location) This implementation delegates to this context'sResourceLoaderif set, falling back to the default superclass behavior otherwise.Set<cn.taketoday.core.io.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.booleanbooleanbooleanisBeanNameInUse(String beanName) protected final voidDo nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's).voidregisterAlias(String name, String alias) final <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(Class<T> beanClass, Object... constructorArgs) Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process.final <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).final <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).<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).voidregisterBeanDefinition(String name, BeanDefinition def) voidregisterPrototype(String name, Class<?> clazz) Register a prototype bean with the underlying bean factory.voidregisterPrototype(String name, Class<?> clazz, PropertyValues pvs) Register a prototype bean with the underlying bean factory.voidregisterSingleton(Object obj) voidregisterSingleton(String name, Class<?> clazz) Register a singleton bean with the underlying bean factory.voidregisterSingleton(String name, Class<?> clazz, PropertyValues pvs) Register a singleton bean with the underlying bean factory.voidregisterSingleton(String name, Object obj) Register a bean with the given name and bean instancevoidremoveAlias(String alias) voidremoveBeanDefinition(String beanName) voidThis implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior else.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 and bean classes with.voidsetParent(ApplicationContext parent) Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.voidsetResourceLoader(cn.taketoday.core.io.ResourceLoader resourceLoader) Set a ResourceLoader to use for this context.从类继承的方法 cn.taketoday.context.support.AbstractApplicationContext
addApplicationListener, addBeanFactoryPostProcessor, addFactoryPostProcessors, applyState, assertBeanFactoryActive, close, containsBean, containsLocalBean, createEnvironment, destroyBeans, doClose, findAnnotationOnBean, findAnnotationOnBean, findSynthesizedAnnotation, finishBeanFactoryInitialization, finishRefresh, formatStartupDate, getAliases, getAnnotatedBeans, getApplicationEventMulticaster, getApplicationListeners, getApplicationName, getBean, getBean, getBean, getBean, getBean, getBeanDefinitions, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeans, getBeansOfType, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getBeansWithAnnotation, getBootstrapContext, getDisplayName, getEnvironment, getExpressionEvaluator, getFactoryPostProcessors, getId, getInjector, getInternalParentBeanFactory, getInternalParentMessageSource, getLifecycleProcessor, getMessage, getMessage, getMessage, getObjectSupplier, getObjectSupplier, getObjectSupplier, getObjectSupplier, getParent, getParentBeanFactory, getPatternResourceLoader, getStartupDate, getState, getType, getType, hasStarted, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, initPropertySources, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, isTypeMatch, obtainBootstrapContext, obtainFreshBeanFactory, onClose, onRefresh, postProcessBeanFactory, prepareBeanFactory, prepareRefresh, publishEvent, publishEvent, refresh, registerApplicationListeners, registerBeanPostProcessors, registerFrameworkComponents, registerShutdownHook, removeApplicationListener, resetCommonCaches, setBootstrapContext, setDisplayName, setEnvironment, setId, setLifecycleProcessor, start, stop, toString, unwrapFactory从类继承的方法 cn.taketoday.core.io.DefaultResourceLoader
addProtocolResolver, clearResourceCaches, getProtocolResolvers, getResourceByPath, getResourceCache从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait从接口继承的方法 cn.taketoday.core.AliasRegistry
getAliases从接口继承的方法 cn.taketoday.beans.factory.support.BeanDefinitionRegistry
containsBeanDefinition, getBeanDefinitions从接口继承的方法 cn.taketoday.beans.factory.BeanFactory
getBeanNamesForType, unwrap从接口继承的方法 cn.taketoday.context.ConfigurableApplicationContext
addProtocolResolver从接口继承的方法 cn.taketoday.core.io.PatternResourceLoader
getResourcesArray
-
字段详细资料
-
beanFactory
-
-
构造器详细资料
-
GenericApplicationContext
public GenericApplicationContext()Create a new GenericApplicationContext. -
GenericApplicationContext
Create a new GenericApplicationContext with the given StandardBeanFactory.- 参数:
beanFactory- the StandardBeanFactory instance to use for this context- 另请参阅:
-
GenericApplicationContext
Create a new GenericApplicationContext with the given parent.- 参数:
parent- the parent application context- 另请参阅:
-
GenericApplicationContext
Create a new GenericApplicationContext with the given StandardBeanFactory.- 参数:
beanFactory- the StandardBeanFactory instance to use for this contextparent- the parent application context- 另请参阅:
-
-
方法详细资料
-
createBootstrapContext
从类复制的说明:AbstractApplicationContextReturn the DefinitionLoadingContext to use for loading this context- 指定者:
createBootstrapContext在类中AbstractApplicationContext- 返回:
- the DefinitionLoadingContext for this context
-
setParent
Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.- 指定者:
setParent在接口中ConfigurableApplicationContext- 覆盖:
setParent在类中AbstractApplicationContext- 参数:
parent- the parent context- 另请参阅:
-
refreshBeanFactory
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- 另请参阅:
-
cancelRefresh
从类复制的说明:AbstractApplicationContextCancel this context's refresh attempt, 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
从类复制的说明:AbstractApplicationContextSubclasses must return their internal bean factory here. They should implement the lookup efficiently, so that it can be called repeatedly without a performance penalty.Note: Subclasses should check whether the context is still active before returning the internal bean factory. The internal factory should generally be considered unavailable once the context has been closed.
- 指定者:
getBeanFactory在接口中ApplicationContext- 指定者:
getBeanFactory在接口中ConfigurableApplicationContext- 指定者:
getBeanFactory在类中AbstractApplicationContext- 返回:
- this application context's internal bean factory (never
null) - 另请参阅:
-
getAutowireCapableBeanFactory
从接口复制的说明:ApplicationContextExpose AutowireCapableBeanFactory functionality for this context.This is not typically used by application code, except for the purpose of initializing bean instances that live outside of the application context, applying the bean lifecycle (fully or partly) to them.
Alternatively, the internal BeanFactory exposed by the
ConfigurableApplicationContextinterface offers access to theAutowireCapableBeanFactoryinterface too. The present method mainly serves as a convenient, specific facility on the ApplicationContext interface.NOTE: this method will consistently throw IllegalStateException after the application context has been closed. In current Framework versions, only refreshable application contexts behave that way; all application context implementations will be required to comply.
- 指定者:
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- 另请参阅:
-
setResourceLoader
public void setResourceLoader(@Nullable cn.taketoday.core.io.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(java.lang.String)DefaultResourceLoaderPatternResourceLoaderPatternResourceLoader.getResourcesArray(java.lang.String)
-
getResource
This implementation delegates to this context'sResourceLoaderif set, falling back to the default superclass behavior otherwise.As of Spring Framework 5.3.22, this method also honors registered protocol resolvers when a custom
ResourceLoaderhas been set.- 指定者:
getResource在接口中cn.taketoday.core.io.ResourceLoader- 覆盖:
getResource在类中cn.taketoday.core.io.DefaultResourceLoader- 另请参阅:
-
setResourceLoader(ResourceLoader)DefaultResourceLoader.addProtocolResolver(ProtocolResolver)
-
getResources
This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior else.- 指定者:
getResources在接口中cn.taketoday.core.io.PatternResourceLoader- 覆盖:
getResources在类中AbstractApplicationContext- 抛出:
IOException- 另请参阅:
-
scan
public void scan(String locationPattern, cn.taketoday.core.io.ResourceConsumer consumer) throws IOException This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior else.- 指定者:
scan在接口中cn.taketoday.core.io.PatternResourceLoader- 覆盖:
scan在类中AbstractApplicationContext- 抛出:
IOException- 另请参阅:
-
setClassLoader
从接口复制的说明:ConfigurableApplicationContextSpecify the ClassLoader to load class path resources and bean classes with.This context class loader will be passed to the internal bean factory.
- 指定者:
setClassLoader在接口中ConfigurableApplicationContext- 覆盖:
setClassLoader在类中cn.taketoday.core.io.DefaultResourceLoader- 另请参阅:
-
DefaultResourceLoader(ClassLoader)
-
getClassLoader
- 指定者:
getClassLoader在接口中cn.taketoday.core.io.ResourceLoader- 覆盖:
getClassLoader在类中cn.taketoday.core.io.DefaultResourceLoader
-
registerBeanDefinition
- 指定者:
registerBeanDefinition在接口中BeanDefinitionRegistry
-
removeBeanDefinition
- 指定者:
removeBeanDefinition在接口中BeanDefinitionRegistry
-
getBeanDefinition
- 指定者:
getBeanDefinition在接口中BeanDefinitionRegistry
-
containsBeanDefinition
- 指定者:
containsBeanDefinition在接口中BeanDefinitionRegistry
-
containsBeanDefinition
- 指定者:
containsBeanDefinition在接口中BeanDefinitionRegistry
-
isBeanNameInUse
- 指定者:
isBeanNameInUse在接口中BeanDefinitionRegistry
-
isAllowBeanDefinitionOverriding
public boolean isAllowBeanDefinitionOverriding() -
getBeanDefinitionCount
public int getBeanDefinitionCount()- 指定者:
getBeanDefinitionCount在接口中BeanDefinitionRegistry- 指定者:
getBeanDefinitionCount在接口中BeanFactory- 覆盖:
getBeanDefinitionCount在类中AbstractApplicationContext
-
getBeanDefinitionNames
- 指定者:
getBeanDefinitionNames在接口中BeanDefinitionRegistry- 指定者:
getBeanDefinitionNames在接口中BeanFactory- 覆盖:
getBeanDefinitionNames在类中AbstractApplicationContext
-
containsBeanDefinition
- 指定者:
containsBeanDefinition在接口中BeanDefinitionRegistry- 指定者:
containsBeanDefinition在接口中BeanFactory- 覆盖:
containsBeanDefinition在类中AbstractApplicationContext
-
getBeanDefinition
- 指定者:
getBeanDefinition在接口中BeanDefinitionRegistry- 指定者:
getBeanDefinition在接口中BeanFactory- 覆盖:
getBeanDefinition在类中AbstractApplicationContext
-
registerSingleton
Register a bean with the given name and bean instance- 参数:
name- bean name (must not be null)obj- bean instance (must not be null)
-
registerSingleton
Register a singleton bean with the underlying bean factory.For more advanced needs, register with the underlying BeanFactory directly.
- 抛出:
BeansException- 另请参阅:
-
registerSingleton
public void registerSingleton(String name, Class<?> clazz, @Nullable PropertyValues pvs) throws BeansException Register a singleton bean with the underlying bean factory.For more advanced needs, register with the underlying BeanFactory directly.
- 抛出:
BeansException- 另请参阅:
-
registerPrototype
Register a prototype bean with the underlying bean factory.For more advanced needs, register with the underlying BeanFactory directly.
- 抛出:
BeansException- 另请参阅:
-
registerPrototype
public void registerPrototype(String name, Class<?> clazz, PropertyValues pvs) throws BeansException Register a prototype bean with the underlying bean factory.For more advanced needs, register with the underlying BeanFactory directly.
- 抛出:
BeansException- 另请参阅:
-
registerSingleton
-
registerBean
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 Framework's constructor resolution algorithm, resolving either all arguments or just specific ones, with the rest to be resolved through regular autowiring (may benullor empty)
-
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 Framework's constructor resolution algorithm, resolving either all arguments or just specific ones, with the rest to be resolved through regular autowiring (may benullor empty)
-
registerBean
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- 另请参阅:
-
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- 另请参阅:
-
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- 另请参阅:
-
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
-
registerAlias
- 指定者:
registerAlias在接口中cn.taketoday.core.AliasRegistry
-
removeAlias
- 指定者:
removeAlias在接口中cn.taketoday.core.AliasRegistry
-
isAlias
- 指定者:
isAlias在接口中cn.taketoday.core.AliasRegistry
-
getAliasList
- 指定者:
getAliasList在接口中cn.taketoday.core.AliasRegistry
-
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. This also applies to overriding aliases.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.
- 从以下版本开始:
- 4.0
-