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
NO_OP
DEFAULT
Modifier and Type | Method and Description |
---|---|
static ApplicationContextBuilder |
builder()
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(ClassLoader classLoader,
String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(Class mainClass,
String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(Map<String,Object> properties,
String... environments)
Build a
ApplicationContext . |
static ApplicationContextBuilder |
builder(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, build, createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, destroyBean, destroyBean, destroyBean, getBeanValidator, getClassLoader, getContextConfiguration, getEventPublisher, inject, publishEvent, publishEventAsync, refreshBean, run
createExecutionHandle, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, findProxyTargetMethod, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod, getProxyTargetMethod, getProxyTargetMethod
findBean, findBean, findBean, findBean, findOrInstantiateBean, getBean, getBean, getBean, getBean, getBean, getBeansOfType, getBeansOfType, getBeansOfType, getBeansOfType, getProxyTargetBean, getProxyTargetBean, streamOfType, streamOfType, streamOfType, streamOfType
containsBean, containsBean, containsBean, containsBean, findBeanConfiguration, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanRegistration, findProxyBeanDefinition, findProxyBeanDefinition, findProxyTargetBeanDefinition, findProxyTargetBeanDefinition, getActiveBeanRegistrations, getActiveBeanRegistrations, getAllBeanDefinitions, getBeanDefinition, getBeanDefinition, getBeanDefinition, getBeanDefinition, getBeanDefinitionReferences, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanRegistration, getBeanRegistration, getBeanRegistrations, getBeanRegistrations, getBeanRegistrations, getProxyTargetBeanDefinition, getProxyTargetBeanDefinition
noOp
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 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@NonNull default ApplicationContext registerSingleton(@NonNull 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 builder(@NonNull String... environments)
ApplicationContext
.environments
- The environments to use@NonNull static ApplicationContextBuilder builder(@NonNull Map<String,Object> properties, @NonNull String... environments)
ApplicationContext
.properties
- The propertiesenvironments
- The environments to use@NonNull static ApplicationContextBuilder builder()
ApplicationContext
.@NonNull static ApplicationContext run(@NonNull ClassLoader classLoader, @NonNull String... environments)
classLoader
- The classloader to useenvironments
- The environments to useApplicationContext
@NonNull static ApplicationContextBuilder builder(@NonNull ClassLoader classLoader, @NonNull String... environments)
ApplicationContext
.classLoader
- The classloader to useenvironments
- The environment to use@NonNull static ApplicationContextBuilder builder(@NonNull Class mainClass, @NonNull String... environments)
ApplicationContext
.mainClass
- The main class of the applicationenvironments
- The environment to use