Save This Page
Home » spring-framework-2.5.4 » org.springframework » context » support » [javadoc | source]
org.springframework.context.support
public class: GenericApplicationContext [javadoc | source]
java.lang.Object
   org.springframework.core.io.DefaultResourceLoader
      org.springframework.context.support.AbstractApplicationContext
         org.springframework.context.support.GenericApplicationContext

All Implemented Interfaces:
    BeanDefinitionRegistry, ConfigurableApplicationContext, DisposableBean, ResourceLoader

Direct Known Subclasses:
    GenericWebApplicationContext, StaticApplicationContext, ResourceAdapterApplicationContext, StaticWebApplicationContext, StaticPortletApplicationContext

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.

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 org.springframework.web.context.support.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.

Fields inherited from org.springframework.context.support.AbstractApplicationContext:
MESSAGE_SOURCE_BEAN_NAME,  APPLICATION_EVENT_MULTICASTER_BEAN_NAME,  logger
Constructor:
 public GenericApplicationContext() 
 public GenericApplicationContext(DefaultListableBeanFactory beanFactory) 
    Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
 public GenericApplicationContext(ApplicationContext parent) 
    Create a new GenericApplicationContext with the given parent.
 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
    Also see:
    registerBeanDefinition
    refresh
Method from org.springframework.context.support.GenericApplicationContext Summary:
closeBeanFactory,   getBeanDefinition,   getBeanFactory,   getDefaultListableBeanFactory,   getResource,   getResources,   isAlias,   isBeanNameInUse,   refreshBeanFactory,   registerAlias,   registerBeanDefinition,   removeAlias,   removeBeanDefinition,   setParent,   setResourceLoader
Methods from org.springframework.context.support.AbstractApplicationContext:
addApplicationListener,   addBeanFactoryPostProcessor,   addListener,   cancelRefresh,   close,   closeBeanFactory,   containsBean,   containsBeanDefinition,   containsLocalBean,   destroy,   destroyBeans,   doClose,   finishBeanFactoryInitialization,   finishRefresh,   getAliases,   getApplicationListeners,   getAutowireCapableBeanFactory,   getBean,   getBean,   getBean,   getBeanDefinitionCount,   getBeanDefinitionNames,   getBeanFactory,   getBeanFactoryPostProcessors,   getBeanNamesForType,   getBeanNamesForType,   getBeansOfType,   getBeansOfType,   getDisplayName,   getId,   getInternalParentBeanFactory,   getInternalParentMessageSource,   getMessage,   getMessage,   getMessage,   getParent,   getParentBeanFactory,   getResourcePatternResolver,   getResources,   getStartupDate,   getType,   initApplicationEventMulticaster,   initMessageSource,   invokeBeanFactoryPostProcessors,   isActive,   isPrototype,   isRunning,   isSingleton,   isTypeMatch,   obtainFreshBeanFactory,   onClose,   onRefresh,   postProcessBeanFactory,   prepareBeanFactory,   prepareRefresh,   publishEvent,   refresh,   refreshBeanFactory,   registerBeanPostProcessors,   registerListeners,   registerShutdownHook,   setDisplayName,   setId,   setParent,   start,   stop,   toString
Methods from org.springframework.core.io.DefaultResourceLoader:
getClassLoader,   getResource,   getResourceByPath,   setClassLoader
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.context.support.GenericApplicationContext Detail:
 protected final  void closeBeanFactory() 
    Do nothing: We hold a single internal BeanFactory that will never get released.
 public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException 
 public final ConfigurableListableBeanFactory getBeanFactory() 
    Return the single internal BeanFactory held by this context (as ConfigurableListableBeanFactory).
 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).

 public Resource getResource(String location) 
    This implementation delegates to this context's ResourceLoader if set, falling back to the default superclass behavior else.
 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.
 public boolean isAlias(String beanName) 
 public boolean isBeanNameInUse(String beanName) 
 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).
 public  void registerAlias(String beanName,
    String alias) 
 public  void registerBeanDefinition(String beanName,
    BeanDefinition beanDefinition) throws BeanDefinitionStoreException 
 public  void removeAlias(String alias) 
 public  void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException 
 public  void setParent(ApplicationContext parent) 
    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. 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 (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.

    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.