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
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 TypeMethodDescriptionstatic ApplicationContextBuilderbuilder()Build aApplicationContext.static ApplicationContextBuilderBuild aApplicationContext.static ApplicationContextBuilderbuilder(ClassLoader classLoader) static ApplicationContextBuilderbuilder(ClassLoader classLoader, String... environments) Build aApplicationContext.static ApplicationContextBuilderBuild aApplicationContext.static ApplicationContextBuilderBuild aApplicationContext.static ApplicationContextcreate(Environment environment) Creates theApplicationContextusing the givenEnvironment.default ApplicationContextenvironment(Consumer<Environment> consumer) Allow configuration theEnvironment.default <T> ApplicationContextregisterSingleton(Class<T> type, T singleton) Registers a new singleton bean at runtime.default <T> ApplicationContextregisterSingleton(Class<T> type, T singleton, @Nullable Qualifier<T> qualifier) Registers a new singleton bean at runtime.registerSingleton(Class<T> type, T singleton, @Nullable Qualifier<T> qualifier, boolean inject) Registers a new singleton bean at runtime.default ApplicationContextregisterSingleton(Object singleton) Registers a new singleton bean at runtime.default ApplicationContextregisterSingleton(Object singleton, boolean inject) Registers a new singleton bean at runtime.static ApplicationContextrun()Run theApplicationContext.static ApplicationContextrun(PropertySource properties, String... environments) Run theApplicationContextwith the given type.static <T extends AutoCloseable>
Trun(Class<T> type, PropertySource propertySource, String... environments) Run theApplicationContextwith the given type.static <T extends AutoCloseable>
TRun theApplicationContextwith the given type.static <T extends AutoCloseable>
TRun theApplicationContextwith the given type.static ApplicationContextrun(ClassLoader classLoader, String... environments) Run theBeanContext.static ApplicationContextRun theApplicationContext.static 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
Environment getEnvironment()- Returns:
- The application environment
-
start
ApplicationContext start()Starts the application context.- Specified by:
startin interfaceLifeCycle<BeanContext>- Returns:
- The application context
-
stop
ApplicationContext stop()Stops the application context.- Specified by:
stopin interfaceLifeCycle<BeanContext>- Returns:
- The application context
-
registerSingleton
<T> ApplicationContext registerSingleton(Class<T> type, T singleton, @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
default <T> ApplicationContext registerSingleton(Class<T> type, T singleton, @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
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
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
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
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
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
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
static <T extends AutoCloseable> T run(Class<T> type, Map<String, Object> properties, 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
static <T extends AutoCloseable> T run(Class<T> type, PropertySource propertySource, 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
Build aApplicationContext.- Parameters:
environments- The environments to use- Returns:
- The application context builder
-
builder
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
- Parameters:
classLoader- The classloader to useenvironments- The environments to use- Returns:
- The running
ApplicationContext
-
builder
Build aApplicationContext.- Parameters:
classLoader- The classloader to useenvironments- The environment to use- Returns:
- The application context builder
-
builder
Build aApplicationContext.- Parameters:
mainClass- The main class of the applicationenvironments- The environment to use- Returns:
- The application context builder
-
create
Creates theApplicationContextusing the givenEnvironment.- Returns:
- The created
ApplicationContext - Since:
- 5.0
-