org.springframework.context.support
Class GenericApplicationContext

java.lang.Object
  extended byorg.springframework.core.io.DefaultResourceLoader
      extended byorg.springframework.context.support.AbstractApplicationContext
          extended byorg.springframework.context.support.GenericApplicationContext
All Implemented Interfaces:
ApplicationContext, ApplicationEventPublisher, BeanDefinitionRegistry, BeanFactory, ConfigurableApplicationContext, HierarchicalBeanFactory, ListableBeanFactory, MessageSource, ResourceLoader, ResourcePatternResolver
Direct Known Subclasses:
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 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, as 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, implementing the extended WebApplicationContext interface.

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. For a corresponding base class that pre-implements the extended WebApplicationContext interface, consider AbstractRefreshableWebApplicationContext.

Since:
1.1.2
Author:
Juergen Hoeller
See Also:
registerBeanDefinition(java.lang.String, org.springframework.beans.factory.config.BeanDefinition), AbstractApplicationContext.refresh(), BeanDefinitionRegistry, DefaultListableBeanFactory, AbstractRefreshableApplicationContext, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, WebApplicationContext, AbstractRefreshableWebApplicationContext, XmlWebApplicationContext

Field Summary
 
Fields inherited from class org.springframework.context.support.AbstractApplicationContext
APPLICATION_EVENT_MULTICASTER_BEAN_NAME, logger, MESSAGE_SOURCE_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_URL_PREFIX
 
Constructor Summary
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
 BeanDefinition getBeanDefinition(String beanName)
          Return the BeanDefinition for the given bean name.
 ConfigurableListableBeanFactory getBeanFactory()
          Return the single internal BeanFactory held by this context.
 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.
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 bean name, create an alias.
 void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
          Register a new bean definition with this registry.
 void setResourceLoader(ResourceLoader resourceLoader)
          Set a ResourceLoader to use for this context.
 
Methods inherited from class org.springframework.context.support.AbstractApplicationContext
addBeanFactoryPostProcessor, addListener, close, containsBean, containsBeanDefinition, getAliases, getBean, getBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanDefinitionNames, getBeanFactoryPostProcessors, getBeanNamesForType, getBeansOfType, getBeansOfType, getDisplayName, getInternalParentBeanFactory, getInternalParentMessageSource, getMessage, getMessage, getMessage, getParent, getParentBeanFactory, getResourcePatternResolver, getResources, getStartupDate, getType, isSingleton, onRefresh, postProcessBeanFactory, publishEvent, refresh, setDisplayName, setParent, toString
 
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
getClassLoader, getResourceByPath
 
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, getAliases, getBeanDefinitionCount, getBeanDefinitionNames
 

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

setResourceLoader

public void setResourceLoader(ResourceLoader resourceLoader)
Set a ResourceLoader to use for this context. If set, the context will delegate all resource loading 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 (withour 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.

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

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
See Also:
setResourceLoader(org.springframework.core.io.ResourceLoader)

refreshBeanFactory

protected 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)

getBeanFactory

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

Specified by:
getBeanFactory in interface ConfigurableApplicationContext
Specified by:
getBeanFactory in class AbstractApplicationContext
Returns:
this application context's internal bean factory
See Also:
AbstractApplicationContext.refresh()

getDefaultListableBeanFactory

public 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 (auto-detecting BeanFactoryPostProcessors, etc)

See Also:
AbstractApplicationContext.refresh()

getBeanDefinition

public BeanDefinition getBeanDefinition(String beanName)
                                 throws BeansException
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:
BeansException - in case of errors

registerBeanDefinition

public void registerBeanDefinition(String beanName,
                                   BeanDefinition beanDefinition)
                            throws BeansException
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:
BeansException - if the bean definition is invalid
See Also:
RootBeanDefinition, ChildBeanDefinition

registerAlias

public void registerAlias(String beanName,
                          String alias)
                   throws BeansException
Description copied from interface: BeanDefinitionRegistry
Given a bean name, create an alias. We typically use this method to support names that are illegal within XML ids (used for bean names).

Specified by:
registerAlias in interface BeanDefinitionRegistry
Parameters:
beanName - the name of the bean
alias - alias that will behave the same as the bean name
Throws:
BeansException - if the alias is already in use


Copyright (C) 2003-2004 The Spring Framework Project.