Interface BeanContext

All Superinterfaces:
AnnotationMetadataResolver, ApplicationEventPublisher<Object>, AttributeHolder, AutoCloseable, BeanDefinitionRegistry, BeanLocator, Closeable, ConversionServiceProvider, ExecutionHandleLocator, LifeCycle<BeanContext>, MutableAttributeHolder
All Known Subinterfaces:
ApplicationContext, InitializableBeanContext
All Known Implementing Classes:
DefaultApplicationContext, DefaultBeanContext

The core BeanContext abstraction which allows for dependency injection of classes annotated with Inject.

Apart of the standard jakarta.inject annotations for dependency injection, additional annotations within the io.micronaut.context.annotation package allow control over configuration of the bean context.

Since:
1.0
Author:
Graeme Rocher
  • Method Details

    • getContextConfiguration

      @NonNull @NonNull BeanContextConfiguration getContextConfiguration()
      Obtains the configuration for this context.
      Returns:
      The BeanContextConfiguration
      Since:
      3.0.0
    • getEventPublisher

      @NonNull default <E> @NonNull ApplicationEventPublisher<E> getEventPublisher(@NonNull @NonNull Class<E> eventType)
      Obtain an ApplicationEventPublisher for the given even type.
      Type Parameters:
      E - The event generic type
      Parameters:
      eventType - The event type
      Returns:
      The event publisher, never null
    • inject

      @NonNull <T> T inject(@NonNull T instance)
      Inject an existing instance.
      Type Parameters:
      T - The bean generic type
      Parameters:
      instance - The instance to inject
      Returns:
      The instance to inject
    • createBean

      @NonNull default <T> T createBean(@NonNull @NonNull Class<T> beanType)
      Creates a new instance of the given bean performing dependency injection and returning a new instance.

      Note that the instance returned is not saved as a singleton in the context.

      Type Parameters:
      T - The bean generic type
      Parameters:
      beanType - The bean type
      Returns:
      The instance
    • createBean

      @NonNull <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Qualifier<T> qualifier)
      Creates a new instance of the given bean performing dependency injection and returning a new instance.

      Note that the instance returned is not saved as a singleton in the context.

      Type Parameters:
      T - The bean generic type
      Parameters:
      beanType - The bean type
      qualifier - The qualifier
      Returns:
      The instance
    • createBean

      @NonNull <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Qualifier<T> qualifier, @Nullable @Nullable Map<String,Object> argumentValues)

      Creates a new instance of the given bean performing dependency injection and returning a new instance.

      If the bean defines any Parameter values then the values passed in the argumentValues parameter will be used

      Note that the instance returned is not saved as a singleton in the context.

      Type Parameters:
      T - The bean generic type
      Parameters:
      beanType - The bean type
      qualifier - The qualifier
      argumentValues - The argument values
      Returns:
      The instance
    • createBean

      @NonNull <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Qualifier<T> qualifier, @Nullable @Nullable Object... args)

      Creates a new instance of the given bean performing dependency injection and returning a new instance.

      If the bean defines any Parameter values then the values passed in the argumentValues parameter will be used

      Note that the instance returned is not saved as a singleton in the context.

      Type Parameters:
      T - The bean generic type
      Parameters:
      beanType - The bean type
      qualifier - The qualifier
      args - The argument values
      Returns:
      The instance
    • createBean

      @NonNull default <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Object... args)

      Creates a new instance of the given bean performing dependency injection and returning a new instance.

      If the bean defines any Parameter values then the values passed in the argumentValues parameter will be used

      Note that the instance returned is not saved as a singleton in the context.

      Type Parameters:
      T - The bean generic type
      Parameters:
      beanType - The bean type
      args - The argument values
      Returns:
      The instance
    • createBean

      @NonNull default <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Map<String,Object> argumentValues)

      Creates a new instance of the given bean performing dependency injection and returning a new instance.

      If the bean defines any Parameter values then the values passed in the argumentValues parameter will be used

      Note that the instance returned is not saved as a singleton in the context.

      Type Parameters:
      T - The bean generic type
      Parameters:
      beanType - The bean type
      argumentValues - The argument values
      Returns:
      The instance
    • destroyBean

      @Nullable <T> T destroyBean(@NonNull @NonNull Class<T> beanType)
      Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be destroyed and removed from the context, otherwise null will be returned.
      Type Parameters:
      T - The concrete class
      Parameters:
      beanType - The bean type
      Returns:
      The destroy instance or null if no such bean exists
    • destroyBean

      @Nullable default <T> T destroyBean(@NonNull @NonNull Argument<T> beanType)
      Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be destroyed and removed from the context, otherwise null will be returned.
      Type Parameters:
      T - The concrete class
      Parameters:
      beanType - The bean type
      Returns:
      The destroy instance or null if no such bean exists
      Since:
      3.0.0
    • destroyBean

      @Nullable <T> T destroyBean(@NonNull @NonNull Argument<T> beanType, @Nullable @Nullable Qualifier<T> qualifier)
      Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be destroyed and removed from the context, otherwise null will be returned.
      Type Parameters:
      T - The concrete class
      Parameters:
      beanType - The bean type
      qualifier - The qualifier
      Returns:
      The destroy instance or null if no such bean exists
      Since:
      3.0.0
    • destroyBean

      @NonNull <T> T destroyBean(@NonNull T bean)
      Destroys the given bean.
      Type Parameters:
      T - The concrete class
      Parameters:
      bean - The bean
      Returns:
      The destroy instance
      Since:
      3.0.0
    • destroyBean

      @NonNull <T> void destroyBean(@NonNull @NonNull BeanRegistration<T> beanRegistration)
      Destroys the given bean.
      Type Parameters:
      T - The bean type
      Parameters:
      beanRegistration - The bean registration
      Since:
      3.5.0
    • refreshBean

      @NonNull <T> @NonNull Optional<T> refreshBean(@Nullable @Nullable BeanIdentifier identifier)

      Refresh the state of the given registered bean applying dependency injection and configuration wiring again.

      Note that if the bean was produced by a Factory then this method will refresh the factory too

      Type Parameters:
      T - The concrete class
      Parameters:
      identifier - The BeanIdentifier
      Returns:
      An Optional of the instance if it exists for the given registration
    • refreshBean

      @NonNull <T> void refreshBean(@NonNull @NonNull BeanRegistration<T> beanRegistration)

      Refresh the state of the given registered bean applying dependency injection and configuration wiring again.

      Note that if the bean was produced by a Factory then this method will refresh the factory too

      This methods skips an additional resolution of the BeanRegistration.
      Type Parameters:
      T - The concrete class
      Parameters:
      beanRegistration - The BeanRegistration
      Since:
      3.5.0
    • getClassLoader

      @NonNull @NonNull ClassLoader getClassLoader()
      Returns:
      The class loader used by this context
    • getBeanValidator

      Returns:
      Get the configured bean validator, if any.
    • registerSingleton

      @NonNull <T> @NonNull BeanContext 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 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 BeanContext 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 BeanDefinitionRegistry
      Parameters:
      singleton - The singleton bean
      Returns:
      This bean context
    • registerSingleton

      default <T> BeanContext registerSingleton(Class<T> type, T singleton, 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 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> BeanContext 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 BeanDefinitionRegistry
      Type Parameters:
      T - The concrete type
      Parameters:
      type - the bean type
      singleton - The singleton bean
      Returns:
      This bean context
    • registerSingleton

      @NonNull default @NonNull BeanContext 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 BeanDefinitionRegistry
      Parameters:
      singleton - The singleton bean
      inject - Whether the singleton should be injected (defaults to true)
      Returns:
      This bean context
    • run

      Run the BeanContext. This method will instantiate a new BeanContext and call LifeCycle.start().
      Returns:
      The running BeanContext
    • build

      @NonNull static @NonNull BeanContext build()
      Build a BeanContext.
      Returns:
      The built, but not yet running BeanContext
    • run

      @NonNull static @NonNull BeanContext run(ClassLoader classLoader)
      Run the BeanContext. This method will instantiate a new BeanContext and call LifeCycle.start().
      Parameters:
      classLoader - The classloader to use
      Returns:
      The running BeanContext
    • build

      @NonNull static @NonNull BeanContext build(ClassLoader classLoader)
      Build a BeanContext.
      Parameters:
      classLoader - The classloader to use
      Returns:
      The built, but not yet running BeanContext