public interface ApplicationContext extends BeanContext, PropertyResolver, PropertyPlaceholderResolver
An application context extends a BeanContext
and adds the concepts of configuration, environments and
runtimes.
The ApplicationContext
is the main entry point for starting and running Micronaut applications. It
can be thought of as a container object for all dependency injected objects.
The ApplicationContext
can be started via the run()
method. For example:
ApplicationContext context = ApplicationContext.run();
Alternatively, the build()
method can be used to customize the ApplicationContext
using the ApplicationContextBuilder
interface
prior to running. For example:
ApplicationContext context = ApplicationContext.build().environments("test").start();
The getEnvironment()
method can be used to obtain a reference to the application Environment
, which contains the loaded configuration
and active environment names.
ApplicationContextBuilder
,
Environment
PropertyPlaceholderResolver.Placeholder
DEFAULT
Modifier and Type | Method and Description |
---|---|
static ApplicationContextBuilder |
build()
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(ClassLoader classLoader,
String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(Class mainClass,
String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(Map<String,Object> properties,
String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(String... environments)
Build a
ApplicationContext . |
default ApplicationContext |
environment(Consumer<Environment> consumer)
Allow configuration the
Environment . |
ConversionService<?> |
getConversionService() |
Environment |
getEnvironment() |
default <T> ApplicationContext |
registerSingleton(Class<T> type,
T singleton)
Registers a new singleton bean at runtime.
|
default <T> ApplicationContext |
registerSingleton(Class<T> type,
T singleton,
Qualifier<T> qualifier)
Registers a new singleton bean at runtime.
|
<T> ApplicationContext |
registerSingleton(Class<T> type,
T singleton,
Qualifier<T> qualifier,
boolean inject)
Registers a new singleton bean at runtime.
|
default ApplicationContext |
registerSingleton(Object singleton)
Registers a new singleton bean at runtime.
|
default ApplicationContext |
registerSingleton(Object singleton,
boolean inject)
Registers a new singleton bean at runtime.
|
static ApplicationContext |
run()
Run the
ApplicationContext . |
static <T extends AutoCloseable> |
run(Class<T> type,
Map<String,Object> properties,
String... environments)
Run the
ApplicationContext with the given type. |
static <T extends AutoCloseable> |
run(Class<T> type,
PropertySource propertySource,
String... environments)
Run the
ApplicationContext with the given type. |
static <T extends AutoCloseable> |
run(Class<T> type,
String... environments)
Run the
ApplicationContext with the given type. |
static ApplicationContext |
run(ClassLoader classLoader,
String... environments)
Run the
BeanContext . |
static ApplicationContext |
run(Map<String,Object> properties,
String... environments)
Run the
ApplicationContext with the given type. |
static ApplicationContext |
run(PropertySource properties,
String... environments)
Run the
ApplicationContext with the given type. |
static ApplicationContext |
run(String... environments)
Run the
ApplicationContext . |
ApplicationContext |
start()
Starts the application context.
|
ApplicationContext |
stop()
Stops the application context.
|
build, createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, getClassLoader, inject, refreshBean, run
findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod
findBean, findBean, findOrInstantiateBean, getBean, getBean, getBeansOfType, getBeansOfType, getProxyTargetBean, streamOfType, streamOfType
containsBean, containsBean, findBeanConfiguration, findBeanDefinition, findBeanDefinition, findBeanRegistration, findProxyBeanDefinition, findProxyTargetBeanDefinition, getActiveBeanRegistrations, getActiveBeanRegistrations, getAllBeanDefinitions, getBeanDefinition, getBeanDefinition, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanRegistrations, getProxyTargetBeanDefinition
publishEvent
resolveMetadata, resolveMetadata
containsProperties, containsProperty, get, getProperty, getProperty, getProperty, getProperty, getProperty, getRequiredProperty, nameOf
get, get, get
getPrefix, resolvePlaceholders, resolvePropertyNames, resolveRequiredPlaceholders
ConversionService<?> getConversionService()
Environment getEnvironment()
ApplicationContext start()
start
in interface LifeCycle<BeanContext>
ApplicationContext stop()
stop
in interface LifeCycle<BeanContext>
<T> ApplicationContext registerSingleton(Class<T> type, T singleton, Qualifier<T> qualifier, boolean inject)
BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by
invoking any PostConstruct
hooks.
If no bean definition data is found the bean is registered as is.
registerSingleton
in interface BeanContext
registerSingleton
in interface BeanDefinitionRegistry
T
- The concrete typetype
- The bean typesingleton
- The singleton beanqualifier
- The bean qualifierinject
- Whether the singleton should be injected (defaults to true)default <T> ApplicationContext registerSingleton(Class<T> type, T singleton, Qualifier<T> qualifier)
BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by
invoking any PostConstruct
hooks.
If no bean definition data is found the bean is registered as is.
registerSingleton
in interface BeanContext
registerSingleton
in interface BeanDefinitionRegistry
T
- The concrete typetype
- The bean typesingleton
- The singleton beanqualifier
- The bean qualifierdefault <T> ApplicationContext registerSingleton(Class<T> type, T singleton)
BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by
invoking any PostConstruct
hooks.
If no bean definition data is found the bean is registered as is.
registerSingleton
in interface BeanContext
registerSingleton
in interface BeanDefinitionRegistry
T
- The concrete typetype
- the bean typesingleton
- The singleton beandefault ApplicationContext registerSingleton(Object singleton, boolean inject)
BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by
invoking any PostConstruct
hooks.
If no bean definition data is found the bean is registered as is.
registerSingleton
in interface BeanContext
registerSingleton
in interface BeanDefinitionRegistry
singleton
- The singleton beaninject
- Whether the singleton should be injected (defaults to true)default ApplicationContext environment(Consumer<Environment> consumer)
Environment
.consumer
- The consumerdefault ApplicationContext registerSingleton(Object singleton)
BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by
invoking any PostConstruct
hooks.
If no bean definition data is found the bean is registered as is.
registerSingleton
in interface BeanContext
registerSingleton
in interface BeanDefinitionRegistry
singleton
- The singleton beanstatic ApplicationContext run(String... environments)
environments
- The environments to useApplicationContext
static ApplicationContext run()
run
in interface BeanContext
ApplicationContext
static ApplicationContext run(Map<String,Object> properties, String... environments)
ApplicationContext
with the given type. Returning an instance of the type. Note this method
should not be used.
If the ApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for
shutting down the context.properties
- Additional propertiesenvironments
- The environment namesApplicationContext
static ApplicationContext run(PropertySource properties, String... environments)
ApplicationContext
with the given type. Returning an instance of the type. Note this method
should not be used.
If the ApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for
shutting down the context.properties
- Additional propertiesenvironments
- The environment namesApplicationContext
static <T extends AutoCloseable> T run(Class<T> type, String... environments)
ApplicationContext
with the given type. Returning an instance of the type. Note this method
should not be used.
If the ApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for
shutting down the context.T
- The typetype
- The type of the bean to runenvironments
- The environments to usestatic <T extends AutoCloseable> T run(Class<T> type, Map<String,Object> properties, String... environments)
ApplicationContext
with the given type. Returning an instance of the type. Note this method
should not be used.
If the ApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for
shutting down the context.T
- The typetype
- The type of the bean to runproperties
- Additional propertiesenvironments
- The environment namesstatic <T extends AutoCloseable> T run(Class<T> type, PropertySource propertySource, String... environments)
ApplicationContext
with the given type. Returning an instance of the type. Note this method
should not be used.
If the ApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for
shutting down the context.T
- The typetype
- The environment to usepropertySource
- Additional propertiesenvironments
- The environment namesBeanContext
static ApplicationContextBuilder build(String... environments)
ApplicationContext
.environments
- The environments to useApplicationContext
static ApplicationContextBuilder build(Map<String,Object> properties, String... environments)
ApplicationContext
.properties
- The propertiesenvironments
- The environments to useApplicationContext
static ApplicationContextBuilder build()
ApplicationContext
.build
in interface BeanContext
ApplicationContext
static ApplicationContext run(ClassLoader classLoader, String... environments)
classLoader
- The classloader to useenvironments
- The environments to useApplicationContext
static ApplicationContextBuilder build(ClassLoader classLoader, String... environments)
ApplicationContext
.classLoader
- The classloader to useenvironments
- The environment to useApplicationContext
static ApplicationContextBuilder build(Class mainClass, String... environments)
ApplicationContext
.mainClass
- The main class of the applicationenvironments
- The environment to useApplicationContext