public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable
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.
Modifier and Type | Field and Description |
---|---|
static String |
APPLICATION_STARTUP_BEAN_NAME
Name of the
ApplicationStartup bean in the factory. |
static String |
CONFIG_LOCATION_DELIMITERS
Any number of these characters are considered delimiters between
multiple context config paths in a single String value.
|
static String |
CONVERSION_SERVICE_BEAN_NAME
Name of the ConversionService bean in the factory.
|
static String |
ENVIRONMENT_BEAN_NAME
Name of the
Environment bean in the factory. |
static String |
LOAD_TIME_WEAVER_BEAN_NAME
Name of the LoadTimeWeaver bean in the factory.
|
static String |
SHUTDOWN_HOOK_THREAD_NAME
Name of the shutdown hook thread: "SpringContextShutdownHook". |
static String |
SYSTEM_ENVIRONMENT_BEAN_NAME
Name of the System environment bean in the factory.
|
static String |
SYSTEM_PROPERTIES_BEAN_NAME
Name of the System properties bean in the factory.
|
FACTORY_BEAN_PREFIX
CLASSPATH_ALL_URL_PREFIX
CLASSPATH_URL_PREFIX
Modifier and Type | Method and 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(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(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.
|
ApplicationStartup |
getApplicationStartup()
Return the
ApplicationStartup for this application context. |
ConfigurableListableBeanFactory |
getBeanFactory()
Return the internal bean factory of this application context.
|
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.
|
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 |
setApplicationStartup(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(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.
|
getApplicationName, getAutowireCapableBeanFactory, getDisplayName, getId, getParent, getStartupDate
containsBeanDefinition, findAnnotationOnBean, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForAnnotation, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanNamesForType, getBeanProvider, getBeanProvider, getBeansOfType, getBeansOfType, getBeansWithAnnotation
containsLocalBean, getParentBeanFactory
containsBean, getAliases, getBean, getBean, getBean, getBean, getBean, getBeanProvider, getBeanProvider, getType, getType, isPrototype, isSingleton, isTypeMatch, isTypeMatch
getMessage, getMessage, getMessage
publishEvent, publishEvent
getResources
getClassLoader, getResource
static final String CONFIG_LOCATION_DELIMITERS
static final String CONVERSION_SERVICE_BEAN_NAME
ConversionService
,
Constant Field Valuesstatic final String LOAD_TIME_WEAVER_BEAN_NAME
LoadTimeWeaver
,
Constant Field Valuesstatic final String ENVIRONMENT_BEAN_NAME
Environment
bean in the factory.static final String SYSTEM_PROPERTIES_BEAN_NAME
System.getProperties()
,
Constant Field Valuesstatic final String SYSTEM_ENVIRONMENT_BEAN_NAME
System.getenv()
,
Constant Field Valuesstatic final String APPLICATION_STARTUP_BEAN_NAME
ApplicationStartup
bean in the factory.static final String SHUTDOWN_HOOK_THREAD_NAME
Name
of the shutdown hook thread: "SpringContextShutdownHook".registerShutdownHook()
,
Constant Field Valuesvoid setId(String id)
void setParent(@Nullable ApplicationContext parent)
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.
parent
- the parent contextConfigurableWebApplicationContext
void setEnvironment(ConfigurableEnvironment environment)
Environment
for this application context.environment
- the new environmentConfigurableEnvironment getEnvironment()
Environment
for this application context in configurable
form, allowing for further customization.getEnvironment
in interface EnvironmentCapable
void setApplicationStartup(ApplicationStartup applicationStartup)
ApplicationStartup
for this application context.
This allows the application context to record metrics during startup.
applicationStartup
- the new context event factoryApplicationStartup getApplicationStartup()
ApplicationStartup
for this application context.void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor)
postProcessor
- the factory processor to registervoid addApplicationListener(ApplicationListener<?> listener)
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.
listener
- the ApplicationListener to registerContextRefreshedEvent
,
ContextClosedEvent
void setClassLoader(ClassLoader classLoader)
This context class loader will be passed to the internal bean factory.
void addProtocolResolver(ProtocolResolver resolver)
Any such resolver will be invoked ahead of this context's standard resolution rules. It may therefore also override any default rules.
void refresh() throws BeansException, IllegalStateException
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.
BeansException
- if the bean factory could not be initializedIllegalStateException
- if already initialized and multiple refresh
attempts are not supportedvoid registerShutdownHook()
This method can be called multiple times. Only one shutdown hook (at max) will be registered for each context instance.
As of Spring Framework 5.2, the name of
the shutdown hook thread should be SHUTDOWN_HOOK_THREAD_NAME
.
Runtime.addShutdownHook(java.lang.Thread)
,
close()
void close()
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.
close
in interface AutoCloseable
close
in interface Closeable
boolean isActive()
refresh()
,
close()
,
getBeanFactory()
ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException
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.
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)isActive()
,
refresh()
,
close()
,
addBeanFactoryPostProcessor(org.springframework.beans.factory.config.BeanFactoryPostProcessor)