Interface ApplicationContext

All Superinterfaces:
io.micronaut.core.annotation.AnnotationMetadataResolver, ApplicationEventPublisher<Object>, io.micronaut.core.attr.AttributeHolder, AutoCloseable, BeanContext, BeanDefinitionRegistry, BeanLocator, Closeable, io.micronaut.core.convert.ConversionServiceProvider, ExecutionHandleLocator, LifeCycle<BeanContext>, io.micronaut.core.attr.MutableAttributeHolder, PropertyPlaceholderResolver, io.micronaut.core.value.PropertyResolver, io.micronaut.core.value.ValueResolver<String>
All Known Subinterfaces:
ConfigurableApplicationContext
All Known Implementing Classes:
DefaultApplicationContext

public interface ApplicationContext extends BeanContext, io.micronaut.core.value.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.

Since:
1.0
See Also:
  • Method Details

    • getEnvironment

      @NonNull @NonNull Environment getEnvironment()
      Returns:
      The application environment
    • start

      @NonNull @NonNull ApplicationContext start()
      Starts the application context.
      Specified by:
      start in interface LifeCycle<BeanContext>
      Returns:
      The application context
    • stop

      @NonNull @NonNull ApplicationContext stop()
      Stops the application context.
      Specified by:
      stop in interface LifeCycle<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 interface BeanContext
      Specified by:
      registerSingleton in interface BeanDefinitionRegistry
      Type Parameters:
      T - The concrete type
      Parameters:
      type - The bean type
      singleton - The singleton bean
      qualifier - The bean qualifier
      inject - 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 interface BeanContext
      Specified by:
      registerSingleton in interface BeanDefinitionRegistry
      Type Parameters:
      T - The concrete type
      Parameters:
      type - The bean type
      singleton - The singleton bean
      qualifier - 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 interface BeanContext
      Specified by:
      registerSingleton in interface BeanDefinitionRegistry
      Type Parameters:
      T - The concrete type
      Parameters:
      type - the bean type
      singleton - 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 interface BeanContext
      Specified by:
      registerSingleton in interface BeanDefinitionRegistry
      Parameters:
      singleton - The singleton bean
      inject - 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 the Environment.
      Parameters:
      consumer - The consumer
      Returns:
      This context
    • registerSingleton

      @NonNull default @NonNull ApplicationContext registerSingleton(@NonNull @NonNull Object 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 interface BeanContext
      Specified by:
      registerSingleton in interface BeanDefinitionRegistry
      Parameters:
      singleton - The singleton bean
      Returns:
      This bean context
    • run

      @NonNull static @NonNull ApplicationContext run(@NonNull @NonNull String... environments)
      Run the ApplicationContext. This method will instantiate a new ApplicationContext and call start().
      Parameters:
      environments - The environments to use
      Returns:
      The running ApplicationContext
    • run

      @NonNull static @NonNull ApplicationContext run()
      Run the ApplicationContext. This method will instantiate a new ApplicationContext and call start().
      Returns:
      The running ApplicationContext
    • run

      @NonNull static @NonNull ApplicationContext run(@NonNull @NonNull Map<String,Object> properties, @NonNull @NonNull String... environments)
      Run the 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.
      Parameters:
      properties - Additional properties
      environments - The environment names
      Returns:
      The running ApplicationContext
    • run

      @NonNull static @NonNull ApplicationContext run(@NonNull @NonNull PropertySource properties, @NonNull @NonNull String... environments)
      Run the 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.
      Parameters:
      properties - Additional properties
      environments - 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 the 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.
      Type Parameters:
      T - The type
      Parameters:
      type - The type of the bean to run
      environments - 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 the 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.
      Type Parameters:
      T - The type
      Parameters:
      type - The type of the bean to run
      properties - Additional properties
      environments - 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 the 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.
      Type Parameters:
      T - The type
      Parameters:
      type - The environment to use
      propertySource - Additional properties
      environments - The environment names
      Returns:
      The running BeanContext
    • builder

      @NonNull static @NonNull ApplicationContextBuilder builder(@NonNull @NonNull String... environments)
      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)
      Parameters:
      properties - The properties
      environments - The environments to use
      Returns:
      The application context builder
    • builder

      @NonNull static @NonNull ApplicationContextBuilder builder()
      Returns:
      The application context builder
    • builder

      @NonNull static @NonNull ApplicationContextBuilder builder(ClassLoader classLoader)
      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)
      Run the BeanContext. This method will instantiate a new BeanContext and call start()
      Parameters:
      classLoader - The classloader to use
      environments - The environments to use
      Returns:
      The running ApplicationContext
    • builder

      @NonNull static @NonNull ApplicationContextBuilder builder(@NonNull @NonNull ClassLoader classLoader, @NonNull @NonNull String... environments)
      Parameters:
      classLoader - The classloader to use
      environments - The environment to use
      Returns:
      The application context builder
    • builder

      @NonNull static @NonNull ApplicationContextBuilder builder(@NonNull @NonNull Class<?> mainClass, @NonNull @NonNull String... environments)
      Parameters:
      mainClass - The main class of the application
      environments - The environment to use
      Returns:
      The application context builder