Package org.springframework.context

# Interface ConfigurableApplicationContext

All Superinterfaces:
  `ApplicationContext`, `ApplicationEventPublisher`, `AutoCloseable`, `org.springframework.beans.factory.BeanFactory`, `Closeable`, `org.springframework.core.env.EnvironmentCapable`, `org.springframework.beans.factory.HierarchicalBeanFactory`, `Lifecycle`, `org.springframework.beans.factory.ListableBeanFactory`, `MessageSource`, `org.springframework.core.io.ResourceLoader`, `org.springframework.core.io.support.ResourcePatternResolver`

All Known Implementing Classes:
  `AbstractApplicationContext`, `AbstractRefreshableApplicationContext`, `AbstractRefreshableConfigApplicationContext`, `AbstractXmlApplicationContext`, `AnnotationConfigApplicationContext`, `ClassPathXmlApplicationContext`, `FileSystemXmlApplicationContext`, `GenericApplicationContext`, `GenericGroovyApplicationContext`, `GenericXmlApplicationContext`, `StaticApplicationContext`

public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable
SPI interface to be implemented by most if not all application contexts. Provides facilities to configure an application context in addition to the application context client methods in the `ApplicationContext` interface.

Configuration and lifecycle methods are encapsulated here to avoid making them obvious to ApplicationContext client code. The present methods should only be used by startup and shutdown code.

Since:
  03.11.2003
Author:
  Juergen Hoeller, Chris Beams, Sam Brannen

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`APPLICATION_STARTUP_BEAN_NAME`
Name of the `ApplicationStartup` bean in the factory.
`static final String`
`BOOTSTRAP_EXECUTOR_BEAN_NAME`
The name of the `bootstrap executor` bean in the context.
`static final String`
`CONFIG_LOCATION_DELIMITERS`
Any number of these characters are considered delimiters between multiple context config paths in a single String value.
`static final String`
`CONVERSION_SERVICE_BEAN_NAME`
Name of the ConversionService bean in the factory.
`static final String`
`ENVIRONMENT_BEAN_NAME`
Name of the `Environment` bean in the factory.
`static final String`
`LOAD_TIME_WEAVER_BEAN_NAME`
Name of the LoadTimeWeaver bean in the factory.
`static final String`
`SHUTDOWN_HOOK_THREAD_NAME`
`Name` of the shutdown hook thread: "SpringContextShutdownHook".
`static final String`
`SYSTEM_ENVIRONMENT_BEAN_NAME`
Name of the System environment bean in the factory.
`static final String`
`SYSTEM_PROPERTIES_BEAN_NAME`
Name of the System properties bean in the factory.

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

`FACTORY_BEAN_PREFIX, FACTORY_BEAN_PREFIX_CHAR`

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

`CLASSPATH_URL_PREFIX`

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

`CLASSPATH_ALL_URL_PREFIX`

## Method Summary

Modifier and Type
Method
Description
`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 postProcessor)`
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.
`void`
`addProtocolResolver(org.springframework.core.io.ProtocolResolver resolver)`
Register the given protocol resolver with this application context, allowing for additional resource protocols to be handled.
`void`
`close()`
Close this application context, releasing all resources and locks that the implementation might hold.
`org.springframework.core.metrics.ApplicationStartup`
`getApplicationStartup()`
Return the `ApplicationStartup` for this application context.
`org.springframework.beans.factory.config.ConfigurableListableBeanFactory`
`getBeanFactory()`
Return the internal bean factory of this application context.
`org.springframework.core.env.ConfigurableEnvironment`
`getEnvironment()`
Return the `Environment` for this application context in configurable form, allowing for further customization.
`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`
`isClosed()`
Return whether this context has been closed already, that is, whether `close()` has been called on an active context in order to initiate its shutdown.
`void`
`refresh()`
Load or refresh the persistent representation of the configuration, which might be from Java-based configuration, an XML file, a properties file, a relational database schema, or some other format.
`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`
`removeApplicationListener(ApplicationListener<?> listener)`
Remove the given ApplicationListener from this context's set of listeners, assuming it got registered via `addApplicationListener(org.springframework.context.ApplicationListener<?>)` before.
`void`
`setApplicationStartup(org.springframework.core.metrics.ApplicationStartup applicationStartup)`
Set the `ApplicationStartup` for this application context.
`void`
`setClassLoader(ClassLoader classLoader)`
Specify the ClassLoader to load class path resources and bean classes with.
`void`
`setEnvironment(org.springframework.core.env.ConfigurableEnvironment environment)`
Set the `Environment` for this application context.
`void`
`setId(String id)`
Set the unique id of this application context.
`void`
`setParent(ApplicationContext parent)`
Set the parent of this application context.

### Methods inherited from interface org.springframework.context.ApplicationContext

`getApplicationName, getAutowireCapableBeanFactory, getDisplayName, getId, getParent, getStartupDate`

### Methods inherited from interface org.springframework.context.ApplicationEventPublisher

`publishEvent, publishEvent`

### Methods inherited from interface org.springframework.beans.factory.BeanFactory

`containsBean, getAliases, getBean, getBean, getBean, getBean, getBean, getBeanProvider, getBeanProvider, getType, getType, isPrototype, isSingleton, isTypeMatch, isTypeMatch`

### Methods inherited from interface org.springframework.beans.factory.HierarchicalBeanFactory

`containsLocalBean, getParentBeanFactory`

### Methods inherited from interface org.springframework.context.Lifecycle

`isRunning, start, stop`

### Methods inherited from interface org.springframework.beans.factory.ListableBeanFactory

`containsBeanDefinition, findAllAnnotationsOnBean, findAnnotationOnBean, findAnnotationOnBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanProvider, getBeanProvider, getBeansOfType, getBeansOfType, getBeansWithAnnotation`

### Methods inherited from interface org.springframework.context.MessageSource

`getMessage, getMessage, getMessage`

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

`getClassLoader, getResource`

### Methods inherited from interface org.springframework.core.io.support.ResourcePatternResolver

`getResources`

## Field Details

### CONFIG_LOCATION_DELIMITERS

static final String CONFIG_LOCATION_DELIMITERS
Any number of these characters are considered delimiters between multiple context config paths in a single String value.

See Also:
  - `AbstractRefreshableConfigApplicationContext.setConfigLocation(java.lang.String)`
  - `org.springframework.web.context.ContextLoader#CONFIG_LOCATION_PARAM`
  - `org.springframework.web.servlet.FrameworkServlet#setContextConfigLocation`
  - Constant Field Values

### BOOTSTRAP_EXECUTOR_BEAN_NAME

static final String BOOTSTRAP_EXECUTOR_BEAN_NAME
The name of the `bootstrap executor` bean in the context. If none is supplied, no background bootstrapping will be active.

Since:
  6.2
See Also:
  - `Executor`
  - `TaskExecutor`
  - `DefaultListableBeanFactory.setBootstrapExecutor(java.util.concurrent.Executor)`
  - Constant Field Values

### CONVERSION_SERVICE_BEAN_NAME

static final String CONVERSION_SERVICE_BEAN_NAME
Name of the ConversionService bean in the factory. If none is supplied, default conversion rules apply.

Since:
  3.0
See Also:
  - `ConversionService`
  - Constant Field Values

### LOAD_TIME_WEAVER_BEAN_NAME

static final String LOAD_TIME_WEAVER_BEAN_NAME
Name of the LoadTimeWeaver bean in the factory. If such a bean is supplied, the context will use a temporary ClassLoader for type matching, in order to allow the LoadTimeWeaver to process all actual bean classes.

Since:
  2.5
See Also:
  - `LoadTimeWeaver`
  - Constant Field Values

### ENVIRONMENT_BEAN_NAME

static final String ENVIRONMENT_BEAN_NAME
Name of the `Environment` bean in the factory.

Since:
  3.1
See Also:
  - Constant Field Values

### SYSTEM_PROPERTIES_BEAN_NAME

static final String SYSTEM_PROPERTIES_BEAN_NAME
Name of the System properties bean in the factory.

See Also:
  - `System.getProperties()`
  - Constant Field Values

### SYSTEM_ENVIRONMENT_BEAN_NAME

static final String SYSTEM_ENVIRONMENT_BEAN_NAME
Name of the System environment bean in the factory.

See Also:
  - `System.getenv()`
  - Constant Field Values

### APPLICATION_STARTUP_BEAN_NAME

static final String APPLICATION_STARTUP_BEAN_NAME
Name of the `ApplicationStartup` bean in the factory.

Since:
  5.3
See Also:
  - Constant Field Values

### SHUTDOWN_HOOK_THREAD_NAME

static final String SHUTDOWN_HOOK_THREAD_NAME
`Name` of the shutdown hook thread: "SpringContextShutdownHook".

Since:
  5.2
See Also:
  - `registerShutdownHook()`
  - Constant Field Values

## Method Details

### setId

void setId(String id)
Set the unique id of this application context.

Since:
  3.0

### setParent

void setParent(@Nullable 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.

Parameters:
  `parent` - the parent context
See Also:
  - `org.springframework.web.context.ConfigurableWebApplicationContext`

### setEnvironment

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

Parameters:
  `environment` - the new environment
Since:
  3.1

### getEnvironment

org.springframework.core.env.ConfigurableEnvironment getEnvironment()
Return the `Environment` for this application context in configurable form, allowing for further customization.

Specified by:
  `getEnvironment` in interface `org.springframework.core.env.EnvironmentCapable`
Since:
  3.1

### setApplicationStartup

void setApplicationStartup(org.springframework.core.metrics.ApplicationStartup applicationStartup)
Set the `ApplicationStartup` for this application context.

This allows the application context to record metrics during startup.

Parameters:
  `applicationStartup` - the new context event factory
Since:
  5.3

### getApplicationStartup

org.springframework.core.metrics.ApplicationStartup getApplicationStartup()
Return the `ApplicationStartup` for this application context.

Since:
  5.3

### addBeanFactoryPostProcessor

void addBeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor postProcessor)
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.

Parameters:
  `postProcessor` - the factory processor to register

### addApplicationListener

void addApplicationListener(ApplicationListener<?> listener)
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.

Parameters:
  `listener` - the ApplicationListener to register
See Also:
  - `ContextRefreshedEvent`
  - `ContextClosedEvent`

### removeApplicationListener

void removeApplicationListener(ApplicationListener<?> listener)
Remove the given ApplicationListener from this context's set of listeners, assuming it got registered via `addApplicationListener(org.springframework.context.ApplicationListener<?>)` before.

Parameters:
  `listener` - the ApplicationListener to deregister
Since:
  6.0

### setClassLoader

void setClassLoader(ClassLoader classLoader)
Specify the ClassLoader to load class path resources and bean classes with.

This context class loader will be passed to the internal bean factory.

Since:
  5.2.7
See Also:
  - `DefaultResourceLoader(ClassLoader)`
  - `ConfigurableBeanFactory.setBeanClassLoader(java.lang.ClassLoader)`

### addProtocolResolver

void addProtocolResolver(org.springframework.core.io.ProtocolResolver resolver)
Register the given protocol resolver with this application context, allowing for additional resource protocols to be handled.

Any such resolver will be invoked ahead of this context's standard resolution rules. It may therefore also override any default rules.

Since:
  4.3

### refresh

void refresh() throws org.springframework.beans.BeansException, IllegalStateException
Load or refresh the persistent representation of the configuration, which might be from Java-based configuration, an XML file, a properties file, a relational database schema, or some other format.

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 this method, either all or no singletons at all should be instantiated.

Throws:
  `org.springframework.beans.BeansException` - if the bean factory could not be initialized
  `IllegalStateException` - if already initialized and multiple refresh attempts are not supported

### registerShutdownHook

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.

This method can be called multiple times. Only one shutdown hook (at max) will be registered for each context instance.

The name of the shutdown hook thread should be `SHUTDOWN_HOOK_THREAD_NAME`.

See Also:
  - `Runtime.addShutdownHook(java.lang.Thread)`
  - `close()`

### close

void close()
Close this application context, releasing all resources and locks that the implementation might hold. This includes destroying all cached singleton beans.

Note: Does not invoke `close` on a parent context; parent contexts have their own, independent lifecycle.

This method can be called multiple times without side effects: Subsequent `close` calls on an already closed context will be ignored.

Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Closeable`

### isClosed

boolean isClosed()
Return whether this context has been closed already, that is, whether `close()` has been called on an active context in order to initiate its shutdown.

Note: This does not indicate whether context shutdown has completed. Use `isActive()` for differentiating between those scenarios: a context becomes inactive once it has been fully shut down and the original `close()` call has returned.

Since:
  6.2

### isActive

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.

Returns:
  whether the context is still active
See Also:
  - `refresh()`
  - `close()`
  - `getBeanFactory()`

### getBeanFactory

org.springframework.beans.factory.config.ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException
Return the internal bean factory of this application context. Can be used to access specific functionality of the underlying factory.

Note: Do not use this to post-process the bean factory; singletons will already have been instantiated before. Use a BeanFactoryPostProcessor to intercept the BeanFactory setup process before beans get touched.

Generally, this internal factory will only be accessible while the context is active, that is, in-between `refresh()` and `close()`. The `isActive()` flag can be used to check whether the context is in an appropriate state.

Returns:
  the underlying bean factory
Throws:
  `IllegalStateException` - if the context does not hold an internal bean factory (usually if `refresh()` hasn't been called yet or if `close()` has already been called)
See Also:
  - `isActive()`
  - `refresh()`
  - `close()`
  - `addBeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor)`