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
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, getBeanDefinitionReferences, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanRegistrations, getProxyTargetBeanDefinition
publishEvent
resolveMetadata, resolveMetadata
containsProperties, containsProperty, get, getProperties, getProperties, getProperty, getProperty, getProperty, getProperty, getProperty, getRequiredProperty, nameOf
get, get, get
getPrefix, resolvePlaceholders, resolveRequiredPlaceholder, resolveRequiredPlaceholders
@Nonnull ConversionService<?> getConversionService()
@Nonnull Environment getEnvironment()
@Nonnull ApplicationContext start()
start
in interface LifeCycle<BeanContext>
@Nonnull ApplicationContext stop()
stop
in interface LifeCycle<BeanContext>
@Nonnull <T> ApplicationContext registerSingleton(@Nonnull Class<T> type, @Nonnull T singleton, @Nullable 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)@Nonnull default <T> ApplicationContext registerSingleton(@Nonnull Class<T> type, @Nonnull T singleton, @Nullable 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 qualifier@Nonnull default <T> ApplicationContext registerSingleton(@Nonnull Class<T> type, @Nonnull 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 bean@NotNull @Nonnull default ApplicationContext registerSingleton(@NotNull 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)@Nonnull default ApplicationContext environment(@Nonnull Consumer<Environment> consumer)
Environment
.consumer
- The consumer@Nonnull default ApplicationContext registerSingleton(@Nonnull 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 bean@Nonnull static ApplicationContext run(@Nonnull String... environments)
environments
- The environments to useApplicationContext
@Nonnull static ApplicationContext run()
run
in interface BeanContext
ApplicationContext
@Nonnull static ApplicationContext run(@Nonnull Map<String,Object> properties, @Nonnull 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
@Nonnull static ApplicationContext run(@Nonnull PropertySource properties, @Nonnull 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
@Nonnull static <T extends AutoCloseable> T run(@Nonnull Class<T> type, @Nonnull 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 use@Nonnull static <T extends AutoCloseable> T run(@Nonnull Class<T> type, @Nonnull Map<String,Object> properties, @Nonnull 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 names@Nonnull static <T extends AutoCloseable> T run(@Nonnull Class<T> type, @Nonnull PropertySource propertySource, @Nonnull 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
@Nonnull static ApplicationContextBuilder build(@Nonnull String... environments)
ApplicationContext
.environments
- The environments to useApplicationContext
@Nonnull static ApplicationContextBuilder build(@Nonnull Map<String,Object> properties, @Nonnull String... environments)
ApplicationContext
.properties
- The propertiesenvironments
- The environments to useApplicationContext
@Nonnull static ApplicationContextBuilder build()
ApplicationContext
.build
in interface BeanContext
ApplicationContext
@Nonnull static ApplicationContext run(@Nonnull ClassLoader classLoader, @Nonnull String... environments)
classLoader
- The classloader to useenvironments
- The environments to useApplicationContext
@Nonnull static ApplicationContextBuilder build(@Nonnull ClassLoader classLoader, @Nonnull String... environments)
ApplicationContext
.classLoader
- The classloader to useenvironments
- The environment to useApplicationContext
@Nonnull static ApplicationContextBuilder build(@Nonnull Class mainClass, @Nonnull String... environments)
ApplicationContext
.mainClass
- The main class of the applicationenvironments
- The environment to useApplicationContext