Top Description Inners Fields Constructors Methods
org.springframework.context.support

public Class GenericApplicationContext

extends AbstractApplicationContext
implements BeanDefinitionRegistry
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.core.AliasRegistry
Known Direct Subclasses
org.springframework.context.support.GenericGroovyApplicationContext, org.springframework.context.support.GenericXmlApplicationContext, org.springframework.context.support.StaticApplicationContext, org.springframework.context.annotation.AnnotationConfigApplicationContext
Imports
java.io.IOException, java.lang.reflect.Constructor, java.util.List, java.util.concurrent.atomic.AtomicBoolean, java.util.function.Supplier, org.springframework.aot.hint.RuntimeHints, org.springframework.aot.hint.support.ClassHintUtils, org.springframework.beans.BeanUtils, .BeansException, org.springframework.beans.factory.BeanDefinitionStoreException, .NoSuchBeanDefinitionException, org.springframework.beans.factory.config.AutowireCapableBeanFactory, .BeanDefinition, .BeanDefinitionCustomizer, .ConfigurableListableBeanFactory, .SmartInstantiationAwareBeanPostProcessor, org.springframework.beans.factory.support.BeanDefinitionRegistry, .BeanDefinitionRegistryPostProcessor, .DefaultListableBeanFactory, .MergedBeanDefinitionPostProcessor, .RootBeanDefinition, org.springframework.context.ApplicationContext, org.springframework.core.io.ProtocolResolver, .Resource, .ResourceLoader, org.springframework.core.io.support.ResourcePatternResolver, org.springframework.core.metrics.ApplicationStartup, org.springframework.lang.Nullable, org.springframework.util.Assert

Generic ApplicationContext implementation that holds a single internal org.springframework.beans.factory.support.DefaultListableBeanFactory instance and does not assume a specific bean definition format. Implements the org.springframework.beans.factory.support.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 org.springframework.beans.factory.support.BeanDefinitionRegistry interface and then call refresh() to initialize those beans with application context semantics (handling org.springframework.context.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. refresh() may only be called once.

This ApplicationContext implementation is suitable for Ahead of Time processing, using refreshForAotProcessing as an alternative to the regular refresh().

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, you may also use ClassPathXmlApplicationContext or FileSystemXmlApplicationContext, 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. For a custom application context implementation supposed to read a specific bean definition format in a refreshable manner, consider deriving from the AbstractRefreshableApplicationContext base class.
Authors
Juergen Hoeller, Chris Beams, Stephane Nicoll, Sam Brannen
Since
1.1.2
See Also
registerBeanDefinition, refresh(), org.springframework.beans.factory.xml.XmlBeanDefinitionReader, org.springframework.beans.factory.support.PropertiesBeanDefinitionReader

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
GenericApplicationContext.ClassDerivedBeanDefinition

RootBeanDefinition subclass for #registerBean based registrations with flexible autowiring for public constructors.

Field Summary

Modifier and TypeField and Description
private final DefaultListableBeanFactory
private boolean
private final AtomicBoolean
private ResourceLoader
Inherited from org.springframework.context.support.AbstractApplicationContext:
APPLICATION_EVENT_MULTICASTER_BEAN_NAMELIFECYCLE_PROCESSOR_BEAN_NAMEloggerMESSAGE_SOURCE_BEAN_NAME

Constructor Summary

AccessConstructor and Description
public
GenericApplicationContext()

Create a new GenericApplicationContext.

public
GenericApplicationContext(DefaultListableBeanFactory
the DefaultListableBeanFactory instance to use for this context
beanFactory
)

Create a new GenericApplicationContext with the given DefaultListableBeanFactory.

public
GenericApplicationContext(ApplicationContext
the parent application context
parent
)

Create a new GenericApplicationContext with the given parent.

public
GenericApplicationContext(DefaultListableBeanFactory
the DefaultListableBeanFactory instance to use for this context
beanFactory
,
ApplicationContext
the parent application context
parent
)

Create a new GenericApplicationContext with the given DefaultListableBeanFactory.

Method Summary

Modifier and TypeMethod and Description
protected void
cancelRefresh(Throwable
the exception that led to the cancellation
ex
)

Overrides org.springframework.context.support.AbstractApplicationContext.cancelRefresh.

Cancel this context's refresh attempt, resetting the active flag after an exception got thrown.

protected final void
closeBeanFactory()

Implements abstract org.springframework.context.support.AbstractApplicationContext.closeBeanFactory.

Not much to do: We hold a single internal BeanFactory that will never get released.

public AutowireCapableBeanFactory
public BeanDefinition
getBeanDefinition(String
name of the bean to find a definition for
beanName
)

Implements org.springframework.beans.factory.support.BeanDefinitionRegistry.getBeanDefinition.

Return the BeanDefinition for the given bean name.

public final ConfigurableListableBeanFactory
getBeanFactory()

Implements abstract org.springframework.context.support.AbstractApplicationContext.getBeanFactory.

Implements org.springframework.context.ConfigurableApplicationContext.getBeanFactory.

Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).

public ClassLoader
public final DefaultListableBeanFactory

Returns:

the internal bean factory (as DefaultListableBeanFactory)
getDefaultListableBeanFactory
()

Return the underlying bean factory of this context, available for registering bean definitions.

public Resource
getResource(String
the resource location
location
)

Overrides org.springframework.core.io.DefaultResourceLoader.getResource.

Implements org.springframework.core.io.ResourceLoader.getResource.

This implementation delegates to this context's ResourceLoader if set, falling back to the default superclass behavior otherwise.

public Resource[]
getResources(String
the location pattern to resolve
locationPattern
)

Overrides org.springframework.context.support.AbstractApplicationContext.getResources.

Implements org.springframework.core.io.support.ResourcePatternResolver.getResources.

This implementation delegates to this context's ResourceLoader if it implements the ResourcePatternResolver interface, falling back to the default superclass behavior otherwise.

public boolean
isAlias(String
the name to check
beanName
)

Implements org.springframework.core.AliasRegistry.isAlias.

Determine whether the given name is defined as an alias (as opposed to the name of an actually registered component).

public boolean
isBeanDefinitionOverridable(String
the name to check
beanName
)

Overrides default org.springframework.beans.factory.support.BeanDefinitionRegistry.isBeanDefinitionOverridable.

Determine whether the bean definition for the given name is overridable, i.e. whether registerBeanDefinition would successfully return against an existing definition of the same name.

public boolean
isBeanNameInUse(String
the name to check
beanName
)

Implements org.springframework.beans.factory.support.BeanDefinitionRegistry.isBeanNameInUse.

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.

private void
preDetermineBeanTypes(RuntimeHints runtimeHints)

Pre-determine bean types in order to trigger early proxy class creation.

protected final void
refreshBeanFactory()

Implements abstract org.springframework.context.support.AbstractApplicationContext.refreshBeanFactory.

Do nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's).

public void
refreshForAotProcessing(RuntimeHints
the runtime hints
runtimeHints
)

Load or refresh the persistent representation of the configuration up to a point where the underlying bean factory is ready to create bean instances.

public void
registerAlias(String
the canonical name
beanName
,
String
the alias to be registered
alias
)

Implements org.springframework.core.AliasRegistry.registerAlias.

Given a name, register an alias for it.

public <T> void
registerBean(Class<T>
the class of the bean
beanClass
,
Object...
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 be null or empty)
constructorArgs
)

Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process.

public <T> void
registerBean(String
the name of the bean (may be null)
beanName
,
Class<T>
the class of the bean
beanClass
,
Object...
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 be null or empty)
constructorArgs
)

Register a bean from the given bean class, optionally providing explicit constructor arguments for consideration in the autowiring process.

public final <T> void
registerBean(Class<T>
the class of the bean (resolving a public constructor to be autowired, possibly simply the default constructor)
beanClass
,
BeanDefinitionCustomizer...
one or more callbacks for customizing the factory's BeanDefinition, e.g. setting a lazy-init or primary flag
customizers
)

Register a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression).

public final <T> void
registerBean(String
the name of the bean (may be null)
beanName
,
Class<T>
the class of the bean (resolving a public constructor to be autowired, possibly simply the default constructor)
beanClass
,
BeanDefinitionCustomizer...
one or more callbacks for customizing the factory's BeanDefinition, e.g. setting a lazy-init or primary flag
customizers
)

Register a bean from the given bean class, optionally customizing its bean definition metadata (typically declared as a lambda expression).

public final <T> void
registerBean(Class<T>
the class of the bean
beanClass
,
Supplier<T>
a callback for creating an instance of the bean
supplier
,
BeanDefinitionCustomizer...
one or more callbacks for customizing the factory's BeanDefinition, e.g. setting a lazy-init or primary flag
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).

public <T> void
registerBean(String
the name of the bean (may be null)
beanName
,
Class<T>
the class of the bean
beanClass
,
Supplier<T>
a callback for creating an instance of the bean (in case of null, resolving a public constructor to be autowired instead)
supplier
,
BeanDefinitionCustomizer...
one or more callbacks for customizing the factory's BeanDefinition, e.g. setting a lazy-init or primary flag
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).

public void
registerBeanDefinition(String
the name of the bean instance to register
beanName
,
BeanDefinition
definition of the bean instance to register
beanDefinition
)

Implements org.springframework.beans.factory.support.BeanDefinitionRegistry.registerBeanDefinition.

Register a new bean definition with this registry.

public void
removeAlias(String
the alias to remove
alias
)

Implements org.springframework.core.AliasRegistry.removeAlias.

Remove the specified alias from this registry.

public void
removeBeanDefinition(String
the name of the bean instance to register
beanName
)

Implements org.springframework.beans.factory.support.BeanDefinitionRegistry.removeBeanDefinition.

Remove the BeanDefinition for the given name.

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.

public void
setAllowCircularReferences(boolean allowCircularReferences)

Set whether to allow circular references between beans - and automatically try to resolve them.

public void
public void
setClassLoader(ClassLoader classLoader)

Overrides org.springframework.core.io.DefaultResourceLoader.setClassLoader.

Implements org.springframework.context.ConfigurableApplicationContext.setClassLoader.

Specify the ClassLoader to load class path resources and bean classes with.

public void
setParent(ApplicationContext
the parent context
parent
)

Overrides org.springframework.context.support.AbstractApplicationContext.setParent.

Implements org.springframework.context.ConfigurableApplicationContext.setParent.

Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.

public void
setResourceLoader(ResourceLoader resourceLoader)

Set a ResourceLoader to use for this context.

Inherited from org.springframework.context.support.AbstractApplicationContext:
addApplicationListeneraddBeanFactoryPostProcessorassertBeanFactoryActiveclosecontainsBeancontainsBeanDefinitioncontainsLocalBeancreateEnvironmentdestroyBeansdoClosefindAllAnnotationsOnBeanfindAnnotationOnBeanfindAnnotationOnBeanfinishBeanFactoryInitializationfinishRefreshgetAliasesgetApplicationEventMulticastergetApplicationListenersgetApplicationNamegetApplicationStartupgetBeangetBeangetBeangetBeangetBeangetBeanDefinitionCountgetBeanDefinitionNamesgetBeanFactoryPostProcessorsgetBeanNamesForAnnotationgetBeanNamesForTypegetBeanNamesForTypegetBeanNamesForTypegetBeanNamesForTypegetBeanProvidergetBeanProvidergetBeanProvidergetBeanProvidergetBeansOfTypegetBeansOfTypegetBeansWithAnnotationgetDisplayNamegetEnvironmentgetIdgetInternalParentBeanFactorygetInternalParentMessageSourcegetLifecycleProcessorgetMessagegetMessagegetMessagegetParentgetParentBeanFactorygetResourcePatternResolvergetStartupDategetTypegetTypeinitApplicationEventMulticasterinitLifecycleProcessorinitMessageSourceinitPropertySourcesinvokeBeanFactoryPostProcessorsisActiveisPrototypeisRunningisSingletonisTypeMatchisTypeMatchobtainFreshBeanFactoryonCloseonRefreshpostProcessBeanFactoryprepareBeanFactoryprepareRefreshpublishEventpublishEventpublishEventrefreshregisterBeanPostProcessorsregisterListenersregisterShutdownHookremoveApplicationListenerresetCommonCachessetDisplayNamesetEnvironmentsetIdstartstoptoString