Class BeanInstanceSupplier<T>

java.lang.Object
org.springframework.beans.factory.aot.BeanInstanceSupplier<T>
Type Parameters:
T - the type of instance supplied by this supplier
All Implemented Interfaces:
Supplier<T>, InstanceSupplier<T>, org.springframework.util.function.ThrowingSupplier<T>

public final class BeanInstanceSupplier<T> extends Object implements InstanceSupplier<T>
Specialized InstanceSupplier that provides the factory Method used to instantiate the underlying bean instance, if any. Transparently handles resolution of AutowiredArguments if necessary. Typically used in AOT-processed applications as a targeted alternative to the reflection based injection.

If no generator is provided, reflection is used to instantiate the bean instance, and full invocation hints are contributed. Multiple generator callback styles are supported:

  • A function with the registeredBean and resolved arguments for executables that require arguments resolution. An introspection hint is added so that parameter annotations can be read
  • A function with only the registeredBean for simpler cases that do not require resolution of arguments
  • A supplier when a method reference can be used
Generator callbacks handle checked exceptions so that the caller does not have to deal with them.
Since:
6.0
Author:
Phillip Webb, Stephane Nicoll
See Also:
  • Method Details

    • forConstructor

      public static <T> BeanInstanceSupplier<T> forConstructor(Class<?>... parameterTypes)
      Create a BeanInstanceSupplier that resolves arguments for the specified bean constructor.
      Type Parameters:
      T - the type of instance supplied
      Parameters:
      parameterTypes - the constructor parameter types
      Returns:
      a new BeanInstanceSupplier instance
    • forFactoryMethod

      public static <T> BeanInstanceSupplier<T> forFactoryMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes)
      Create a new BeanInstanceSupplier that resolves arguments for the specified factory method.
      Type Parameters:
      T - the type of instance supplied
      Parameters:
      declaringClass - the class that declares the factory method
      methodName - the factory method name
      parameterTypes - the factory method parameter types
      Returns:
      a new BeanInstanceSupplier instance
    • withGenerator

      public BeanInstanceSupplier<T> withGenerator(org.springframework.util.function.ThrowingBiFunction<RegisteredBean,AutowiredArguments,T> generator)
      Return a new BeanInstanceSupplier instance that uses the specified generator bi-function to instantiate the underlying bean.
      Parameters:
      generator - a ThrowingBiFunction that uses the RegisteredBean and resolved AutowiredArguments to instantiate the underlying bean
      Returns:
      a new BeanInstanceSupplier instance with the specified generator
    • withGenerator

      public BeanInstanceSupplier<T> withGenerator(org.springframework.util.function.ThrowingFunction<RegisteredBean,T> generator)
      Return a new BeanInstanceSupplier instance that uses the specified generator function to instantiate the underlying bean.
      Parameters:
      generator - a ThrowingFunction that uses the RegisteredBean to instantiate the underlying bean
      Returns:
      a new BeanInstanceSupplier instance with the specified generator
    • withGenerator

      @Deprecated(since="6.0.11", forRemoval=true) public BeanInstanceSupplier<T> withGenerator(org.springframework.util.function.ThrowingSupplier<T> generator)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Return a new BeanInstanceSupplier instance that uses the specified generator supplier to instantiate the underlying bean.
      Parameters:
      generator - a ThrowingSupplier to instantiate the underlying bean
      Returns:
      a new BeanInstanceSupplier instance with the specified generator
    • withShortcuts

      public BeanInstanceSupplier<T> withShortcuts(String... beanNames)
      Return a new BeanInstanceSupplier instance that uses direct bean name injection shortcuts for specific parameters.
      Parameters:
      beanNames - the bean names to use as shortcuts (aligned with the constructor or factory method parameters)
      Returns:
      a new BeanInstanceSupplier instance that uses the shortcuts
    • get

      public T get(RegisteredBean registeredBean) throws Exception
      Description copied from interface: InstanceSupplier
      Get the supplied instance.
      Specified by:
      get in interface InstanceSupplier<T>
      Parameters:
      registeredBean - the registered bean requesting the instance
      Returns:
      the supplied instance
      Throws:
      Exception - on error
    • getFactoryMethod

      @Nullable public Method getFactoryMethod()
      Description copied from interface: InstanceSupplier
      Return the factory method that this supplier uses to create the instance, or null if it is not known or this supplier uses another means.
      Specified by:
      getFactoryMethod in interface InstanceSupplier<T>
      Returns:
      the factory method used to create the instance, or null
    • registerDependentBeans

      protected final void registerDependentBeans(ConfigurableBeanFactory beanFactory, String beanName, Set<String> autowiredBeanNames)