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 builder()
method can be used to customize the ApplicationContext
using the ApplicationContextBuilder
interface
prior to running. For example:
ApplicationContext context = ApplicationContext.builder().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
EMPTY
DEFAULT
Modifier and Type | Method and Description |
---|---|
static ApplicationContextBuilder |
build()
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(java.lang.ClassLoader classLoader,
java.lang.String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(java.lang.Class mainClass,
java.lang.String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(java.util.Map<java.lang.String,java.lang.Object> properties,
java.lang.String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
build(java.lang.String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder()
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(java.lang.ClassLoader classLoader,
java.lang.String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(java.lang.Class mainClass,
java.lang.String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(java.util.Map<java.lang.String,java.lang.Object> properties,
java.lang.String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(java.lang.String... environments)
Build a
ApplicationContext . |
default ApplicationContext |
environment(java.util.function.Consumer<Environment> consumer)
Allow configuration the
Environment . |
ConversionService<?> |
getConversionService() |
Environment |
getEnvironment() |
default <T> ApplicationContext |
registerSingleton(java.lang.Class<T> type,
T singleton)
Registers a new singleton bean at runtime.
|
default <T> ApplicationContext |
registerSingleton(java.lang.Class<T> type,
T singleton,
Qualifier<T> qualifier)
Registers a new singleton bean at runtime.
|
<T> ApplicationContext |
registerSingleton(java.lang.Class<T> type,
T singleton,
Qualifier<T> qualifier,
boolean inject)
Registers a new singleton bean at runtime.
|
default ApplicationContext |
registerSingleton(java.lang.Object singleton)
Registers a new singleton bean at runtime.
|
default @NotNull ApplicationContext |
registerSingleton(@NotNull java.lang.Object singleton,
boolean inject)
Registers a new singleton bean at runtime.
|
static ApplicationContext |
run()
Run the
ApplicationContext . |
static <T extends java.lang.AutoCloseable> |
run(java.lang.Class<T> type,
java.util.Map<java.lang.String,java.lang.Object> properties,
java.lang.String... environments)
Run the
ApplicationContext with the given type. |
static <T extends java.lang.AutoCloseable> |
run(java.lang.Class<T> type,
PropertySource propertySource,
java.lang.String... environments)
Run the
ApplicationContext with the given type. |
static <T extends java.lang.AutoCloseable> |
run(java.lang.Class<T> type,
java.lang.String... environments)
Run the
ApplicationContext with the given type. |
static ApplicationContext |
run(java.lang.ClassLoader classLoader,
java.lang.String... environments)
Run the
BeanContext . |
static ApplicationContext |
run(java.util.Map<java.lang.String,java.lang.Object> properties,
java.lang.String... environments)
Run the
ApplicationContext with the given type. |
static ApplicationContext |
run(PropertySource properties,
java.lang.String... environments)
Run the
ApplicationContext with the given type. |
static ApplicationContext |
run(java.lang.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, getBeanValidator, getClassLoader, inject, refreshBean, run
createExecutionHandle, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod, getProxyTargetMethod
findBean, findBean, findOrInstantiateBean, getBean, 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, publishEventAsync
resolveMetadata, resolveMetadata
getAttributes, removeAttribute, setAttribute
getAttribute, getAttribute
containsProperties, containsProperty, get, getProperties, getProperties, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyEntries, 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 java.lang.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 java.lang.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 java.lang.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 @NotNull ApplicationContext registerSingleton(@NotNull @NotNull java.lang.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 java.util.function.Consumer<Environment> consumer)
Environment
.consumer
- The consumer@NonNull default ApplicationContext registerSingleton(@NonNull java.lang.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 java.lang.String... environments)
environments
- The environments to useApplicationContext
@NonNull static ApplicationContext run()
run
in interface BeanContext
ApplicationContext
@NonNull static ApplicationContext run(@NonNull java.util.Map<java.lang.String,java.lang.Object> properties, @NonNull java.lang.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 java.lang.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 java.lang.AutoCloseable> T run(@NonNull java.lang.Class<T> type, @NonNull java.lang.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 java.lang.AutoCloseable> T run(@NonNull java.lang.Class<T> type, @NonNull java.util.Map<java.lang.String,java.lang.Object> properties, @NonNull java.lang.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 java.lang.AutoCloseable> T run(@NonNull java.lang.Class<T> type, @NonNull PropertySource propertySource, @NonNull java.lang.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 java.lang.String... environments)
ApplicationContext
.environments
- The environments to useApplicationContext
@NonNull static ApplicationContextBuilder build(@NonNull java.util.Map<java.lang.String,java.lang.Object> properties, @NonNull java.lang.String... environments)
ApplicationContext
.properties
- The propertiesenvironments
- The environments to useApplicationContext
@NonNull static ApplicationContextBuilder builder(@NonNull java.lang.String... environments)
ApplicationContext
.environments
- The environments to useApplicationContext
@NonNull static ApplicationContextBuilder builder(@NonNull java.util.Map<java.lang.String,java.lang.Object> properties, @NonNull java.lang.String... environments)
ApplicationContext
.properties
- The propertiesenvironments
- The environments to useApplicationContext
@NonNull static ApplicationContextBuilder build()
ApplicationContext
.build
in interface BeanContext
ApplicationContext
@NonNull static ApplicationContextBuilder builder()
ApplicationContext
.ApplicationContext
@NonNull static ApplicationContext run(@NonNull java.lang.ClassLoader classLoader, @NonNull java.lang.String... environments)
classLoader
- The classloader to useenvironments
- The environments to useApplicationContext
@NonNull static ApplicationContextBuilder build(@NonNull java.lang.ClassLoader classLoader, @NonNull java.lang.String... environments)
ApplicationContext
.classLoader
- The classloader to useenvironments
- The environment to useApplicationContext
@NonNull static ApplicationContextBuilder build(@NonNull java.lang.Class mainClass, @NonNull java.lang.String... environments)
ApplicationContext
.mainClass
- The main class of the applicationenvironments
- The environment to useApplicationContext
@NonNull static ApplicationContextBuilder builder(@NonNull java.lang.ClassLoader classLoader, @NonNull java.lang.String... environments)
ApplicationContext
.classLoader
- The classloader to useenvironments
- The environment to useApplicationContext
@NonNull static ApplicationContextBuilder builder(@NonNull java.lang.Class mainClass, @NonNull java.lang.String... environments)
ApplicationContext
.mainClass
- The main class of the applicationenvironments
- The environment to useApplicationContext