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>,ThrowingSupplier<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
registeredBeanand resolvedargumentsfor executables that require arguments resolution. Anintrospectionhint is added so that parameter annotations can be read - A function with only the
registeredBeanfor simpler cases that do not require resolution of arguments - A supplier when a method reference can be used
- Since:
- 6.0
- Author:
- Phillip Webb, Stephane Nicoll
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> BeanInstanceSupplier<T>forConstructor(Class<?>... parameterTypes) Create aBeanInstanceSupplierthat resolves arguments for the specified bean constructor.static <T> BeanInstanceSupplier<T>forFactoryMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) Create a newBeanInstanceSupplierthat resolves arguments for the specified factory method.get(RegisteredBean registeredBean) Get the supplied instance.Return the factory method that this supplier uses to create the instance, ornullif it is not known or this supplier uses another means.protected final voidregisterDependentBeans(ConfigurableBeanFactory beanFactory, String beanName, Set<String> autowiredBeanNames) withGenerator(ThrowingBiFunction<RegisteredBean, AutowiredArguments, T> generator) Return a newBeanInstanceSupplierinstance that uses the specifiedgeneratorbi-function to instantiate the underlying bean.withGenerator(ThrowingFunction<RegisteredBean, T> generator) Return a newBeanInstanceSupplierinstance that uses the specifiedgeneratorfunction to instantiate the underlying bean.withGenerator(ThrowingSupplier<T> generator) Deprecated, for removal: This API element is subject to removal in a future version.withShortcuts(String... beanNames) Return a newBeanInstanceSupplierinstance that uses direct bean name injection shortcuts for specific parameters.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.beans.factory.support.InstanceSupplier
andThen, getWithExceptionMethods inherited from interface org.springframework.util.function.ThrowingSupplier
get, get, throwing
-
Method Details
-
forConstructor
Create aBeanInstanceSupplierthat resolves arguments for the specified bean constructor.- Type Parameters:
T- the type of instance supplied- Parameters:
parameterTypes- the constructor parameter types- Returns:
- a new
BeanInstanceSupplierinstance
-
forFactoryMethod
public static <T> BeanInstanceSupplier<T> forFactoryMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) Create a newBeanInstanceSupplierthat resolves arguments for the specified factory method.- Type Parameters:
T- the type of instance supplied- Parameters:
declaringClass- the class that declares the factory methodmethodName- the factory method nameparameterTypes- the factory method parameter types- Returns:
- a new
BeanInstanceSupplierinstance
-
withGenerator
public BeanInstanceSupplier<T> withGenerator(ThrowingBiFunction<RegisteredBean, AutowiredArguments, T> generator) Return a newBeanInstanceSupplierinstance that uses the specifiedgeneratorbi-function to instantiate the underlying bean.- Parameters:
generator- aThrowingBiFunctionthat uses theRegisteredBeanand resolvedAutowiredArgumentsto instantiate the underlying bean- Returns:
- a new
BeanInstanceSupplierinstance with the specified generator
-
withGenerator
Return a newBeanInstanceSupplierinstance that uses the specifiedgeneratorfunction to instantiate the underlying bean.- Parameters:
generator- aThrowingFunctionthat uses theRegisteredBeanto instantiate the underlying bean- Returns:
- a new
BeanInstanceSupplierinstance with the specified generator
-
withGenerator
@Deprecated(since="6.0.11", forRemoval=true) public BeanInstanceSupplier<T> withGenerator(ThrowingSupplier<T> generator) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofwithGenerator(ThrowingFunction)Return a newBeanInstanceSupplierinstance that uses the specifiedgeneratorsupplier to instantiate the underlying bean.- Parameters:
generator- aThrowingSupplierto instantiate the underlying bean- Returns:
- a new
BeanInstanceSupplierinstance with the specified generator
-
withShortcuts
Return a newBeanInstanceSupplierinstance 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
BeanInstanceSupplierinstance that uses the shortcuts
-
get
Description copied from interface:InstanceSupplierGet the supplied instance.- Specified by:
getin interfaceInstanceSupplier<T>- Parameters:
registeredBean- the registered bean requesting the instance- Returns:
- the supplied instance
- Throws:
Exception- on error
-
getFactoryMethod
Description copied from interface:InstanceSupplierReturn the factory method that this supplier uses to create the instance, ornullif it is not known or this supplier uses another means.- Specified by:
getFactoryMethodin interfaceInstanceSupplier<T>- Returns:
- the factory method used to create the instance, or
null
-
registerDependentBeans
protected final void registerDependentBeans(ConfigurableBeanFactory beanFactory, String beanName, Set<String> autowiredBeanNames)
-
withGenerator(ThrowingFunction)