GenericApplicationContext (Spring Framework 3.2.1.RELEASE API)
Date: 2013-01-24

org.springframework.context.support

## Class GenericApplicationContext

- java.lang.Object
- - org.springframework.core.io.DefaultResourceLoader
  - - org.springframework.context.support.AbstractApplicationContext
    - - org.springframework.context.support.GenericApplicationContext

- All Implemented Interfaces:
    Closeable, AutoCloseable, BeanFactory, DisposableBean, HierarchicalBeanFactory, ListableBeanFactory, BeanDefinitionRegistry, ApplicationContext, ApplicationEventPublisher, ConfigurableApplicationContext, Lifecycle, MessageSource, AliasRegistry, EnvironmentCapable, ResourceLoader, ResourcePatternResolver
  Direct Known Subclasses:
    AnnotationConfigApplicationContext, GenericWebApplicationContext, GenericXmlApplicationContext, ResourceAdapterApplicationContext, StaticApplicationContext

      public class GenericApplicationContext
      extends AbstractApplicationContext
      implements BeanDefinitionRegistry

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

  Since:
    1.1.2
  Author:
    Juergen Hoeller, Chris Beams
  See Also:
    `registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)`, `AbstractApplicationContext.refresh()`, `XmlBeanDefinitionReader`, `PropertiesBeanDefinitionReader`

- - ### Field Summary

    - ### Fields inherited from class org.springframework.context.support.AbstractApplicationContext

      `APPLICATION_EVENT_MULTICASTER_BEAN_NAME, LIFECYCLE_PROCESSOR_BEAN_NAME, logger, MESSAGE_SOURCE_BEAN_NAME`

    - ### Fields inherited from interface org.springframework.context.ConfigurableApplicationContext

      `CONFIG_LOCATION_DELIMITERS, CONVERSION_SERVICE_BEAN_NAME, ENVIRONMENT_BEAN_NAME, LOAD_TIME_WEAVER_BEAN_NAME, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME`

    - ### Fields inherited from interface org.springframework.beans.factory.BeanFactory

      `FACTORY_BEAN_PREFIX`

    - ### Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolver

      `CLASSPATH_ALL_URL_PREFIX`

    - ### Fields inherited from interface org.springframework.core.io.ResourceLoader

      `CLASSPATH_URL_PREFIX`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `GenericApplicationContext()` Create a new GenericApplicationContext.
    `GenericApplicationContext(ApplicationContext parent)` Create a new GenericApplicationContext with the given parent.
    `GenericApplicationContext(DefaultListableBeanFactory beanFactory)` Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
    `GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent)` Create a new GenericApplicationContext with the given DefaultListableBeanFactory.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `cancelRefresh(BeansException ex)` Cancel this context's refresh attempt, resetting the `active` flag after an exception got thrown.
    `protected void` | `closeBeanFactory()` Not much to do: We hold a single internal BeanFactory that will never get released.
    `BeanDefinition` | `getBeanDefinition(String beanName)` Return the BeanDefinition for the given bean name.
    `ConfigurableListableBeanFactory` | `getBeanFactory()` Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).
    `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.
    `protected 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).
    `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.

    - ### Methods inherited from class org.springframework.context.support.AbstractApplicationContext

      `addApplicationListener, addBeanFactoryPostProcessor, addListener, close, containsBean, containsBeanDefinition, containsLocalBean, createEnvironment, destroy, destroyBeans, doClose, findAnnotationOnBean, finishBeanFactoryInitialization, finishRefresh, getAliases, getApplicationListeners, getApplicationName, getAutowireCapableBeanFactory, getBean, getBean, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForType, getBeanNamesForType, getBeansOfType, getBeansOfType, getBeansWithAnnotation, getDisplayName, getEnvironment, getId, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getResourcePatternResolver, getStartupDate, getType, initApplicationEventMulticaster, initLifecycleProcessor, initMessageSource, initPropertySources, invokeBeanFactoryPostProcessors, isActive, isPrototype, isRunning, isSingleton, isTypeMatch, obtainFreshBeanFactory, onClose, onRefresh, postProcessBeanFactory, prepareBeanFactory, prepareRefresh, publishEvent, refresh, registerBeanPostProcessors, registerListeners, registerShutdownHook, setDisplayName, setEnvironment, start, stop, toString`

    - ### Methods inherited from class org.springframework.core.io.DefaultResourceLoader

      `getClassLoader, getResourceByPath, setClassLoader`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.beans.factory.support.BeanDefinitionRegistry

      `containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames`

    - ### Methods inherited from interface org.springframework.core.AliasRegistry

      `getAliases`

    - ### Methods inherited from interface org.springframework.core.io.ResourceLoader

      `getClassLoader`

- - ### Constructor Detail

    - #### GenericApplicationContext

          public GenericApplicationContext()

      Create a new GenericApplicationContext.
      See Also:
        `registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)`, `AbstractApplicationContext.refresh()`

    - #### GenericApplicationContext

          public GenericApplicationContext(DefaultListableBeanFactory beanFactory)

      Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
      Parameters:
        `beanFactory` - the DefaultListableBeanFactory instance to use for this context
      See Also:
        `registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)`, `AbstractApplicationContext.refresh()`

    - #### GenericApplicationContext

          public GenericApplicationContext(ApplicationContext parent)

      Create a new GenericApplicationContext with the given parent.
      Parameters:
        `parent` - the parent application context
      See Also:
        `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.
      Parameters:
        `beanFactory` - the DefaultListableBeanFactory instance to use for this context
        `parent` - the parent application context
      See Also:
        `registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)`, `AbstractApplicationContext.refresh()`

  - ### Method Detail

    - #### setParent

          public void setParent(ApplicationContext parent)

      Set the parent of this application context, also setting the parent of the internal BeanFactory accordingly.
      Specified by:
        `setParent` in interface `ConfigurableApplicationContext`
      Overrides:
        `setParent` in class `AbstractApplicationContext`
      Parameters:
        `parent` - the parent context
      See Also:
        `ConfigurableBeanFactory.setParentBeanFactory(org.springframework.beans.factory.BeanFactory)`

    - #### setId

          public void setId(String id)

      Description copied from class: `AbstractApplicationContext`
      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.

      Specified by:
        `setId` in interface `ConfigurableApplicationContext`
      Overrides:
        `setId` in class `AbstractApplicationContext`
      Parameters:
        `id` - the unique id of the context

    - #### 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".
      See Also:
        `DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)`

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

      See Also:
        `AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)`

    - #### setResourceLoader

          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.

      See Also:
        `getResource(java.lang.String)`, `DefaultResourceLoader`, `FileSystemResourceLoader`, `ResourcePatternResolver`, `getResources(java.lang.String)`

    - #### getResource

          public Resource getResource(String location)

      This implementation delegates to this context's ResourceLoader if set, falling back to the default superclass behavior else.
      Specified by:
        `getResource` in interface `ResourceLoader`
      Overrides:
        `getResource` in class `DefaultResourceLoader`
      Parameters:
        `location` - the resource location
      Returns:
        a corresponding Resource handle
      See Also:
        `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.
      Specified by:
        `getResources` in interface `ResourcePatternResolver`
      Overrides:
        `getResources` in class `AbstractApplicationContext`
      Parameters:
        `locationPattern` - the location pattern to resolve
      Returns:
        the corresponding Resource objects
      Throws:
        `IOException` - in case of I/O errors
      See Also:
        `setResourceLoader(org.springframework.core.io.ResourceLoader)`

    - #### 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).
      Specified by:
        `refreshBeanFactory` in class `AbstractApplicationContext`
      Throws:
        `IllegalStateException` - if already initialized and multiple refresh attempts are not supported
      See Also:
        `registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition)`

    - #### cancelRefresh

          protected void cancelRefresh(BeansException ex)

      Description copied from class: `AbstractApplicationContext`
      Cancel this context's refresh attempt, resetting the `active` flag after an exception got thrown.
      Overrides:
        `cancelRefresh` in class `AbstractApplicationContext`
      Parameters:
        `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.
      Specified by:
        `closeBeanFactory` in class `AbstractApplicationContext`

    - #### getBeanFactory

          public final ConfigurableListableBeanFactory getBeanFactory()

      Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).
      Specified by:
        `getBeanFactory` in interface `ConfigurableApplicationContext`
      Specified by:
        `getBeanFactory` in class `AbstractApplicationContext`
      Returns:
        this application context's internal bean factory (never `null`)
      See Also:
        `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).

      Returns:
        the internal bean factory (as DefaultListableBeanFactory)

    - #### registerBeanDefinition

          public void registerBeanDefinition(String beanName,
                                    BeanDefinition beanDefinition)
                                      throws BeanDefinitionStoreException

      Description copied from interface: `BeanDefinitionRegistry`
      Register a new bean definition with this registry. Must support RootBeanDefinition and ChildBeanDefinition.
      Specified by:
        `registerBeanDefinition` in interface `BeanDefinitionRegistry`
      Parameters:
        `beanName` - the name of the bean instance to register
        `beanDefinition` - definition of the bean instance to register
      Throws:
        `BeanDefinitionStoreException` - if the BeanDefinition is invalid or if there is already a BeanDefinition for the specified bean name (and we are not allowed to override it)
      See Also:
        `RootBeanDefinition`, `ChildBeanDefinition`

    - #### removeBeanDefinition

          public void removeBeanDefinition(String beanName)
                                    throws NoSuchBeanDefinitionException

      Description copied from interface: `BeanDefinitionRegistry`
      Remove the BeanDefinition for the given name.
      Specified by:
        `removeBeanDefinition` in interface `BeanDefinitionRegistry`
      Parameters:
        `beanName` - the name of the bean instance to register
      Throws:
        `NoSuchBeanDefinitionException` - if there is no such bean definition

    - #### getBeanDefinition

          public BeanDefinition getBeanDefinition(String beanName)
                                           throws NoSuchBeanDefinitionException

      Description copied from interface: `BeanDefinitionRegistry`
      Return the BeanDefinition for the given bean name.
      Specified by:
        `getBeanDefinition` in interface `BeanDefinitionRegistry`
      Parameters:
        `beanName` - name of the bean to find a definition for
      Returns:
        the BeanDefinition for the given name (never `null`)
      Throws:
        `NoSuchBeanDefinitionException` - if there is no such bean definition

    - #### isBeanNameInUse

          public boolean isBeanNameInUse(String beanName)

      Description copied from interface: `BeanDefinitionRegistry`
      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.
      Specified by:
        `isBeanNameInUse` in interface `BeanDefinitionRegistry`
      Parameters:
        `beanName` - the name to check
      Returns:
        whether the given bean name is already in use

    - #### registerAlias

          public void registerAlias(String beanName,
                           String alias)

      Description copied from interface: `AliasRegistry`
      Given a name, register an alias for it.
      Specified by:
        `registerAlias` in interface `AliasRegistry`
      Parameters:
        `beanName` - the canonical name
        `alias` - the alias to be registered

    - #### removeAlias

          public void removeAlias(String alias)

      Description copied from interface: `AliasRegistry`
      Remove the specified alias from this registry.
      Specified by:
        `removeAlias` in interface `AliasRegistry`
      Parameters:
        `alias` - the alias to remove

    - #### isAlias

          public boolean isAlias(String beanName)

      Description copied from interface: `AliasRegistry`
      Determine whether this given name is defines as an alias (as opposed to the name of an actually registered component).
      Specified by:
        `isAlias` in interface `AliasRegistry`
      Parameters:
        `beanName` - the bean name to check
      Returns:
        whether the given name is an alias