public class

GenericApplicationContext

extends AbstractApplicationContext
implements BeanDefinitionRegistry
java.lang.Object
   ↳ org.springframework.core.io.DefaultResourceLoader
     ↳ org.springframework.context.support.AbstractApplicationContext
       ↳ org.springframework.context.support.GenericApplicationContext
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Generic ApplicationContext implementation that holds a single internal DefaultListableBeanFactory 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 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. 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 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. The equivalent in a web environment is XmlWebApplicationContext.

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.

Summary

[Expand]
Inherited Constants
From class org.springframework.context.support.AbstractApplicationContext
From interface org.springframework.beans.factory.BeanFactory
From interface org.springframework.context.ConfigurableApplicationContext
From interface org.springframework.core.io.ResourceLoader
From interface org.springframework.core.io.support.ResourcePatternResolver
[Expand]
Inherited Fields
From class org.springframework.context.support.AbstractApplicationContext
Public Constructors
GenericApplicationContext()
Create a new GenericApplicationContext.
GenericApplicationContext(DefaultListableBeanFactory beanFactory)
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
GenericApplicationContext(ApplicationContext parent)
Create a new GenericApplicationContext with the given parent.
GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent)
Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
Public Methods
BeanDefinition getBeanDefinition(String beanName)
Return the BeanDefinition for the given bean name.
final ConfigurableListableBeanFactory getBeanFactory()
Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).
final DefaultListableBeanFactory getDefaultListableBeanFactory()
Return the underlying bean factory of this context, available for registering bean definitions.
Resource getResource(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.
boolean isAlias(String beanName)
Determine whether this given name is defines as an alias (as opposed to the name of an actually registered component).
boolean isBeanNameInUse(String beanName)
Determine whether the given bean name is already in use within this registry, i.e.
void registerAlias(String beanName, String alias)
Given a name, register an alias for it.
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
Register a new bean definition with this registry.
void removeAlias(String alias)
Remove the specified alias from this registry.
void removeBeanDefinition(String beanName)
Remove the BeanDefinition for the given name.
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.
void setAllowCircularReferences(boolean allowCircularReferences)
Set whether to allow circular references between beans - and automatically try to resolve them.
void setId(String id)
Set the unique id of this application context.
void setParent(ApplicationContext parent)
Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.
void setResourceLoader(ResourceLoader resourceLoader)
Set a ResourceLoader to use for this context.
Protected Methods
void cancelRefresh(BeansException ex)
Cancel this context's refresh attempt, resetting the active flag after an exception got thrown.
final void closeBeanFactory()
Not much to do: We hold a single internal BeanFactory that will never get released.
final void refreshBeanFactory()
Do nothing: We hold a single internal BeanFactory and rely on callers to register beans through our public methods (or the BeanFactory's).
[Expand]
Inherited Methods
From class org.springframework.context.support.AbstractApplicationContext
From class org.springframework.core.io.DefaultResourceLoader
From class java.lang.Object
From interface org.springframework.beans.factory.BeanFactory
From interface org.springframework.beans.factory.DisposableBean
From interface org.springframework.beans.factory.HierarchicalBeanFactory
From interface org.springframework.beans.factory.ListableBeanFactory
From interface org.springframework.beans.factory.support.BeanDefinitionRegistry
From interface org.springframework.context.ApplicationContext
From interface org.springframework.context.ApplicationEventPublisher
From interface org.springframework.context.ConfigurableApplicationContext
From interface org.springframework.context.Lifecycle
From interface org.springframework.context.MessageSource
From interface org.springframework.core.AliasRegistry
From interface org.springframework.core.env.EnvironmentCapable
From interface org.springframework.core.io.ResourceLoader
From interface org.springframework.core.io.support.ResourcePatternResolver

Public Constructors

public GenericApplicationContext ()

Create a new GenericApplicationContext.

public GenericApplicationContext (DefaultListableBeanFactory beanFactory)

Create a new GenericApplicationContext with the given DefaultListableBeanFactory.

Parameters
beanFactory the DefaultListableBeanFactory instance to use for this context

public GenericApplicationContext (ApplicationContext parent)

Create a new GenericApplicationContext with the given parent.

Parameters
parent the parent application context

public GenericApplicationContext (DefaultListableBeanFactory beanFactory, ApplicationContext parent)

Create a new GenericApplicationContext with the given DefaultListableBeanFactory.

Parameters
beanFactory the DefaultListableBeanFactory instance to use for this context
parent the parent application context

Public Methods

public BeanDefinition getBeanDefinition (String beanName)

Return the BeanDefinition for the given bean name.

Parameters
beanName name of the bean to find a definition for
Returns
  • the BeanDefinition for the given name (never null)

public final ConfigurableListableBeanFactory getBeanFactory ()

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

Returns
  • this application context's internal bean factory (never null)

public final DefaultListableBeanFactory getDefaultListableBeanFactory ()

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

NOTE: You need to call refresh() to initialize the bean factory and its contained beans with application context semantics (autodetecting BeanFactoryPostProcessors, etc).

Returns
  • the internal bean factory (as DefaultListableBeanFactory)

public Resource getResource (String location)

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

Parameters
location the resource location
Returns
  • a corresponding Resource handle

public 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.

Parameters
locationPattern the location pattern to resolve
Returns
  • the corresponding Resource objects
Throws
IOException

public boolean isAlias (String beanName)

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

Parameters
beanName the bean name to check
Returns
  • whether the given name is an alias

public boolean isBeanNameInUse (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.

Parameters
beanName the name to check
Returns
  • whether the given bean name is already in use

public void registerAlias (String beanName, String alias)

Given a name, register an alias for it.

Parameters
beanName the canonical name
alias the alias to be registered

public void registerBeanDefinition (String beanName, BeanDefinition beanDefinition)

Register a new bean definition with this registry. Must support RootBeanDefinition and ChildBeanDefinition.

Parameters
beanName the name of the bean instance to register
beanDefinition definition of the bean instance to register

public void removeAlias (String alias)

Remove the specified alias from this registry.

Parameters
alias the alias to remove

public void removeBeanDefinition (String beanName)

Remove the BeanDefinition for the given name.

Parameters
beanName the name of the bean instance to register

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".

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.

public void setId (String id)

Set the unique id of this application context.

Default is the object id of the context instance, or the name of the context bean if the context is itself defined as a bean.

Parameters
id the unique id of the context

public void setParent (ApplicationContext parent)

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

Parameters
parent the parent context

public void setResourceLoader (ResourceLoader resourceLoader)

Set a ResourceLoader to use for this context. If set, the context will delegate all getResource calls 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 getResources calls as well. Else, default resource pattern matching will apply.

Protected Methods

protected void cancelRefresh (BeansException ex)

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

Parameters
ex the exception that led to the cancellation

protected final void closeBeanFactory ()

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

protected final void refreshBeanFactory ()

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