Interface ApplicationContext
-
- All Superinterfaces:
io.micronaut.core.annotation.AnnotationMetadataResolver,ApplicationEventPublisher<java.lang.Object>,io.micronaut.core.attr.AttributeHolder,java.lang.AutoCloseable,BeanContext,BeanDefinitionRegistry,BeanLocator,java.io.Closeable,ExecutionHandleLocator,LifeCycle<BeanContext>,io.micronaut.core.attr.MutableAttributeHolder,PropertyPlaceholderResolver,io.micronaut.core.value.PropertyResolver,io.micronaut.core.value.ValueResolver<java.lang.String>
- All Known Implementing Classes:
DefaultApplicationContext
public interface ApplicationContext extends BeanContext, io.micronaut.core.value.PropertyResolver, PropertyPlaceholderResolver
An application context extends a
BeanContextand adds the concepts of configuration, environments and runtimes.The
ApplicationContextis 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
ApplicationContextcan be started via therun()method. For example:ApplicationContext context = ApplicationContext.run();Alternatively, the
builder()method can be used to customize theApplicationContextusing theApplicationContextBuilderinterface prior to running. For example:ApplicationContext context = ApplicationContext.builder().environments("test").start();The
getEnvironment()method can be used to obtain a reference to the applicationEnvironment, which contains the loaded configuration and active environment names.- Since:
- 1.0
- See Also:
ApplicationContextBuilder,Environment
-
-
Field Summary
-
Fields inherited from interface io.micronaut.context.event.ApplicationEventPublisher
NO_OP
-
Fields inherited from interface io.micronaut.context.ExecutionHandleLocator
EMPTY
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static ApplicationContextBuilderbuilder()Build aApplicationContext.static ApplicationContextBuilderbuilder(java.lang.ClassLoader classLoader, java.lang.String... environments)Build aApplicationContext.static ApplicationContextBuilderbuilder(java.lang.Class mainClass, java.lang.String... environments)Build aApplicationContext.static ApplicationContextBuilderbuilder(java.lang.String... environments)Build aApplicationContext.static ApplicationContextBuilderbuilder(java.util.Map<java.lang.String,java.lang.Object> properties, java.lang.String... environments)Build aApplicationContext.default ApplicationContextenvironment(java.util.function.Consumer<Environment> consumer)Allow configuration theEnvironment.io.micronaut.core.convert.ConversionService<?>getConversionService()EnvironmentgetEnvironment()default <T> ApplicationContextregisterSingleton(java.lang.Class<T> type, T singleton)Registers a new singleton bean at runtime.default <T> ApplicationContextregisterSingleton(java.lang.Class<T> type, T singleton, Qualifier<T> qualifier)Registers a new singleton bean at runtime.<T> ApplicationContextregisterSingleton(java.lang.Class<T> type, T singleton, Qualifier<T> qualifier, boolean inject)Registers a new singleton bean at runtime.default ApplicationContextregisterSingleton(java.lang.Object singleton)Registers a new singleton bean at runtime.default ApplicationContextregisterSingleton(java.lang.Object singleton, boolean inject)Registers a new singleton bean at runtime.static ApplicationContextrun()Run theApplicationContext.static ApplicationContextrun(PropertySource properties, java.lang.String... environments)Run theApplicationContextwith the given type.static <T extends java.lang.AutoCloseable>
Trun(java.lang.Class<T> type, PropertySource propertySource, java.lang.String... environments)Run theApplicationContextwith the given type.static <T extends java.lang.AutoCloseable>
Trun(java.lang.Class<T> type, java.lang.String... environments)Run theApplicationContextwith the given type.static <T extends java.lang.AutoCloseable>
Trun(java.lang.Class<T> type, java.util.Map<java.lang.String,java.lang.Object> properties, java.lang.String... environments)Run theApplicationContextwith the given type.static ApplicationContextrun(java.lang.ClassLoader classLoader, java.lang.String... environments)Run theBeanContext.static ApplicationContextrun(java.lang.String... environments)Run theApplicationContext.static ApplicationContextrun(java.util.Map<java.lang.String,java.lang.Object> properties, java.lang.String... environments)Run theApplicationContextwith the given type.ApplicationContextstart()Starts the application context.ApplicationContextstop()Stops the application context.-
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
resolveMetadata, resolveMetadata
-
Methods inherited from interface io.micronaut.context.BeanContext
createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, destroyBean, destroyBean, destroyBean, destroyBean, getBeanValidator, getClassLoader, getContextConfiguration, getEventPublisher, inject, publishEvent, publishEventAsync, refreshBean, refreshBean
-
Methods inherited from interface io.micronaut.context.BeanDefinitionRegistry
containsBean, containsBean, containsBean, containsBean, findBeanConfiguration, 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, 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, getProxyTargetBean, getProxyTargetBean, streamOfType, streamOfType, streamOfType, streamOfType
-
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, resolvePlaceholders, resolveRequiredPlaceholder, resolveRequiredPlaceholders
-
-
-
-
Method Detail
-
getConversionService
@NonNull io.micronaut.core.convert.ConversionService<?> getConversionService()
- Returns:
- The default conversion service
-
getEnvironment
@NonNull Environment getEnvironment()
- Returns:
- The application environment
-
start
@NonNull ApplicationContext start()
Starts the application context.- Specified by:
startin interfaceLifeCycle<BeanContext>- Returns:
- The application context
-
stop
@NonNull ApplicationContext stop()
Stops the application context.- Specified by:
stopin interfaceLifeCycle<BeanContext>- Returns:
- The application context
-
registerSingleton
@NonNull <T> ApplicationContext registerSingleton(@NonNull java.lang.Class<T> type, @NonNull 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
@NonNull default <T> ApplicationContext registerSingleton(@NonNull java.lang.Class<T> type, @NonNull 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
@NonNull default <T> ApplicationContext registerSingleton(@NonNull java.lang.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 ApplicationContext registerSingleton(@NonNull java.lang.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 ApplicationContext environment(@NonNull java.util.function.Consumer<Environment> consumer)
Allow configuration theEnvironment.- Parameters:
consumer- The consumer- Returns:
- This context
-
registerSingleton
@NonNull default ApplicationContext registerSingleton(@NonNull java.lang.Object 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- Parameters:
singleton- The singleton bean- Returns:
- This bean context
-
run
@NonNull static ApplicationContext run(@NonNull java.lang.String... environments)
- Parameters:
environments- The environments to use- Returns:
- The running
ApplicationContext
-
run
@NonNull static ApplicationContext run()
- Returns:
- The running
ApplicationContext
-
run
@NonNull static ApplicationContext run(@NonNull java.util.Map<java.lang.String,java.lang.Object> properties, @NonNull java.lang.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 ApplicationContext run(@NonNull PropertySource properties, @NonNull java.lang.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 java.lang.AutoCloseable> T run(@NonNull java.lang.Class<T> type, @NonNull java.lang.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 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)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 java.lang.AutoCloseable> T run(@NonNull java.lang.Class<T> type, @NonNull PropertySource propertySource, @NonNull java.lang.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 ApplicationContextBuilder builder(@NonNull java.lang.String... environments)
Build aApplicationContext.- Parameters:
environments- The environments to use- Returns:
- The application context builder
-
builder
@NonNull static ApplicationContextBuilder builder(@NonNull java.util.Map<java.lang.String,java.lang.Object> properties, @NonNull java.lang.String... environments)
Build aApplicationContext.- Parameters:
properties- The propertiesenvironments- The environments to use- Returns:
- The application context builder
-
builder
@NonNull static ApplicationContextBuilder builder()
Build aApplicationContext.- Returns:
- The application context builder
-
run
@NonNull static ApplicationContext run(@NonNull java.lang.ClassLoader classLoader, @NonNull java.lang.String... environments)
- Parameters:
classLoader- The classloader to useenvironments- The environments to use- Returns:
- The running
ApplicationContext
-
builder
@NonNull static ApplicationContextBuilder builder(@NonNull java.lang.ClassLoader classLoader, @NonNull java.lang.String... environments)
Build aApplicationContext.- Parameters:
classLoader- The classloader to useenvironments- The environment to use- Returns:
- The application context builder
-
builder
@NonNull static ApplicationContextBuilder builder(@NonNull java.lang.Class mainClass, @NonNull java.lang.String... environments)
Build aApplicationContext.- Parameters:
mainClass- The main class of the applicationenvironments- The environment to use- Returns:
- The application context builder
-
-