Interface BeanContext

    • Method Detail

      • getEventPublisher

        @NonNull
        default <E> ApplicationEventPublisher<E> getEventPublisher​(@NonNull
                                                                   java.lang.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
      • publishEvent

        @Deprecated
        void publishEvent​(java.lang.Object event)
        Deprecated.
        Preferred way is to use event typed ApplicationEventPublisher<MyEventType>
        Publish the given event. The event will be published synchronously and only return once all listeners have consumed the event.
        Specified by:
        publishEvent in interface ApplicationEventPublisher<java.lang.Object>
        Parameters:
        event - The event to publish
      • publishEventAsync

        @Deprecated
        default java.util.concurrent.Future<java.lang.Void> publishEventAsync​(java.lang.Object event)
        Deprecated.
        Preferred way is to use event typed ApplicationEventPublisher<MyEventType>
        Publish the given event. The event will be published asynchronously. A future is returned that can be used to check whether the event completed successfully or not.
        Specified by:
        publishEventAsync in interface ApplicationEventPublisher<java.lang.Object>
        Parameters:
        event - The event to publish
        Returns:
        A future that completes when the event is published
      • 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
                                 java.lang.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
                         java.lang.Class<T> beanType,
                         @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
                         java.lang.Class<T> beanType,
                         @Nullable
                         Qualifier<T> qualifier,
                         @Nullable
                         java.util.Map<java.lang.String,​java.lang.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
                         java.lang.Class<T> beanType,
                         @Nullable
                         Qualifier<T> qualifier,
                         @Nullable
                         java.lang.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
                                 java.lang.Class<T> beanType,
                                 @Nullable
                                 java.lang.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
                                 java.lang.Class<T> beanType,
                                 @Nullable
                                 java.util.Map<java.lang.String,​java.lang.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
                          java.lang.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
                                  io.micronaut.core.type.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
                          io.micronaut.core.type.Argument<T> beanType,
                          @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
                             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> java.util.Optional<T> refreshBean​(@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
                             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
        java.lang.ClassLoader getClassLoader()
        Returns:
        The class loader used by this context
      • getBeanValidator

        @NonNull
        BeanDefinitionValidator getBeanValidator()
        Returns:
        Get the configured bean validator, if any.
      • registerSingleton

        @NonNull
        <T> BeanContext registerSingleton​(@NonNull
                                          java.lang.Class<T> type,
                                          @NonNull
                                          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 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
                                              java.lang.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​(java.lang.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​(java.lang.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 BeanContext registerSingleton​(@NonNull
                                              java.lang.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
      • build

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