Top Description Inners Fields Constructors Methods
org.springframework.beans.factory.support

pack-priv Class ConstructorResolver

extends Object
Class Inheritance
Imports
java.beans.ConstructorProperties, java.lang.reflect.Array, .Constructor, .Executable, .Method, .Modifier, java.util.ArrayDeque, .ArrayList, .Arrays, .Collection, .Collections, .Deque, .HashSet, .LinkedHashSet, .List, .Map, .Set, java.util.function.Function, .Predicate, .Supplier, org.apache.commons.logging.Log, org.springframework.beans.BeanMetadataElement, .BeanUtils, .BeanWrapper, .BeanWrapperImpl, .BeansException, .TypeConverter, .TypeMismatchException, org.springframework.beans.factory.BeanCreationException, .BeanDefinitionStoreException, .BeanFactory, .FactoryBean, .InjectionPoint, .NoSuchBeanDefinitionException, .NoUniqueBeanDefinitionException, .UnsatisfiedDependencyException, org.springframework.beans.factory.config.AutowireCapableBeanFactory, .BeanDefinition, .BeanReference, .ConstructorArgumentValues, .ConstructorArgumentValues.ValueHolder, .DependencyDescriptor, .RuntimeBeanReference, .TypedStringValue, org.springframework.core.CollectionFactory, .MethodParameter, .NamedThreadLocal, .ParameterNameDiscoverer, .ResolvableType, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .MethodInvoker, .ObjectUtils, .ReflectionUtils, .StringUtils

Delegate for resolving constructors and factory methods.

Performs constructor resolution through argument matching.

Authors
Juergen Hoeller, Rob Harrop, Mark Fisher, Costin Leau, Sebastien Deleuze, Sam Brannen, Stephane Nicoll, Phillip Webb
Since
2.0
See Also
autowireConstructor, instantiateUsingFactoryMethod, resolveConstructorOrFactoryMethod, AbstractAutowireCapableBeanFactory

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ConstructorResolver.ArgumentsHolder

Private inner class for holding argument combinations.

private static class
ConstructorResolver.ConstructorDependencyDescriptor

DependencyDescriptor marker for constructor arguments, for differentiating between a provided DependencyDescriptor instance and an internally built DependencyDescriptor for autowiring purposes.

private static class
private static enum

Field Summary

Modifier and TypeField and Description
private final AbstractAutowireCapableBeanFactory
private static final NamedThreadLocal<InjectionPoint>
private static final Object[]
private final Log

Constructor Summary

AccessConstructor and Description
public
ConstructorResolver(AbstractAutowireCapableBeanFactory
the BeanFactory to work with
beanFactory
)

Create a new ConstructorResolver for the given factory and instantiation strategy.

Method Summary

Modifier and TypeMethod and Description
public BeanWrapper

Returns:

a BeanWrapper for the new instance
autowireConstructor
(String
the name of the bean
beanName
,
RootBeanDefinition
the merged bean definition for the bean
mbd
,
Constructor<?>[]
chosen candidate constructors (or null if none)
chosenCtors
,
Object[]
argument values passed in programmatically via the getBean method, or null if none (-> use constructor argument values from bean definition)
explicitArgs
)

"autowire constructor" (with constructor arguments by type) behavior.

private ConstructorResolver.ArgumentsHolder
createArgumentArray(String beanName, RootBeanDefinition mbd, ConstructorArgumentValues resolvedValues, BeanWrapper bw, Class<?>[] paramTypes, String[] paramNames, Executable executable, boolean autowiring, boolean fallback)

Create an array of arguments to invoke a constructor or factory method, given the resolved constructor argument values.

private ResolvableType
private List<ResolvableType>
pack-priv static Constructor<?>[]
private ResolvableType
private ResolvableType
private Method[]
getCandidateMethods(Class<?> factoryClass, RootBeanDefinition mbd)

Retrieve all candidate methods for the given class, considering the RootBeanDefinition#isNonPublicAccessAllowed() flag.

private Class<?>
private Constructor<?>
private Object
instantiate(String beanName, RootBeanDefinition mbd, Constructor<?> constructorToUse, Object[] argsToUse)

private Object
instantiate(String beanName, RootBeanDefinition mbd, Object factoryBean, Method factoryMethod, Object[] args)

public BeanWrapper

Returns:

a BeanWrapper for the new instance
instantiateUsingFactoryMethod
(String
the name of the bean
beanName
,
RootBeanDefinition
the merged bean definition for the bean
mbd
,
Object[]
argument values passed in programmatically via the getBean method, or null if none (-> use constructor argument values from bean definition)
explicitArgs
)

Instantiate the bean using a named factory method.

private Predicate<ResolvableType>
private boolean
isMatch(ResolvableType parameterType, ResolvableType valueType, ConstructorResolver.FallbackMode fallbackMode)

private boolean
isParamMismatch(Method uniqueCandidate, Method candidate)

private Predicate<ResolvableType>
private boolean
isStaticCandidate(Method method, Class<?> factoryClass)

private Predicate<ResolvableType>

Returns:

a predicate to indicate a fallback match for a String to Class parameter
isStringForClassFallback
(ResolvableType
the type of the value
valueType
)

Return a Predicate for a parameter type that checks if its target value is a Class and the value type is a String.

private boolean
match(List<ResolvableType> parameterTypes, List<ResolvableType> valueTypes, ConstructorResolver.FallbackMode fallbackMode)

private void
registerDependentBeans(Executable executable, String beanName, Set<String> autowiredBeanNames)

pack-priv Object
resolveAutowiredArgument(DependencyDescriptor descriptor, Class<?> paramType, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter, boolean fallback)

Resolve the specified argument which is supposed to be autowired.

private Constructor<?>
private int
resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw, ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues)

Resolve the constructor arguments for this bean into the resolvedValues object.

public Executable
private Method
private Method
resolveFactoryMethod(List<Method> executables, Function<Method, List<ResolvableType>> parameterTypesFactory, List<ResolvableType> valueTypes)

public void
resolveFactoryMethodIfPossible(RootBeanDefinition
the bean definition to check
mbd
)

Resolve the factory method in the specified bean definition, if possible.

private Object[]
resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw, Executable executable, Object[] argsToResolve)

Resolve the prepared arguments stored in the given bean definition.

pack-priv static InjectionPoint
private void
private Predicate<ResolvableType>
private Predicate<ResolvableType>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanFactoryback to summary
private final AbstractAutowireCapableBeanFactory beanFactory
currentInjectionPointback to summary
private static final NamedThreadLocal<InjectionPoint> currentInjectionPoint
EMPTY_ARGSback to summary
private static final Object[] EMPTY_ARGS
loggerback to summary
private final Log logger

Constructor Detail

ConstructorResolverback to summary
public ConstructorResolver(AbstractAutowireCapableBeanFactory beanFactory)

Create a new ConstructorResolver for the given factory and instantiation strategy.

Parameters
beanFactory:AbstractAutowireCapableBeanFactory

the BeanFactory to work with

Method Detail

autowireConstructorback to summary
public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd, Constructor<?>[] chosenCtors, Object[] explicitArgs)

"autowire constructor" (with constructor arguments by type) behavior. Also applied if explicit constructor argument values are specified.

Parameters
beanName:String

the name of the bean

mbd:RootBeanDefinition

the merged bean definition for the bean

chosenCtors:Constructor<?>[]

chosen candidate constructors (or null if none)

explicitArgs:Object[]

argument values passed in programmatically via the getBean method, or null if none (-> use constructor argument values from bean definition)

Returns:BeanWrapper

a BeanWrapper for the new instance

createArgumentArrayback to summary
private ConstructorResolver.ArgumentsHolder createArgumentArray(String beanName, RootBeanDefinition mbd, ConstructorArgumentValues resolvedValues, BeanWrapper bw, Class<?>[] paramTypes, String[] paramNames, Executable executable, boolean autowiring, boolean fallback) throws UnsatisfiedDependencyException

Create an array of arguments to invoke a constructor or factory method, given the resolved constructor argument values.

determineParameterValueTypeback to summary
private ResolvableType determineParameterValueType(RootBeanDefinition mbd, ConstructorArgumentValues.ValueHolder valueHolder)
determineParameterValueTypesback to summary
private List<ResolvableType> determineParameterValueTypes(RootBeanDefinition mbd)
determinePreferredConstructorsback to summary
pack-priv static Constructor<?>[] determinePreferredConstructors(Class<?> clazz)

See BeanUtils#getResolvableConstructor(Class) for alignment. This variant adds a lenient fallback to the default constructor if available, similar to org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors.

Annotations
@Nullable
extractElementTypeback to summary
private ResolvableType extractElementType(ResolvableType parameterType)
getBeanTypeback to summary
private ResolvableType getBeanType(String beanName, RootBeanDefinition mbd)
getCandidateMethodsback to summary
private Method[] getCandidateMethods(Class<?> factoryClass, RootBeanDefinition mbd)

Retrieve all candidate methods for the given class, considering the RootBeanDefinition#isNonPublicAccessAllowed() flag. Called as the starting point for factory method determination.

getFactoryBeanClassback to summary
private Class<?> getFactoryBeanClass(String beanName, RootBeanDefinition mbd)
Annotations
@Nullable
getUserDeclaredConstructorback to summary
private Constructor<?> getUserDeclaredConstructor(Constructor<?> constructor)
instantiateback to summary
private Object instantiate(String beanName, RootBeanDefinition mbd, Constructor<?> constructorToUse, Object[] argsToUse)
instantiateback to summary
private Object instantiate(String beanName, RootBeanDefinition mbd, Object factoryBean, Method factoryMethod, Object[] args)
instantiateUsingFactoryMethodback to summary
public BeanWrapper instantiateUsingFactoryMethod(String beanName, RootBeanDefinition mbd, Object[] explicitArgs)

Instantiate the bean using a named factory method. The method may be static, if the bean definition parameter specifies a class, rather than a "factory-bean", or an instance variable on a factory object itself configured using Dependency Injection.

Implementation requires iterating over the static or instance methods with the name specified in the RootBeanDefinition (the method may be overloaded) and trying to match with the parameters. We don't have the types attached to constructor args, so trial and error is the only way to go here. The explicitArgs array may contain argument values passed in programmatically via the corresponding getBean method.

Parameters
beanName:String

the name of the bean

mbd:RootBeanDefinition

the merged bean definition for the bean

explicitArgs:Object[]

argument values passed in programmatically via the getBean method, or null if none (-> use constructor argument values from bean definition)

Returns:BeanWrapper

a BeanWrapper for the new instance

isAssignableback to summary
private Predicate<ResolvableType> isAssignable(ResolvableType valueType)
isMatchback to summary
private boolean isMatch(ResolvableType parameterType, ResolvableType valueType, ConstructorResolver.FallbackMode fallbackMode)
isParamMismatchback to summary
private boolean isParamMismatch(Method uniqueCandidate, Method candidate)
isSimpleValueTypeback to summary
private Predicate<ResolvableType> isSimpleValueType(ResolvableType valueType)
isStaticCandidateback to summary
private boolean isStaticCandidate(Method method, Class<?> factoryClass)
isStringForClassFallbackback to summary
private Predicate<ResolvableType> isStringForClassFallback(ResolvableType valueType)

Return a Predicate for a parameter type that checks if its target value is a Class and the value type is a String. This is a regular use cases where a Class is defined in the bean definition as an FQN.

Parameters
valueType:ResolvableType

the type of the value

Returns:Predicate<ResolvableType>

a predicate to indicate a fallback match for a String to Class parameter

matchback to summary
private boolean match(List<ResolvableType> parameterTypes, List<ResolvableType> valueTypes, ConstructorResolver.FallbackMode fallbackMode)
registerDependentBeansback to summary
private void registerDependentBeans(Executable executable, String beanName, Set<String> autowiredBeanNames)
resolveAutowiredArgumentback to summary
pack-priv Object resolveAutowiredArgument(DependencyDescriptor descriptor, Class<?> paramType, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter, boolean fallback)

Resolve the specified argument which is supposed to be autowired.

Annotations
@Nullable
resolveConstructorback to summary
private Constructor<?> resolveConstructor(String beanName, RootBeanDefinition mbd, Supplier<ResolvableType> beanType, List<ResolvableType> valueTypes)
Annotations
@Nullable
resolveConstructorArgumentsback to summary
private int resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw, ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues)

Resolve the constructor arguments for this bean into the resolvedValues object. This may involve looking up other beans.

This method is also used for handling invocations of static factory methods.

resolveConstructorOrFactoryMethodback to summary
public Executable resolveConstructorOrFactoryMethod(String beanName, RootBeanDefinition mbd)
resolveFactoryMethodback to summary
private Method resolveFactoryMethod(String beanName, RootBeanDefinition mbd, List<ResolvableType> valueTypes)
Annotations
@Nullable
resolveFactoryMethodback to summary
private Method resolveFactoryMethod(List<Method> executables, Function<Method, List<ResolvableType>> parameterTypesFactory, List<ResolvableType> valueTypes)
Annotations
@Nullable
resolveFactoryMethodIfPossibleback to summary
public void resolveFactoryMethodIfPossible(RootBeanDefinition mbd)

Resolve the factory method in the specified bean definition, if possible. RootBeanDefinition#getResolvedFactoryMethod() can be checked for the result.

Parameters
mbd:RootBeanDefinition

the bean definition to check

resolvePreparedArgumentsback to summary
private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw, Executable executable, Object[] argsToResolve)

Resolve the prepared arguments stored in the given bean definition.

setCurrentInjectionPointback to summary
pack-priv static InjectionPoint setCurrentInjectionPoint(InjectionPoint injectionPoint)
setShortcutIfPossibleback to summary
private void setShortcutIfPossible(ConstructorResolver.ConstructorDependencyDescriptor descriptor, Class<?> paramType, Set<String> autowiredBeanNames)
typeConversionFallbackback to summary
private Predicate<ResolvableType> typeConversionFallback(ResolvableType valueType)
valueOrCollectionback to summary
private Predicate<ResolvableType> valueOrCollection(ResolvableType valueType, Function<ResolvableType, Predicate<ResolvableType>> predicateProvider)
org.springframework.beans.factory.support back to summary

private Class ConstructorResolver.ArgumentsHolder

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.beans.factory.support.ConstructorResolver.ArgumentsHolder

Private inner class for holding argument combinations.

Field Summary

Modifier and TypeField and Description
public final Object[]
public final Object[]
public final Object[]
public boolean

Constructor Summary

AccessConstructor and Description
public
ArgumentsHolder(int size)

public

Method Summary

Modifier and TypeMethod and Description
public int
getAssignabilityWeight(Class<?>[] paramTypes)

public int
getTypeDifferenceWeight(Class<?>[] paramTypes)

public void
storeCache(RootBeanDefinition mbd, Executable constructorOrFactoryMethod)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

argumentsback to summary
public final Object[] arguments
preparedArgumentsback to summary
public final Object[] preparedArguments
rawArgumentsback to summary
public final Object[] rawArguments
resolveNecessaryback to summary
public boolean resolveNecessary

Constructor Detail

ArgumentsHolderback to summary
public ArgumentsHolder(int size)
ArgumentsHolderback to summary
public ArgumentsHolder(Object[] args)

Method Detail

getAssignabilityWeightback to summary
public int getAssignabilityWeight(Class<?>[] paramTypes)
getTypeDifferenceWeightback to summary
public int getTypeDifferenceWeight(Class<?>[] paramTypes)
storeCacheback to summary
public void storeCache(RootBeanDefinition mbd, Executable constructorOrFactoryMethod)
org.springframework.beans.factory.support back to summary

private Class ConstructorResolver.ConstructorDependencyDescriptor

extends DependencyDescriptor
Class Inheritance
Annotations
@SuppressWarnings:serial

DependencyDescriptor marker for constructor arguments, for differentiating between a provided DependencyDescriptor instance and an internally built DependencyDescriptor for autowiring purposes.

Field Summary

Modifier and TypeField and Description
private volatile String

Constructor Summary

AccessConstructor and Description
public
ConstructorDependencyDescriptor(MethodParameter methodParameter, boolean required)

Method Summary

Modifier and TypeMethod and Description
public boolean
public Object
resolveShortcut(BeanFactory
the associated factory
beanFactory
)

Overrides org.springframework.beans.factory.config.DependencyDescriptor.resolveShortcut.

Resolve a shortcut for this dependency against the given factory, for example taking some pre-resolved information into account.
public void
setShortcut(String shortcut)

Inherited from org.springframework.beans.factory.config.DependencyDescriptor:
equalsfallbackMatchAllowedforFallbackMatchgetDependencyNamegetDependencyTypegetResolvableTypegetTypeDescriptorhashCodeincreaseNestingLevelinitParameterNameDiscoveryisEagerisRequiredresolveCandidateresolveNotUniquesetContainingClasssupportsLazyResolution

Field Detail

shortcutback to summary
private volatile String shortcut
Annotations
@Nullable

Constructor Detail

ConstructorDependencyDescriptorback to summary
public ConstructorDependencyDescriptor(MethodParameter methodParameter, boolean required)

Method Detail

hasShortcutback to summary
public boolean hasShortcut()
resolveShortcutback to summary
public Object resolveShortcut(BeanFactory beanFactory)

Overrides org.springframework.beans.factory.config.DependencyDescriptor.resolveShortcut.

Doc from org.springframework.beans.factory.config.DependencyDescriptor.resolveShortcut.

Resolve a shortcut for this dependency against the given factory, for example taking some pre-resolved information into account.

The resolution algorithm will first attempt to resolve a shortcut through this method before going into the regular type matching algorithm across all beans. Subclasses may override this method to improve resolution performance based on pre-cached information while still receiving InjectionPoint exposure etc.

Parameters
beanFactory:BeanFactory

the associated factory

Returns:Object

the shortcut result if any, or null if none

Annotations
@Override
setShortcutback to summary
public void setShortcut(String shortcut)
org.springframework.beans.factory.support back to summary

private Class ConstructorResolver.ConstructorPropertiesChecker

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.beans.factory.support.ConstructorResolver.ConstructorPropertiesChecker

Delegate for checking Java's ConstructorProperties annotation.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String[]
evaluate(Constructor<?> candidate, int paramCount)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ConstructorPropertiesCheckerback to summary
private ConstructorPropertiesChecker()

Method Detail

evaluateback to summary
public static String[] evaluate(Constructor<?> candidate, int paramCount)
Annotations
@Nullable
org.springframework.beans.factory.support back to summary

private final Enum ConstructorResolver.FallbackMode

extends Enum<ConstructorResolver.FallbackMode>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final ConstructorResolver.FallbackMode
public static final ConstructorResolver.FallbackMode
public static final ConstructorResolver.FallbackMode

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ConstructorResolver.FallbackMode
public static ConstructorResolver.FallbackMode[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

ASSIGNABLE_ELEMENTback to summary
public static final ConstructorResolver.FallbackMode ASSIGNABLE_ELEMENT
NONEback to summary
public static final ConstructorResolver.FallbackMode NONE
TYPE_CONVERSIONback to summary
public static final ConstructorResolver.FallbackMode TYPE_CONVERSION

Constructor Detail

FallbackModeback to summary
private FallbackMode()

Method Detail

valueOfback to summary
public static ConstructorResolver.FallbackMode valueOf(String name)
valuesback to summary
public static ConstructorResolver.FallbackMode[] values()