spring-context

org.springframework.context.support
Class AbstractApplicationContext

java.lang.Object
  extended by org.springframework.core.io.DefaultResourceLoader
      extended by org.springframework.context.support.AbstractApplicationContext
All Implemented Interfaces:
org.springframework.beans.factory.BeanFactory, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.HierarchicalBeanFactory, org.springframework.beans.factory.ListableBeanFactory, ApplicationContext, ApplicationEventPublisher, ConfigurableApplicationContext, Lifecycle, MessageSource, org.springframework.core.env.EnvironmentCapable, org.springframework.core.io.ResourceLoader, org.springframework.core.io.support.ResourcePatternResolver
Direct Known Subclasses:
AbstractRefreshableApplicationContext, GenericApplicationContext

public abstract class AbstractApplicationContext
extends org.springframework.core.io.DefaultResourceLoader
implements ConfigurableApplicationContext, org.springframework.beans.factory.DisposableBean

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

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

A MessageSource may also be supplied as a bean in the context, with the name "messageSource"; otherwise, message resolution is delegated to the parent context. Furthermore, a multicaster for application events can be supplied as "applicationEventMulticaster" bean of type ApplicationEventMulticaster in the context; otherwise, a default multicaster of type SimpleApplicationEventMulticaster will be used.

Implements resource loading through extending DefaultResourceLoader. Consequently treats non-URL resource paths as class path resources (supporting full class path resource names that include the package path, e.g. "mypackage/myresource.dat"), unless the DefaultResourceLoader.getResourceByPath(java.lang.String) method is overwritten in a subclass.

Since:
January 21, 2001
Author:
Rod Johnson, Juergen Hoeller, Mark Fisher
See Also:
refreshBeanFactory(), getBeanFactory(), BeanFactoryPostProcessor, BeanPostProcessor, ApplicationEventMulticaster, ApplicationListener, MessageSource

Field Summary
static java.lang.String APPLICATION_EVENT_MULTICASTER_BEAN_NAME
          Name of the ApplicationEventMulticaster bean in the factory.
static java.lang.String LIFECYCLE_PROCESSOR_BEAN_NAME
          Name of the LifecycleProcessor bean in the factory.
protected  org.apache.commons.logging.Log logger
          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.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
AbstractApplicationContext()
          Create a new AbstractApplicationContext with no parent.
AbstractApplicationContext(ApplicationContext parent)
          Create a new AbstractApplicationContext with the given parent context.
 
Method Summary
 void addApplicationListener(ApplicationListener<?> listener)
          Add a new ApplicationListener that will be notified on context events such as context refresh and context shutdown.
 void addBeanFactoryPostProcessor(org.springframework.beans.factory.config.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)
          Deprecated. as of Spring 3.0, in favor of addApplicationListener(org.springframework.context.ApplicationListener)
protected  void cancelRefresh(org.springframework.beans.BeansException ex)
          Cancel this context's refresh attempt, resetting the active flag after an exception got thrown.
 void close()
          Close this application context, destroying all beans in its bean factory.
protected abstract  void closeBeanFactory()
          Subclasses must implement this method to release their internal bean factory.
 boolean containsBean(java.lang.String name)
           
 boolean containsBeanDefinition(java.lang.String beanName)
           
 boolean containsLocalBean(java.lang.String name)
           
protected  org.springframework.core.env.ConfigurableEnvironment createEnvironment()
          Create and return a new StandardEnvironment.
 void destroy()
          DisposableBean callback for destruction of this instance.
protected  void destroyBeans()
          Template method for destroying all beans that this context manages.
protected  void doClose()
          Actually performs context closing: publishes a ContextClosedEvent and destroys the singletons in the bean factory of this application context.
<A extends java.lang.annotation.Annotation>
A
findAnnotationOnBean(java.lang.String beanName, java.lang.Class<A> annotationType)
           
protected  void finishBeanFactoryInitialization(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
          Finish the initialization of this context's bean factory, initializing all remaining singleton beans.
protected  void finishRefresh()
          Finish the refresh of this context, invoking the LifecycleProcessor's onRefresh() method and publishing the ContextRefreshedEvent.
 java.lang.String[] getAliases(java.lang.String name)
           
 java.util.Collection<ApplicationListener<?>> getApplicationListeners()
          Return the list of statically specified ApplicationListeners.
 org.springframework.beans.factory.config.AutowireCapableBeanFactory getAutowireCapableBeanFactory()
          Return this context's internal bean factory as AutowireCapableBeanFactory, if already available.
<T> T
getBean(java.lang.Class<T> requiredType)
           
 java.lang.Object getBean(java.lang.String name)
           
<T> T
getBean(java.lang.String name, java.lang.Class<T> requiredType)
           
 java.lang.Object getBean(java.lang.String name, java.lang.Object... args)
           
 int getBeanDefinitionCount()
           
 java.lang.String[] getBeanDefinitionNames()
           
abstract  org.springframework.beans.factory.config.ConfigurableListableBeanFactory getBeanFactory()
          Subclasses must return their internal bean factory here.
 java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor> getBeanFactoryPostProcessors()
          Return the list of BeanFactoryPostProcessors that will get applied to the internal BeanFactory.
 java.lang.String[] getBeanNamesForType(java.lang.Class<?> type)
           
 java.lang.String[] getBeanNamesForType(java.lang.Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)
           
<T> java.util.Map<java.lang.String,T>
getBeansOfType(java.lang.Class<T> type)
           
<T> java.util.Map<java.lang.String,T>
getBeansOfType(java.lang.Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
           
 java.util.Map<java.lang.String,java.lang.Object> getBeansWithAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
           
 java.lang.String getDisplayName()
          Return a friendly name for this context.
 org.springframework.core.env.ConfigurableEnvironment getEnvironment()
          Return the Environment for this application context in configurable form.
 java.lang.String getId()
          Return the unique id of this application context.
protected  org.springframework.beans.factory.BeanFactory getInternalParentBeanFactory()
          Return the internal bean factory of the parent context if it implements ConfigurableApplicationContext; else, return the parent context itself.
protected  MessageSource getInternalParentMessageSource()
          Return the internal message source of the parent context if it is an AbstractApplicationContext too; else, return the parent context itself.
 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 (that is, this context is the root of the context hierarchy).
 org.springframework.beans.factory.BeanFactory getParentBeanFactory()
           
protected  org.springframework.core.io.support.ResourcePatternResolver getResourcePatternResolver()
          Return the ResourcePatternResolver to use for resolving location patterns into Resource instances.
 org.springframework.core.io.Resource[] getResources(java.lang.String locationPattern)
           
 long getStartupDate()
          Return the timestamp (ms) when this context was first loaded.
 java.lang.Class<?> getType(java.lang.String name)
           
protected  void initApplicationEventMulticaster()
          Initialize the ApplicationEventMulticaster.
protected  void initLifecycleProcessor()
          Initialize the LifecycleProcessor.
protected  void initMessageSource()
          Initialize the MessageSource.
protected  void initPropertySources()
          Replace any stub property sources with actual instances.
protected  void invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
          Instantiate and invoke all registered BeanFactoryPostProcessor beans, respecting explicit order if given.
 boolean isActive()
          Determine whether this application context is active, that is, whether it has been refreshed at least once and has not been closed yet.
 boolean isPrototype(java.lang.String name)
           
 boolean isRunning()
          Check whether this component is currently running.
 boolean isSingleton(java.lang.String name)
           
 boolean isTypeMatch(java.lang.String name, java.lang.Class<?> targetType)
           
protected  org.springframework.beans.factory.config.ConfigurableListableBeanFactory obtainFreshBeanFactory()
          Tell the subclass to refresh the internal bean factory.
protected  void onClose()
          Template method which can be overridden to add context-specific shutdown work.
protected  void onRefresh()
          Template method which can be overridden to add context-specific refresh work.
protected  void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
          Modify the application context's internal bean factory after its standard initialization.
protected  void prepareBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
          Configure the factory's standard context characteristics, such as the context's ClassLoader and post-processors.
protected  void prepareRefresh()
          Prepare this context for refreshing, setting its startup date and active flag as well as performing any initialization of property sources.
 void publishEvent(ApplicationEvent event)
          Publish the given event to all listeners.
 void refresh()
          Load or refresh the persistent representation of the configuration, which might an XML file, properties file, or relational database schema.
protected abstract  void refreshBeanFactory()
          Subclasses must implement this method to perform the actual configuration load.
protected  void registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
          Instantiate and invoke all registered BeanPostProcessor beans, respecting explicit order if given.
protected  void registerListeners()
          Add beans that implement ApplicationListener as listeners.
 void registerShutdownHook()
          Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.
 void setDisplayName(java.lang.String displayName)
          Set a friendly name for this context.
 void setEnvironment(org.springframework.core.env.ConfigurableEnvironment environment)
          Set the Environment for this application context.
 void setId(java.lang.String id)
          Set the unique id of this application context.
 void setParent(ApplicationContext parent)
          Set the parent of this application context.
 void start()
          Start this component.
 void stop()
          Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.
 java.lang.String toString()
          Return information about this context.
 
Methods inherited from class org.springframework.core.io.DefaultResourceLoader
getClassLoader, getResource, 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.core.io.ResourceLoader
getClassLoader, 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

LIFECYCLE_PROCESSOR_BEAN_NAME

public static final java.lang.String LIFECYCLE_PROCESSOR_BEAN_NAME
Name of the LifecycleProcessor bean in the factory. If none is supplied, a DefaultLifecycleProcessor is used.

See Also:
LifecycleProcessor, DefaultLifecycleProcessor, Constant Field Values

APPLICATION_EVENT_MULTICASTER_BEAN_NAME

public static final java.lang.String APPLICATION_EVENT_MULTICASTER_BEAN_NAME
Name of the ApplicationEventMulticaster bean in the factory. If none is supplied, a default SimpleApplicationEventMulticaster is used.

See Also:
ApplicationEventMulticaster, SimpleApplicationEventMulticaster, Constant Field Values

logger

protected final org.apache.commons.logging.Log logger
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 - the parent context
Method Detail

setId

public void setId(java.lang.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.

Specified by:
setId in interface ConfigurableApplicationContext
Parameters:
id - the unique id of the context

getId

public java.lang.String getId()
Return the unique id of this application context.

Specified by:
getId in interface ApplicationContext
Returns:
the unique id of the context, or null if none

setDisplayName

public void setDisplayName(java.lang.String displayName)
Set a friendly name for this context. Typically done during initialization of concrete context implementations.

Default is the object id of the context instance.


getDisplayName

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

Specified by:
getDisplayName in interface ApplicationContext
Returns:
a display name for this context (never null)

getParent

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

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

getEnvironment

public org.springframework.core.env.ConfigurableEnvironment getEnvironment()
Description copied from interface: ConfigurableApplicationContext
Return the Environment for this application context in configurable form.

Specified by:
getEnvironment in interface ConfigurableApplicationContext
Specified by:
getEnvironment in interface org.springframework.core.env.EnvironmentCapable

setEnvironment

public void setEnvironment(org.springframework.core.env.ConfigurableEnvironment environment)
Set the Environment for this application context.

Default value is determined by createEnvironment(). Replacing the default with this method is one option but configuration through getEnvironment() should also be considered. In either case, such modifications should be performed before refresh().

Specified by:
setEnvironment in interface ConfigurableApplicationContext
Parameters:
environment - the new environment
See Also:
createEnvironment()

getAutowireCapableBeanFactory

public org.springframework.beans.factory.config.AutowireCapableBeanFactory getAutowireCapableBeanFactory()
                                                                                                  throws java.lang.IllegalStateException
Return this context's internal bean factory as AutowireCapableBeanFactory, if already available.

Specified by:
getAutowireCapableBeanFactory in interface ApplicationContext
Returns:
the AutowireCapableBeanFactory for this context
Throws:
java.lang.IllegalStateException - if the context does not support the AutowireCapableBeanFactory interface or does not hold an autowire-capable bean factory yet (usually if refresh() has never been called)
See Also:
getBeanFactory()

getStartupDate

public long getStartupDate()
Return the timestamp (ms) 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 MessageSource, to be able to access it within listener implementations. Thus, MessageSource implementations cannot publish events.

Specified by:
publishEvent in interface ApplicationEventPublisher
Parameters:
event - the event to publish (may be application-specific or a standard framework event)
See Also:
org.springframework.web.context.support.RequestHandledEvent

getResourcePatternResolver

protected org.springframework.core.io.support.ResourcePatternResolver getResourcePatternResolver()
Return the ResourcePatternResolver to use for resolving location patterns into Resource instances. Default is a PathMatchingResourcePatternResolver, supporting Ant-style location patterns.

Can be overridden in subclasses, for extended resolution strategies, for example in a web environment.

Do not call this when needing to resolve a location pattern. Call the context's getResources method instead, which will delegate to the ResourcePatternResolver.

Returns:
the ResourcePatternResolver for this context
See Also:
getResources(java.lang.String), PathMatchingResourcePatternResolver

setParent

public void setParent(ApplicationContext parent)
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.

The parent environment is merged with this (child) application context environment if the parent is non-null and its environment is an instance of ConfigurableEnvironment.

Specified by:
setParent in interface ConfigurableApplicationContext
Parameters:
parent - the parent context
See Also:
ConfigurableEnvironment.merge(ConfigurableEnvironment)

addBeanFactoryPostProcessor

public void addBeanFactoryPostProcessor(org.springframework.beans.factory.config.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<org.springframework.beans.factory.config.BeanFactoryPostProcessor> getBeanFactoryPostProcessors()
Return the list of BeanFactoryPostProcessors that will get applied to the internal BeanFactory.


addApplicationListener

public void addApplicationListener(ApplicationListener<?> listener)
Description copied from interface: ConfigurableApplicationContext
Add a new ApplicationListener that will be notified on context events such as context refresh and context shutdown.

Note that any ApplicationListener registered here will be applied on refresh if the context is not active yet, or on the fly with the current event multicaster in case of a context that is already active.

Specified by:
addApplicationListener in interface ConfigurableApplicationContext
Parameters:
listener - the ApplicationListener to register
See Also:
ContextRefreshedEvent, ContextClosedEvent

getApplicationListeners

public java.util.Collection<ApplicationListener<?>> getApplicationListeners()
Return the list of statically specified ApplicationListeners.


createEnvironment

protected org.springframework.core.env.ConfigurableEnvironment createEnvironment()
Create and return a new StandardEnvironment.

Subclasses may override this method in order to supply a custom ConfigurableEnvironment implementation.


refresh

public void refresh()
             throws org.springframework.beans.BeansException,
                    java.lang.IllegalStateException
Description copied from interface: ConfigurableApplicationContext
Load or refresh the persistent representation of the configuration, which might an XML file, properties file, or relational database schema.

As this is a startup method, it should destroy already created singletons if it fails, to avoid dangling resources. In other words, after invocation of that method, either all or no singletons at all should be instantiated.

Specified by:
refresh in interface ConfigurableApplicationContext
Throws:
org.springframework.beans.BeansException - if the bean factory could not be initialized
java.lang.IllegalStateException - if already initialized and multiple refresh attempts are not supported

prepareRefresh

protected void prepareRefresh()
Prepare this context for refreshing, setting its startup date and active flag as well as performing any initialization of property sources.


initPropertySources

protected void initPropertySources()

Replace any stub property sources with actual instances.

See Also:
PropertySource.StubPropertySource, org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources

obtainFreshBeanFactory

protected org.springframework.beans.factory.config.ConfigurableListableBeanFactory obtainFreshBeanFactory()
Tell the subclass to refresh the internal bean factory.

Returns:
the fresh BeanFactory instance
See Also:
refreshBeanFactory(), getBeanFactory()

prepareBeanFactory

protected void prepareBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Configure the factory's standard context characteristics, such as the context's ClassLoader and post-processors.

Parameters:
beanFactory - the BeanFactory to configure

postProcessBeanFactory

protected void postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
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

invokeBeanFactoryPostProcessors

protected void invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Instantiate and invoke all registered BeanFactoryPostProcessor beans, respecting explicit order if given.

Must be called before singleton instantiation.


registerBeanPostProcessors

protected void registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Instantiate and invoke all registered BeanPostProcessor beans, respecting explicit order if given.

Must be called before any instantiation of application beans.


initMessageSource

protected void initMessageSource()
Initialize the MessageSource. Use parent's if none defined in this context.


initApplicationEventMulticaster

protected void initApplicationEventMulticaster()
Initialize the ApplicationEventMulticaster. Uses SimpleApplicationEventMulticaster if none defined in the context.

See Also:
SimpleApplicationEventMulticaster

initLifecycleProcessor

protected void initLifecycleProcessor()
Initialize the LifecycleProcessor. Uses DefaultLifecycleProcessor if none defined in the context.

See Also:
DefaultLifecycleProcessor

onRefresh

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

This implementation is empty.

Throws:
org.springframework.beans.BeansException - in case of errors
See Also:
refresh()

registerListeners

protected void registerListeners()
Add beans that implement ApplicationListener as listeners. Doesn't affect other listeners, which can be added without being beans.


addListener

@Deprecated
protected void addListener(ApplicationListener<?> listener)
Deprecated. as of Spring 3.0, in favor of addApplicationListener(org.springframework.context.ApplicationListener)

Subclasses can invoke this method to register a listener. Any beans in the context that are listeners are automatically added.

Note: This method only works within an active application context, i.e. when an ApplicationEventMulticaster is already available. Generally prefer the use of addApplicationListener(org.springframework.context.ApplicationListener) which is more flexible.

Parameters:
listener - the listener to register

finishBeanFactoryInitialization

protected void finishBeanFactoryInitialization(org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory)
Finish the initialization of this context's bean factory, initializing all remaining singleton beans.


finishRefresh

protected void finishRefresh()
Finish the refresh of this context, invoking the LifecycleProcessor's onRefresh() method and publishing the ContextRefreshedEvent.


cancelRefresh

protected void cancelRefresh(org.springframework.beans.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

registerShutdownHook

public void registerShutdownHook()
Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Delegates to doClose() for the actual closing procedure.

Specified by:
registerShutdownHook in interface ConfigurableApplicationContext
See Also:
Runtime.addShutdownHook(java.lang.Thread), close(), doClose()

destroy

public void destroy()
DisposableBean callback for destruction of this instance. Only called when the ApplicationContext itself is running as a bean in another BeanFactory or ApplicationContext, which is rather unusual.

The close method is the native way to shut down an ApplicationContext.

Specified by:
destroy in interface org.springframework.beans.factory.DisposableBean
See Also:
close(), SingletonBeanFactoryLocator

close

public void close()
Close this application context, destroying all beans in its bean factory.

Delegates to doClose() for the actual closing procedure. Also removes a JVM shutdown hook, if registered, as it's not needed anymore.

Specified by:
close in interface ConfigurableApplicationContext
See Also:
doClose(), registerShutdownHook()

doClose

protected void doClose()
Actually performs context closing: publishes a ContextClosedEvent and destroys the singletons in the bean factory of this application context.

Called by both close() and a JVM shutdown hook, if any.

See Also:
ContextClosedEvent, destroyBeans(), close(), registerShutdownHook()

destroyBeans

protected void destroyBeans()
Template method for destroying all beans that this context manages. The default implementation destroy all cached singletons in this context, invoking DisposableBean.destroy() and/or the specified "destroy-method".

Can be overridden to add context-specific bean destruction steps right before or right after standard singleton destruction, while the context's BeanFactory is still active.

See Also:
getBeanFactory(), ConfigurableBeanFactory.destroySingletons()

onClose

protected void onClose()
Template method which can be overridden to add context-specific shutdown work. The default implementation is empty.

Called at the end of doClose()'s shutdown procedure, after this context's BeanFactory has been closed. If custom shutdown logic needs to execute while the BeanFactory is still active, override the destroyBeans() method instead.


isActive

public boolean isActive()
Description copied from interface: ConfigurableApplicationContext
Determine whether this application context is active, that is, whether it has been refreshed at least once and has not been closed yet.

Specified by:
isActive in interface ConfigurableApplicationContext
Returns:
whether the context is still active
See Also:
ConfigurableApplicationContext.refresh(), ConfigurableApplicationContext.close(), ConfigurableApplicationContext.getBeanFactory()

getBean

public java.lang.Object getBean(java.lang.String name)
                         throws org.springframework.beans.BeansException
Specified by:
getBean in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.BeansException

getBean

public <T> T getBean(java.lang.String name,
                     java.lang.Class<T> requiredType)
          throws org.springframework.beans.BeansException
Specified by:
getBean in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.BeansException

getBean

public <T> T getBean(java.lang.Class<T> requiredType)
          throws org.springframework.beans.BeansException
Specified by:
getBean in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.BeansException

getBean

public java.lang.Object getBean(java.lang.String name,
                                java.lang.Object... args)
                         throws org.springframework.beans.BeansException
Specified by:
getBean in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.BeansException

containsBean

public boolean containsBean(java.lang.String name)
Specified by:
containsBean in interface org.springframework.beans.factory.BeanFactory

isSingleton

public boolean isSingleton(java.lang.String name)
                    throws org.springframework.beans.factory.NoSuchBeanDefinitionException
Specified by:
isSingleton in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.factory.NoSuchBeanDefinitionException

isPrototype

public boolean isPrototype(java.lang.String name)
                    throws org.springframework.beans.factory.NoSuchBeanDefinitionException
Specified by:
isPrototype in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.factory.NoSuchBeanDefinitionException

isTypeMatch

public boolean isTypeMatch(java.lang.String name,
                           java.lang.Class<?> targetType)
                    throws org.springframework.beans.factory.NoSuchBeanDefinitionException
Specified by:
isTypeMatch in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.factory.NoSuchBeanDefinitionException

getType

public java.lang.Class<?> getType(java.lang.String name)
                           throws org.springframework.beans.factory.NoSuchBeanDefinitionException
Specified by:
getType in interface org.springframework.beans.factory.BeanFactory
Throws:
org.springframework.beans.factory.NoSuchBeanDefinitionException

getAliases

public java.lang.String[] getAliases(java.lang.String name)
Specified by:
getAliases in interface org.springframework.beans.factory.BeanFactory

containsBeanDefinition

public boolean containsBeanDefinition(java.lang.String beanName)
Specified by:
containsBeanDefinition in interface org.springframework.beans.factory.ListableBeanFactory

getBeanDefinitionCount

public int getBeanDefinitionCount()
Specified by:
getBeanDefinitionCount in interface org.springframework.beans.factory.ListableBeanFactory

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames()
Specified by:
getBeanDefinitionNames in interface org.springframework.beans.factory.ListableBeanFactory

getBeanNamesForType

public java.lang.String[] getBeanNamesForType(java.lang.Class<?> type)
Specified by:
getBeanNamesForType in interface org.springframework.beans.factory.ListableBeanFactory

getBeanNamesForType

public java.lang.String[] getBeanNamesForType(java.lang.Class<?> type,
                                              boolean includeNonSingletons,
                                              boolean allowEagerInit)
Specified by:
getBeanNamesForType in interface org.springframework.beans.factory.ListableBeanFactory

getBeansOfType

public <T> java.util.Map<java.lang.String,T> getBeansOfType(java.lang.Class<T> type)
                                                 throws org.springframework.beans.BeansException
Specified by:
getBeansOfType in interface org.springframework.beans.factory.ListableBeanFactory
Throws:
org.springframework.beans.BeansException

getBeansOfType

public <T> java.util.Map<java.lang.String,T> getBeansOfType(java.lang.Class<T> type,
                                                            boolean includeNonSingletons,
                                                            boolean allowEagerInit)
                                                 throws org.springframework.beans.BeansException
Specified by:
getBeansOfType in interface org.springframework.beans.factory.ListableBeanFactory
Throws:
org.springframework.beans.BeansException

getBeansWithAnnotation

public java.util.Map<java.lang.String,java.lang.Object> getBeansWithAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
                                                                        throws org.springframework.beans.BeansException
Specified by:
getBeansWithAnnotation in interface org.springframework.beans.factory.ListableBeanFactory
Throws:
org.springframework.beans.BeansException

findAnnotationOnBean

public <A extends java.lang.annotation.Annotation> A findAnnotationOnBean(java.lang.String beanName,
                                                                          java.lang.Class<A> annotationType)
Specified by:
findAnnotationOnBean in interface org.springframework.beans.factory.ListableBeanFactory

getParentBeanFactory

public org.springframework.beans.factory.BeanFactory getParentBeanFactory()
Specified by:
getParentBeanFactory in interface org.springframework.beans.factory.HierarchicalBeanFactory

containsLocalBean

public boolean containsLocalBean(java.lang.String name)
Specified by:
containsLocalBean in interface org.springframework.beans.factory.HierarchicalBeanFactory

getInternalParentBeanFactory

protected org.springframework.beans.factory.BeanFactory getInternalParentBeanFactory()
Return the internal bean factory of the parent context if it implements ConfigurableApplicationContext; else, return the parent context itself.

See Also:
ConfigurableApplicationContext.getBeanFactory()

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 - the 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.
defaultMessage - String to return if the lookup fails
locale - the Locale in which to do the lookup
Returns:
the resolved message if the lookup was successful; otherwise the default message passed as a parameter
See Also:
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 - the 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 - the Locale in which to do the lookup
Returns:
the resolved message
Throws:
NoSuchMessageException - if the message wasn't found
See Also:
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 - the Locale in which to do the lookup
Returns:
the resolved message
Throws:
NoSuchMessageException - if the message wasn't found
See Also:
MessageFormat

getInternalParentMessageSource

protected MessageSource getInternalParentMessageSource()
Return the internal message source of the parent context if it is an AbstractApplicationContext too; else, return the parent context itself.


getResources

public org.springframework.core.io.Resource[] getResources(java.lang.String locationPattern)
                                                    throws java.io.IOException
Specified by:
getResources in interface org.springframework.core.io.support.ResourcePatternResolver
Throws:
java.io.IOException

start

public void start()
Description copied from interface: Lifecycle
Start this component. Should not throw an exception if the component is already running.

In the case of a container, this will propagate the start signal to all components that apply.

Specified by:
start in interface Lifecycle

stop

public void stop()
Description copied from interface: Lifecycle
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing SmartLifecycle and its stop(Runnable) variant in cases where asynchronous stop behavior is necessary.

Should not throw an exception if the component isn't started yet.

In the case of a container, this will propagate the stop signal to all components that apply.

Specified by:
stop in interface Lifecycle
See Also:
SmartLifecycle.stop(Runnable)

isRunning

public boolean isRunning()
Description copied from interface: Lifecycle
Check whether this component is currently running.

In the case of a container, this will return true only if all components that apply are currently running.

Specified by:
isRunning in interface Lifecycle
Returns:
whether the component is currently running

refreshBeanFactory

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

A subclass will either create a new bean factory and hold a reference to it, or return a single BeanFactory instance that it holds. In the latter case, it will usually throw an IllegalStateException if refreshing the context more than once.

Throws:
org.springframework.beans.BeansException - if initialization of the bean factory failed
java.lang.IllegalStateException - if already initialized and multiple refresh attempts are not supported

closeBeanFactory

protected abstract void closeBeanFactory()
Subclasses must implement this method to release their internal bean factory. This method gets invoked by close() after all other shutdown work.

Should never throw an exception but rather log shutdown failures.


getBeanFactory

public abstract org.springframework.beans.factory.config.ConfigurableListableBeanFactory getBeanFactory()
                                                                                                 throws java.lang.IllegalStateException
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.

Note: Subclasses should check whether the context is still active before returning the internal bean factory. The internal factory should generally be considered unavailable once the context has been closed.

Specified by:
getBeanFactory in interface ConfigurableApplicationContext
Returns:
this application context's internal bean factory (never null)
Throws:
java.lang.IllegalStateException - if the context does not hold an internal bean factory yet (usually if refresh() has never been called) or if the context has been closed already
See Also:
refreshBeanFactory(), closeBeanFactory()

toString

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

Overrides:
toString in class java.lang.Object

spring-context