Interface ApplicationContext
- All Superinterfaces:
AnnotationMetadataResolver
,ApplicationEventPublisher<Object>
,AttributeHolder
,AutoCloseable
,BeanContext
,BeanDefinitionRegistry
,BeanLocator
,Closeable
,ConversionServiceProvider
,ExecutionHandleLocator
,LifeCycle<BeanContext>
,MutableAttributeHolder
,PropertyPlaceholderResolver
,PropertyResolver
,ValueResolver<String>
- All Known Subinterfaces:
ConfigurableApplicationContext
- All Known Implementing Classes:
DefaultApplicationContext
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.
- Since:
- 1.0
- Author:
- Graeme Rocher
- See Also:
-
Field Summary
Fields inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
DEFAULT
Fields inherited from interface io.micronaut.context.event.ApplicationEventPublisher
NO_OP
Fields inherited from interface io.micronaut.context.ExecutionHandleLocator
EMPTY
-
Method Summary
Modifier and TypeMethodDescriptionbuilder()
Build aApplicationContext
.Build aApplicationContext
.builder
(@NonNull ClassLoader classLoader, @NonNull String... environments) Build aApplicationContext
.Build aApplicationContext
.Build aApplicationContext
.builder
(ClassLoader classLoader) default @NonNull ApplicationContext
environment
(@NonNull Consumer<Environment> consumer) Allow configuration theEnvironment
.default <T> @NonNull ApplicationContext
registerSingleton
(@NonNull Class<T> type, T singleton) Registers a new singleton bean at runtime.default <T> @NonNull ApplicationContext
registerSingleton
(@NonNull Class<T> type, T singleton, @Nullable Qualifier<T> qualifier) Registers a new singleton bean at runtime.registerSingleton
(@NonNull Class<T> type, T singleton, @Nullable Qualifier<T> qualifier, boolean inject) Registers a new singleton bean at runtime.default @NonNull ApplicationContext
registerSingleton
(@NonNull Object singleton) Registers a new singleton bean at runtime.default @NonNull ApplicationContext
registerSingleton
(@NonNull Object singleton, boolean inject) Registers a new singleton bean at runtime.static @NonNull ApplicationContext
run()
Run theApplicationContext
.static @NonNull ApplicationContext
run
(@NonNull PropertySource properties, @NonNull String... environments) Run theApplicationContext
with the given type.static <T extends AutoCloseable>
Trun
(@NonNull Class<T> type, @NonNull PropertySource propertySource, @NonNull String... environments) Run theApplicationContext
with the given type.static <T extends AutoCloseable>
TRun theApplicationContext
with the given type.static <T extends AutoCloseable>
Trun
(@NonNull Class<T> type, @NonNull Map<String, Object> properties, @NonNull String... environments) Run theApplicationContext
with the given type.static @NonNull ApplicationContext
run
(@NonNull ClassLoader classLoader, @NonNull String... environments) Run theBeanContext
.static @NonNull ApplicationContext
Run theApplicationContext
.static @NonNull ApplicationContext
Run theApplicationContext
with the given type.start()
Starts the application context.stop()
Stops the application context.Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
resolveMetadata, resolveMetadata
Methods inherited from interface io.micronaut.context.event.ApplicationEventPublisher
isEmpty, publishEvent, publishEventAsync
Methods inherited from interface io.micronaut.core.attr.AttributeHolder
getAttribute, getAttribute
Methods inherited from interface io.micronaut.context.BeanContext
createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, destroyBean, destroyBean, destroyBean, destroyBean, getBeanValidator, getClassLoader, getContextConfiguration, getEventPublisher, inject, refreshBean, refreshBean
Methods inherited from interface io.micronaut.context.BeanDefinitionRegistry
containsBean, containsBean, containsBean, containsBean, findBeanConfiguration, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanRegistration, findProxyBeanDefinition, findProxyBeanDefinition, findProxyTargetBeanDefinition, findProxyTargetBeanDefinition, findProxyTargetBeanDefinition, getActiveBeanRegistrations, getActiveBeanRegistrations, getAllBeanDefinitions, getBeanDefinition, getBeanDefinition, getBeanDefinition, getBeanDefinition, getBeanDefinitionReferences, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanRegistration, getBeanRegistration, getBeanRegistration, getBeanRegistrations, getBeanRegistrations, getBeanRegistrations, getDisabledBeans, getProxyTargetBeanDefinition, getProxyTargetBeanDefinition, registerBeanDefinition
Methods inherited from interface io.micronaut.context.BeanLocator
findBean, findBean, findBean, findBean, findOrInstantiateBean, getBean, getBean, getBean, getBean, getBean, getBeansOfType, getBeansOfType, getBeansOfType, getBeansOfType, getProvider, getProvider, getProvider, getProvider, getProxyTargetBean, getProxyTargetBean, mapOfType, mapOfType, mapOfType, mapOfType, streamOfType, streamOfType, streamOfType, streamOfType
Methods inherited from interface io.micronaut.core.convert.ConversionServiceProvider
getConversionService
Methods inherited from interface io.micronaut.context.ExecutionHandleLocator
createExecutionHandle, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, findProxyTargetMethod, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod, getProxyTargetMethod, getProxyTargetMethod
Methods inherited from interface io.micronaut.core.attr.MutableAttributeHolder
getAttributes, removeAttribute, setAttribute
Methods inherited from interface io.micronaut.context.env.PropertyPlaceholderResolver
getPrefix, resolveOptionalPlaceholder, resolvePlaceholders, resolveRequiredPlaceholder, resolveRequiredPlaceholders, resolveRequiredPlaceholdersObject
Methods inherited from interface io.micronaut.core.value.PropertyResolver
containsProperties, containsProperty, get, getProperties, getProperties, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyEntries, getPropertyEntries, getPropertyPathMatches, getRequiredProperty
Methods inherited from interface io.micronaut.core.value.ValueResolver
get, get, get
-
Method Details
-
getEnvironment
- Returns:
- The application environment
-
start
Starts the application context.- Specified by:
start
in interfaceLifeCycle<BeanContext>
- Returns:
- The application context
-
stop
Stops the application context.- Specified by:
stop
in interfaceLifeCycle<BeanContext>
- Returns:
- The application context
-
registerSingleton
@NonNull <T> @NonNull ApplicationContext registerSingleton(@NonNull @NonNull Class<T> type, @NonNull T singleton, @Nullable @Nullable Qualifier<T> qualifier, boolean inject) Description copied from interface: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.
- Specified by:
registerSingleton
in interfaceBeanContext
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Type Parameters:
T
- The concrete type- Parameters:
type
- The bean typesingleton
- The singleton beanqualifier
- The bean qualifierinject
- Whether the singleton should be injected (defaults to true)- Returns:
- This bean context
-
registerSingleton
@NonNull default <T> @NonNull ApplicationContext registerSingleton(@NonNull @NonNull Class<T> type, @NonNull T singleton, @Nullable @Nullable Qualifier<T> qualifier) Description copied from interface: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.
- Specified by:
registerSingleton
in interfaceBeanContext
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Type Parameters:
T
- The concrete type- Parameters:
type
- The bean typesingleton
- The singleton beanqualifier
- The bean qualifier- Returns:
- This bean context
-
registerSingleton
@NonNull default <T> @NonNull ApplicationContext registerSingleton(@NonNull @NonNull Class<T> type, @NonNull T singleton) Description copied from interface: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.
- Specified by:
registerSingleton
in interfaceBeanContext
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Type Parameters:
T
- The concrete type- Parameters:
type
- the bean typesingleton
- The singleton bean- Returns:
- This bean context
-
registerSingleton
@NonNull default @NonNull ApplicationContext registerSingleton(@NonNull @NonNull Object singleton, boolean inject) Description copied from interface: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.
- Specified by:
registerSingleton
in interfaceBeanContext
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Parameters:
singleton
- The singleton beaninject
- Whether the singleton should be injected (defaults to true)- Returns:
- This bean context
-
environment
@NonNull default @NonNull ApplicationContext environment(@NonNull @NonNull Consumer<Environment> consumer) Allow configuration theEnvironment
.- Parameters:
consumer
- The consumer- Returns:
- This context
-
registerSingleton
Description copied from interface: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.
- Specified by:
registerSingleton
in interfaceBeanContext
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Parameters:
singleton
- The singleton bean- Returns:
- This bean context
-
run
- Parameters:
environments
- The environments to use- Returns:
- The running
ApplicationContext
-
run
- Returns:
- The running
ApplicationContext
-
run
@NonNull static @NonNull ApplicationContext run(@NonNull @NonNull Map<String, Object> properties, @NonNull @NonNull String... environments) Run theApplicationContext
with the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for shutting down the context.- Parameters:
properties
- Additional propertiesenvironments
- The environment names- Returns:
- The running
ApplicationContext
-
run
@NonNull static @NonNull ApplicationContext run(@NonNull @NonNull PropertySource properties, @NonNull @NonNull String... environments) Run theApplicationContext
with the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for shutting down the context.- Parameters:
properties
- Additional propertiesenvironments
- The environment names- Returns:
- The running
ApplicationContext
-
run
@NonNull static <T extends AutoCloseable> T run(@NonNull @NonNull Class<T> type, @NonNull @NonNull String... environments) Run theApplicationContext
with the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for shutting down the context.- Type Parameters:
T
- The type- Parameters:
type
- The type of the bean to runenvironments
- The environments to use- Returns:
- The running bean
-
run
@NonNull static <T extends AutoCloseable> T run(@NonNull @NonNull Class<T> type, @NonNull @NonNull Map<String, Object> properties, @NonNull @NonNull String... environments) Run theApplicationContext
with the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for shutting down the context.- Type Parameters:
T
- The type- Parameters:
type
- The type of the bean to runproperties
- Additional propertiesenvironments
- The environment names- Returns:
- The running bean
-
run
@NonNull static <T extends AutoCloseable> T run(@NonNull @NonNull Class<T> type, @NonNull @NonNull PropertySource propertySource, @NonNull @NonNull String... environments) Run theApplicationContext
with the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContext
requires graceful shutdown unless the returned bean takes responsibility for shutting down the context.- Type Parameters:
T
- The type- Parameters:
type
- The environment to usepropertySource
- Additional propertiesenvironments
- The environment names- Returns:
- The running
BeanContext
-
builder
@NonNull static @NonNull ApplicationContextBuilder builder(@NonNull @NonNull String... environments) Build aApplicationContext
.- Parameters:
environments
- The environments to use- Returns:
- The application context builder
-
builder
@NonNull static @NonNull ApplicationContextBuilder builder(@NonNull @NonNull Map<String, Object> properties, @NonNull @NonNull String... environments) Build aApplicationContext
.- Parameters:
properties
- The propertiesenvironments
- The environments to use- Returns:
- The application context builder
-
builder
Build aApplicationContext
.- Returns:
- The application context builder
-
builder
- Parameters:
classLoader
- The class loader to use- Returns:
- The application context builder
-
run
@NonNull static @NonNull ApplicationContext run(@NonNull @NonNull ClassLoader classLoader, @NonNull @NonNull String... environments) - Parameters:
classLoader
- The classloader to useenvironments
- The environments to use- Returns:
- The running
ApplicationContext
-
builder
@NonNull static @NonNull ApplicationContextBuilder builder(@NonNull @NonNull ClassLoader classLoader, @NonNull @NonNull String... environments) Build aApplicationContext
.- Parameters:
classLoader
- The classloader to useenvironments
- The environment to use- Returns:
- The application context builder
-
builder
@NonNull static @NonNull ApplicationContextBuilder builder(@NonNull @NonNull Class<?> mainClass, @NonNull @NonNull String... environments) Build aApplicationContext
.- Parameters:
mainClass
- The main class of the applicationenvironments
- The environment to use- Returns:
- The application context builder
-