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
DEFAULTFields inherited from interface io.micronaut.context.event.ApplicationEventPublisher
NO_OPFields 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 ApplicationContextenvironment(@NonNull Consumer<Environment> consumer) Allow configuration theEnvironment.default <T> @NonNull ApplicationContextregisterSingleton(@NonNull Class<T> type, T singleton) Registers a new singleton bean at runtime.default <T> @NonNull ApplicationContextregisterSingleton(@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 ApplicationContextregisterSingleton(@NonNull Object singleton) Registers a new singleton bean at runtime.default @NonNull ApplicationContextregisterSingleton(@NonNull Object singleton, boolean inject) Registers a new singleton bean at runtime.static @NonNull ApplicationContextrun()Run theApplicationContext.static @NonNull ApplicationContextrun(@NonNull PropertySource properties, @NonNull String... environments) Run theApplicationContextwith the given type.static <T extends AutoCloseable>
Trun(@NonNull Class<T> type, @NonNull PropertySource propertySource, @NonNull String... environments) Run theApplicationContextwith the given type.static <T extends AutoCloseable>
TRun theApplicationContextwith the given type.static <T extends AutoCloseable>
Trun(@NonNull Class<T> type, @NonNull Map<String, Object> properties, @NonNull String... environments) Run theApplicationContextwith the given type.static @NonNull ApplicationContextrun(@NonNull ClassLoader classLoader, @NonNull String... environments) Run theBeanContext.static @NonNull ApplicationContextRun theApplicationContext.static @NonNull ApplicationContextRun theApplicationContextwith the given type.start()Starts the application context.stop()Stops the application context.Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
resolveMetadata, resolveMetadataMethods inherited from interface io.micronaut.context.event.ApplicationEventPublisher
isEmpty, publishEvent, publishEventAsyncMethods inherited from interface io.micronaut.core.attr.AttributeHolder
getAttribute, getAttributeMethods inherited from interface io.micronaut.context.BeanContext
createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, destroyBean, destroyBean, destroyBean, destroyBean, getBeanValidator, getClassLoader, getContextConfiguration, getEventPublisher, inject, refreshBean, refreshBeanMethods 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, registerBeanConfiguration, registerBeanDefinitionMethods 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, streamOfTypeMethods inherited from interface io.micronaut.core.convert.ConversionServiceProvider
getConversionServiceMethods inherited from interface io.micronaut.context.ExecutionHandleLocator
createExecutionHandle, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, findProxyTargetMethod, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod, getProxyTargetMethod, getProxyTargetMethodMethods inherited from interface io.micronaut.core.attr.MutableAttributeHolder
getAttributes, removeAttribute, setAttributeMethods inherited from interface io.micronaut.context.env.PropertyPlaceholderResolver
getPrefix, resolveOptionalPlaceholder, resolvePlaceholders, resolveRequiredPlaceholder, resolveRequiredPlaceholders, resolveRequiredPlaceholdersObjectMethods inherited from interface io.micronaut.core.value.PropertyResolver
containsProperties, containsProperty, get, getProperties, getProperties, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyEntries, getPropertyEntries, getPropertyPathMatches, getRequiredPropertyMethods 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:
startin interfaceLifeCycle<BeanContext>- Returns:
- The application context
-
stop
Stops the application context.- Specified by:
stopin 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:BeanDefinitionRegistryRegisters 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
PostConstructhooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingletonin interfaceBeanContext- Specified by:
registerSingletonin 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:BeanDefinitionRegistryRegisters 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
PostConstructhooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingletonin interfaceBeanContext- Specified by:
registerSingletonin 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:BeanDefinitionRegistryRegisters 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
PostConstructhooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingletonin interfaceBeanContext- Specified by:
registerSingletonin 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:BeanDefinitionRegistryRegisters 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
PostConstructhooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingletonin interfaceBeanContext- Specified by:
registerSingletonin 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:BeanDefinitionRegistryRegisters 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
PostConstructhooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingletonin interfaceBeanContext- Specified by:
registerSingletonin 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 theApplicationContextwith the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContextrequires 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 theApplicationContextwith the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContextrequires 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 theApplicationContextwith the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContextrequires 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 theApplicationContextwith the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContextrequires 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 theApplicationContextwith the given type. Returning an instance of the type. Note this method should not be used. If theApplicationContextrequires 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
-