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
registeredBean
and resolvedarguments
for executables that require arguments resolution. Anintrospection
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
- Since:
- 6.0
- Author:
- Phillip Webb, Stephane Nicoll
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> BeanInstanceSupplier<T>
forConstructor
(Class<?>... parameterTypes) Create aBeanInstanceSupplier
that resolves arguments for the specified bean constructor.static <T> BeanInstanceSupplier<T>
forFactoryMethod
(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) Create a newBeanInstanceSupplier
that resolves arguments for the specified factory method.get
(RegisteredBean registeredBean) Gets the supplied instance.Return the factory method that this supplier uses to create the instance, ornull
if it is not known or this supplier uses another mean.protected final void
registerDependentBeans
(ConfigurableBeanFactory beanFactory, String beanName, Set<String> autowiredBeanNames) withGenerator
(ThrowingBiFunction<RegisteredBean, AutowiredArguments, T> generator) Return a newBeanInstanceSupplier
instance that uses the specifiedgenerator
bi-function to instantiate the underlying bean.withGenerator
(ThrowingFunction<RegisteredBean, T> generator) Return a newBeanInstanceSupplier
instance that uses the specifiedgenerator
function to instantiate the underlying bean.withGenerator
(ThrowingSupplier<T> generator) Return a newBeanInstanceSupplier
instance that uses the specifiedgenerator
supplier to instantiate the underlying bean.withShortcuts
(String... beanNames) Return a newBeanInstanceSupplier
instance 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, wait
Methods inherited from interface org.springframework.beans.factory.support.InstanceSupplier
andThen, getWithException
Methods inherited from interface org.springframework.util.function.ThrowingSupplier
get, get, throwing
-
Method Details
-
forConstructor
Create aBeanInstanceSupplier
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 newBeanInstanceSupplier
that 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
BeanInstanceSupplier
instance
-
withGenerator
public BeanInstanceSupplier<T> withGenerator(ThrowingBiFunction<RegisteredBean, AutowiredArguments, T> generator) Return a newBeanInstanceSupplier
instance that uses the specifiedgenerator
bi-function to instantiate the underlying bean.- Parameters:
generator
- aThrowingBiFunction
that uses theRegisteredBean
and resolvedAutowiredArguments
to instantiate the underlying bean- Returns:
- a new
BeanInstanceSupplier
instance with the specified generator
-
withGenerator
Return a newBeanInstanceSupplier
instance that uses the specifiedgenerator
function to instantiate the underlying bean.- Parameters:
generator
- aThrowingFunction
that uses theRegisteredBean
to instantiate the underlying bean- Returns:
- a new
BeanInstanceSupplier
instance with the specified generator
-
withGenerator
Return a newBeanInstanceSupplier
instance that uses the specifiedgenerator
supplier to instantiate the underlying bean.- Parameters:
generator
- aThrowingSupplier
to instantiate the underlying bean- Returns:
- a new
BeanInstanceSupplier
instance with the specified generator
-
withShortcuts
Return a newBeanInstanceSupplier
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
Description copied from interface:InstanceSupplier
Gets the supplied instance.- Specified by:
get
in interfaceInstanceSupplier<T>
- Parameters:
registeredBean
- the registered bean requesting the instance- Returns:
- the supplied instance
- Throws:
Exception
- on error
-
getFactoryMethod
Description copied from interface:InstanceSupplier
Return the factory method that this supplier uses to create the instance, ornull
if it is not known or this supplier uses another mean.- Specified by:
getFactoryMethod
in 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)
-