org.springframework.context.support
Class AbstractApplicationContext

java.lang.Object
  extended byorg.springframework.core.io.DefaultResourceLoader
      extended byorg.springframework.context.support.AbstractApplicationContext
All Implemented Interfaces:
ApplicationContext, BeanFactory, ConfigurableApplicationContext, HierarchicalBeanFactory, ListableBeanFactory, MessageSource, ResourceLoader
Direct Known Subclasses:
AbstractXmlApplicationContext, StaticApplicationContext

public abstract class AbstractApplicationContext
extends DefaultResourceLoader
implements ConfigurableApplicationContext

Partial implementation of ApplicationContext. Doesn't mandate the type of storage used for configuration, but implements common functionality. Uses the Template Method design pattern, requiring concrete subclasses to implement abstract methods.

In contrast to a plain bean factory, an ApplicationContext is supposed to detect special beans defined in its bean factory: Therefore, this class automatically registers BeanFactoryPostProcessors, BeanPostProcessors and ApplicationListeners that are defined as beans in the context.

A MessageSource may be also supplied as a bean in the context, with the name "messageSource". Else, message resolution is delegated to the parent context.

Implements resource loading through extending DefaultResourceLoader. Therefore, treats resource paths as class path resources. Only supports full classpath resource names that include the package path, like "mypackage/myresource.dat".

Since:
January 21, 2001
Version:
$Revision: 1.35 $
Author:
Rod Johnson, Juergen Hoeller
See Also:
refreshBeanFactory(), getBeanFactory(), MESSAGE_SOURCE_BEAN_NAME

Field Summary
protected  org.apache.commons.logging.Log logger
          Log4j logger used by this class.
static java.lang.String MESSAGE_SOURCE_BEAN_NAME
          Name of the MessageSource bean in the factory.
 
Fields inherited from interface org.springframework.core.io.ResourceLoader
CLASSPATH_URL_PREFIX
 
Constructor Summary
AbstractApplicationContext()
          Create a new AbstractApplicationContext with no parent.
AbstractApplicationContext(ApplicationContext parent)
          Create a new AbstractApplicationContext with the given parent context.
 
Method Summary
 void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor)
          Add a new BeanFactoryPostProcessor that will get applied to the internal bean factory of this application context on refresh, before any of the bean definitions get evaluated.
protected  void addListener(ApplicationListener listener)
          Subclasses can invoke this method to register a listener.
 void close()
          Destroy the singletons in the bean factory of this application context.
 boolean containsBean(java.lang.String name)
          Does this bean factory contain a bean with the given name?
 boolean containsBeanDefinition(java.lang.String name)
          Check if this bean factory contains a bean definition with the given name.
 java.lang.String[] getAliases(java.lang.String name)
          Return the aliases for the given bean name, if defined.
 java.lang.Object getBean(java.lang.String name)
          Return an instance (possibly shared or independent) of the given bean name.
 java.lang.Object getBean(java.lang.String name, java.lang.Class requiredType)
          Return an instance (possibly shared or independent) of the given bean name.
 int getBeanDefinitionCount()
          Return the number of beans defined in the factory.
 java.lang.String[] getBeanDefinitionNames()
          Return the names of all beans defined in this factory.
 java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
          Return the names of beans matching the given type (including subclasses), judging from the bean definitions.
abstract  ConfigurableListableBeanFactory getBeanFactory()
          Subclasses must return their internal bean factory here.
 java.util.List getBeanFactoryPostProcessors()
          Return the list of BeanPostProcessors that will get applied to beans created with this factory.
 java.util.Map getBeansOfType(java.lang.Class type, boolean includePrototypes, boolean includeFactoryBeans)
          Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType() in the case of FactoryBeans.
 java.lang.String getDisplayName()
          Return a friendly name for context
 java.lang.String getMessage(MessageSourceResolvable resolvable, java.util.Locale locale)
          Try to resolve the message using all the attributes contained within the MessageSourceResolvable argument that was passed in.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.util.Locale locale)
          Try to resolve the message.
 java.lang.String getMessage(java.lang.String code, java.lang.Object[] args, java.lang.String defaultMessage, java.util.Locale locale)
          Try to resolve the message.
 ApplicationContext getParent()
          Return the parent context, or null if there is no parent, and this is the root of the context hierarchy.
 BeanFactory getParentBeanFactory()
          Return the parent bean factory, or null if there is none.
 long getStartupDate()
          Return the timestamp when this context was first loaded
 boolean isSingleton(java.lang.String name)
          Is this bean a singleton? That is, will getBean() always return the same object?
protected  void onRefresh()
          Template method which can be overridden to add context-specific refresh work.
protected  void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
          Modify the application context's internal bean factory after its standard initialization.
 void publishEvent(ApplicationEvent event)
          Publish the given event to all listeners.
 void refresh()
          Load or reload configuration.
protected abstract  void refreshBeanFactory()
          Subclasses must implement this method to perform the actual configuration load.
protected  void setDisplayName(java.lang.String displayName)
          To avoid endless constructor chaining, only concrete classes take this in their constructor, and then invoke this method
 void setParent(ApplicationContext parent)
          Set the parent of this application context.
 java.lang.String toString()
          Return information about this context.
 
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
getResource, getResourceByPath
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.core.io.ResourceLoader
getResource
 

Field Detail

MESSAGE_SOURCE_BEAN_NAME

public static final java.lang.String MESSAGE_SOURCE_BEAN_NAME
Name of the MessageSource bean in the factory. If none is supplied, message resolution is delegated to the parent.

See Also:
MessageSource, Constant Field Values

logger

protected final org.apache.commons.logging.Log logger
Log4j logger used by this class. Available to subclasses.

Constructor Detail

AbstractApplicationContext

public AbstractApplicationContext()
Create a new AbstractApplicationContext with no parent.


AbstractApplicationContext

public AbstractApplicationContext(ApplicationContext parent)
Create a new AbstractApplicationContext with the given parent context.

Parameters:
parent - parent context
Method Detail

getParent

public ApplicationContext getParent()
Return the parent context, or null if there is no parent, and this is the root of the context hierarchy.

Specified by:
getParent in interface ApplicationContext
Returns:
the parent context, or null if there is no parent

setDisplayName

protected void setDisplayName(java.lang.String displayName)
To avoid endless constructor chaining, only concrete classes take this in their constructor, and then invoke this method


getDisplayName

public java.lang.String getDisplayName()
Return a friendly name for context

Specified by:
getDisplayName in interface ApplicationContext
Returns:
a display name for the context

getStartupDate

public long getStartupDate()
Return the timestamp when this context was first loaded

Specified by:
getStartupDate in interface ApplicationContext
Returns:
the timestamp (ms) when this context was first loaded

publishEvent

public void publishEvent(ApplicationEvent event)
Publish the given event to all listeners.

Note: Listeners get initialized after the message source, to be able to access it within listener implementations. Thus, message source implementation cannot publish events.

Specified by:
publishEvent in interface ApplicationContext
Parameters:
event - event to publish. The event may be application-specific, or a standard framework event.
See Also:
RequestHandledEvent

setParent

public void setParent(ApplicationContext parent)
Description copied from interface: ConfigurableApplicationContext
Set the parent of this application context.

Note that the parent shouldn't be changed: It should only be set outside a constructor if it isn't available when an object of this class is created, for example in case of WebApplicationContext setup.

Specified by:
setParent in interface ConfigurableApplicationContext
Parameters:
parent - the parent context
See Also:
ConfigurableWebApplicationContext

addBeanFactoryPostProcessor

public void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor)
Description copied from interface: ConfigurableApplicationContext
Add a new BeanFactoryPostProcessor that will get applied to the internal bean factory of this application context on refresh, before any of the bean definitions get evaluated. To be invoked during context configuration.

Specified by:
addBeanFactoryPostProcessor in interface ConfigurableApplicationContext
Parameters:
beanFactoryPostProcessor - the factory processor to register

getBeanFactoryPostProcessors

public java.util.List getBeanFactoryPostProcessors()
Return the list of BeanPostProcessors that will get applied to beans created with this factory.


refresh

public void refresh()
             throws BeansException
Load or reload configuration.

Specified by:
refresh in interface ConfigurableApplicationContext
Throws:
ApplicationContextException - if the configuration was invalid or couldn't be found, or if configuration has already been loaded and reloading is forbidden
BeansException - if the bean factory could not be initialized

postProcessBeanFactory

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
                               throws BeansException
Modify the application context's internal bean factory after its standard initialization. All bean definitions will have been loaded, but no beans will have been instantiated yet. This allows for registering special BeanPostProcessors etc in certain ApplicationContext implementations.

Parameters:
beanFactory - the bean factory used by the application context
Throws:
BeansException - in case of errors

onRefresh

protected void onRefresh()
                  throws BeansException
Template method which can be overridden to add context-specific refresh work. Called on initialization of special beans, before instantiation of singletons.

Throws:
BeansException - in case of errors during refresh
See Also:
refresh()

addListener

protected void addListener(ApplicationListener listener)
Subclasses can invoke this method to register a listener. Any beans in the context that are listeners are automatically added.

Parameters:
listener - the listener to register

close

public void close()
Destroy the singletons in the bean factory of this application context.

Specified by:
close in interface ConfigurableApplicationContext

getBean

public java.lang.Object getBean(java.lang.String name)
                         throws BeansException
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. This method allows a bean factory to be used as a replacement for the Singleton or Prototype design pattern.

Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getBean in interface BeanFactory
Parameters:
name - name of the bean to return
Returns:
the instance of the bean
Throws:
BeansException - if the bean could not be created

getBean

public java.lang.Object getBean(java.lang.String name,
                                java.lang.Class requiredType)
                         throws BeansException
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. Provides a measure of type safety by throwing an exception if the bean is not of the required type.

Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getBean in interface BeanFactory
Parameters:
name - name of the bean to return
requiredType - type the bean may match. Can be an interface or superclass of the actual class. For example, if the value is Object.class, this method will succeed whatever the class of the returned instance.
Returns:
the instance of the bean
Throws:
BeansException - if the bean could not be created

containsBean

public boolean containsBean(java.lang.String name)
Description copied from interface: BeanFactory
Does this bean factory contain a bean with the given name?

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
containsBean in interface BeanFactory
Parameters:
name - name of the bean to query
Returns:
whether a bean with the given name is defined

isSingleton

public boolean isSingleton(java.lang.String name)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Is this bean a singleton? That is, will getBean() always return the same object?

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
isSingleton in interface BeanFactory
Parameters:
name - name of the bean to query
Returns:
is this bean a singleton
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name

getAliases

public java.lang.String[] getAliases(java.lang.String name)
                              throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Return the aliases for the given bean name, if defined.

Will ask the parent factory if the bean cannot be found in this factory instance.

Specified by:
getAliases in interface BeanFactory
Parameters:
name - the bean name to check for aliases
Returns:
the aliases, or an empty array if none
Throws:
NoSuchBeanDefinitionException - if there's no such bean definition

getBeanDefinitionCount

public int getBeanDefinitionCount()
Description copied from interface: ListableBeanFactory
Return the number of beans defined in the factory. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeanDefinitionCount in interface ListableBeanFactory
Returns:
the number of beans defined in the factory

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames()
Description copied from interface: ListableBeanFactory
Return the names of all beans defined in this factory. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Returns:
the names of all beans defined in this factory, or an empty array if none defined

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
Description copied from interface: ListableBeanFactory
Return the names of beans matching the given type (including subclasses), judging from the bean definitions. Will not consider FactoryBeans, as the type of their created objects is not known before instantiation. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Parameters:
type - class or interface to match, or null for all bean names
Returns:
the names of beans matching the given object type (including subclasses), or an empty array if none

containsBeanDefinition

public boolean containsBeanDefinition(java.lang.String name)
Description copied from interface: ListableBeanFactory
Check if this bean factory contains a bean definition with the given name. Does not consider any hierarchy this factory may participate in.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
containsBeanDefinition in interface ListableBeanFactory
Parameters:
name - the name of the bean to look for
Returns:
if this bean factory contains a bean definition with the given name

getBeansOfType

public java.util.Map getBeansOfType(java.lang.Class type,
                                    boolean includePrototypes,
                                    boolean includeFactoryBeans)
                             throws BeansException
Description copied from interface: ListableBeanFactory
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType() in the case of FactoryBeans. Does not consider any hierarchy this factory may participate in.

If FactoryBean's getObjectType() returns null and the bean is a singleton, the type of the actually created objects should be evaluated. Prototypes without explicit object type specification should be ignored.

Note: Ignores any singleton beans that have been registered by other means than bean definitions.

Specified by:
getBeansOfType in interface ListableBeanFactory
Parameters:
type - class or interface to match
includePrototypes - whether to include prototype beans too or just singletons (also applies to FactoryBeans)
includeFactoryBeans - whether to include FactoryBeans too or just normal beans
Returns:
a Map with the matching beans, containing the bean names as keys and the corresponding bean instances as values
Throws:
BeansException - if the beans could not be created

getParentBeanFactory

public BeanFactory getParentBeanFactory()
Description copied from interface: HierarchicalBeanFactory
Return the parent bean factory, or null if there is none.

Specified by:
getParentBeanFactory in interface HierarchicalBeanFactory

getMessage

public java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args,
                                   java.lang.String defaultMessage,
                                   java.util.Locale locale)
Description copied from interface: MessageSource
Try to resolve the message. Return default message if no message was found.

Specified by:
getMessage in interface MessageSource
Parameters:
code - code to lookup up, such as 'calculator.noRateSet'. Users of this class are encouraged to base message names on the relevant fully qualified class name, thus avoiding conflict and ensuring maximum clarity.
args - array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
locale - the Locale in which to do lookup
defaultMessage - String to return if the lookup fails
Returns:
the resolved message if the lookup is successful; otherwise the default message passed as a parameter
See Also:
java.text.MessageFormat

getMessage

public java.lang.String getMessage(java.lang.String code,
                                   java.lang.Object[] args,
                                   java.util.Locale locale)
                            throws NoSuchMessageException
Description copied from interface: MessageSource
Try to resolve the message. Treat as an error if the message can't be found.

Specified by:
getMessage in interface MessageSource
Parameters:
code - code to lookup up, such as 'calculator.noRateSet'
args - Array of arguments that will be filled in for params within the message (params look like "{0}", "{1,date}", "{2,time}" within a message), or null if none.
locale - Locale in which to do lookup
Returns:
the resolved message
Throws:
NoSuchMessageException - if not found in any locale
See Also:
java.text.MessageFormat

getMessage

public java.lang.String getMessage(MessageSourceResolvable resolvable,
                                   java.util.Locale locale)
                            throws NoSuchMessageException
Description copied from interface: MessageSource
Try to resolve the message using all the attributes contained within the MessageSourceResolvable argument that was passed in.

NOTE: We must throw a NoSuchMessageException on this method since at the time of calling this method we aren't able to determine if the defaultMessage property of the resolvable is null or not.

Specified by:
getMessage in interface MessageSource
Parameters:
resolvable - value object storing attributes required to properly resolve a message
locale - Locale to be used as the "driver" to figuring out what message to return
Returns:
the resolved message
Throws:
NoSuchMessageException - if not found in any locale
See Also:
java.text.MessageFormat

toString

public java.lang.String toString()
Return information about this context.


refreshBeanFactory

protected abstract void refreshBeanFactory()
                                    throws BeansException
Subclasses must implement this method to perform the actual configuration load. The method is invoked by refresh before any other initialization work.

Throws:
BeansException
See Also:
refresh()

getBeanFactory

public abstract ConfigurableListableBeanFactory getBeanFactory()
Subclasses must return their internal bean factory here. They should implement the lookup efficiently, so that it can be called repeatedly without a performance penalty.

Specified by:
getBeanFactory in interface ConfigurableApplicationContext
Returns:
this application context's internal bean factory
See Also:
ConfigurableApplicationContext.refresh(), ConfigurableApplicationContext.addBeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor)


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