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

public interface ApplicationContext extends BeanContext, 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
Author:
Graeme Rocher
See Also:
  • Method Details

    • getEnvironment

      Environment getEnvironment()
      Returns:
      The application environment
    • start

      Starts the application context.
      Specified by:
      start in interface LifeCycle<BeanContext>
      Returns:
      The application context
    • stop

      Stops the application context.
      Specified by:
      stop in interface LifeCycle<BeanContext>
      Returns:
      The application context
    • registerSingleton

      <T> ApplicationContext registerSingleton(Class<T> type, T singleton, @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

      default <T> ApplicationContext registerSingleton(Class<T> type, T singleton, @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

      default <T> ApplicationContext registerSingleton(Class<T> type, 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

      default ApplicationContext registerSingleton(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

      default ApplicationContext environment(Consumer<Environment> consumer)
      Allow configuration the Environment.
      Parameters:
      consumer - The consumer
      Returns:
      This context
    • registerSingleton

      default ApplicationContext registerSingleton(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

      static ApplicationContext run(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

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

      static ApplicationContext run(Map<String,Object> properties, 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

      static ApplicationContext run(PropertySource properties, 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

      static <T extends AutoCloseable> T run(Class<T> type, 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

      static <T extends AutoCloseable> T run(Class<T> type, Map<String,Object> properties, 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

      static <T extends AutoCloseable> T run(Class<T> type, PropertySource propertySource, 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

      static ApplicationContextBuilder builder(String... environments)
      Parameters:
      environments - The environments to use
      Returns:
      The application context builder
    • builder

      static ApplicationContextBuilder builder(Map<String,Object> properties, String... environments)
      Parameters:
      properties - The properties
      environments - The environments to use
      Returns:
      The application context builder
    • builder

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

      static ApplicationContextBuilder builder(ClassLoader classLoader)
      Parameters:
      classLoader - The class loader to use
      Returns:
      The application context builder
    • run

      static ApplicationContext run(ClassLoader classLoader, 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

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

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

      static ApplicationContext create(Environment environment)
      Creates the ApplicationContext using the given Environment.
      Returns:
      The created ApplicationContext
      Since:
      5.0