org.springframework.beans.factory.support
Class ConstructorResolver

java.lang.Object
  extended by org.springframework.beans.factory.support.ConstructorResolver

 class ConstructorResolver
extends java.lang.Object

Helper class for resolving constructors and factory methods. Performs constructor resolution through argument matching.

Operates on an AbstractBeanFactory and an InstantiationStrategy. Used by AbstractAutowireCapableBeanFactory.

Since:
2.0
Author:
Juergen Hoeller, Rob Harrop, Mark Fisher, Costin Leau
See Also:
autowireConstructor(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.reflect.Constructor[], java.lang.Object[]), instantiateUsingFactoryMethod(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[]), AbstractAutowireCapableBeanFactory

Nested Class Summary
private static class ConstructorResolver.ArgumentsHolder
          Private inner class for holding argument combinations.
private static class ConstructorResolver.AutowiredArgumentMarker
          Marker for autowired arguments in a cached argument array.
private static class ConstructorResolver.ConstructorPropertiesChecker
          Inner class to avoid a Java 6 dependency.
 
Field Summary
private  AbstractAutowireCapableBeanFactory beanFactory
           
private static java.lang.String CONSTRUCTOR_PROPERTIES_CLASS_NAME
           
private static boolean constructorPropertiesAnnotationAvailable
           
 
Constructor Summary
ConstructorResolver(AbstractAutowireCapableBeanFactory beanFactory)
          Create a new ConstructorResolver for the given factory and instantiation strategy.
 
Method Summary
 BeanWrapper autowireConstructor(java.lang.String beanName, RootBeanDefinition mbd, java.lang.reflect.Constructor[] chosenCtors, java.lang.Object[] explicitArgs)
          "autowire constructor" (with constructor arguments by type) behavior.
private  ConstructorResolver.ArgumentsHolder createArgumentArray(java.lang.String beanName, RootBeanDefinition mbd, ConstructorArgumentValues resolvedValues, BeanWrapper bw, java.lang.Class[] paramTypes, java.lang.String[] paramNames, java.lang.Object methodOrCtor, boolean autowiring)
          Create an array of arguments to invoke a constructor or factory method, given the resolved constructor argument values.
 BeanWrapper instantiateUsingFactoryMethod(java.lang.String beanName, RootBeanDefinition mbd, java.lang.Object[] explicitArgs)
          Instantiate the bean using a named factory method.
protected  java.lang.Object resolveAutowiredArgument(MethodParameter param, java.lang.String beanName, java.util.Set<java.lang.String> autowiredBeanNames, TypeConverter typeConverter)
          Template method for resolving the specified argument which is supposed to be autowired.
private  int resolveConstructorArguments(java.lang.String beanName, RootBeanDefinition mbd, BeanWrapper bw, ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues)
          Resolve the constructor arguments for this bean into the resolvedValues object.
 void resolveFactoryMethodIfPossible(RootBeanDefinition mbd)
          Resolve the factory method in the specified bean definition, if possible.
private  java.lang.Object[] resolvePreparedArguments(java.lang.String beanName, RootBeanDefinition mbd, BeanWrapper bw, java.lang.reflect.Member methodOrCtor, java.lang.Object[] argsToResolve)
          Resolve the prepared arguments stored in the given bean definition.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONSTRUCTOR_PROPERTIES_CLASS_NAME

private static final java.lang.String CONSTRUCTOR_PROPERTIES_CLASS_NAME
See Also:
Constant Field Values

constructorPropertiesAnnotationAvailable

private static final boolean constructorPropertiesAnnotationAvailable

beanFactory

private final AbstractAutowireCapableBeanFactory beanFactory
Constructor Detail

ConstructorResolver

public ConstructorResolver(AbstractAutowireCapableBeanFactory beanFactory)
Create a new ConstructorResolver for the given factory and instantiation strategy.

Parameters:
beanFactory - the BeanFactory to work with
Method Detail

autowireConstructor

public BeanWrapper autowireConstructor(java.lang.String beanName,
                                       RootBeanDefinition mbd,
                                       java.lang.reflect.Constructor[] chosenCtors,
                                       java.lang.Object[] explicitArgs)
"autowire constructor" (with constructor arguments by type) behavior. Also applied if explicit constructor argument values are specified, matching all remaining arguments with beans from the bean factory.

This corresponds to constructor injection: In this mode, a Spring bean factory is able to host components that expect constructor-based dependency resolution.

Parameters:
beanName - the name of the bean
mbd - the merged bean definition for the bean
chosenCtors - chosen candidate constructors (or null if none)
explicitArgs - argument values passed in programmatically via the getBean method, or null if none (-> use constructor argument values from bean definition)
Returns:
a BeanWrapper for the new instance

resolveFactoryMethodIfPossible

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 - the bean definition to check

instantiateUsingFactoryMethod

public BeanWrapper instantiateUsingFactoryMethod(java.lang.String beanName,
                                                 RootBeanDefinition mbd,
                                                 java.lang.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 - the name of the bean
mbd - the merged bean definition for the bean
explicitArgs - argument values passed in programmatically via the getBean method, or null if none (-> use constructor argument values from bean definition)
Returns:
a BeanWrapper for the new instance

resolveConstructorArguments

private int resolveConstructorArguments(java.lang.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.


createArgumentArray

private ConstructorResolver.ArgumentsHolder createArgumentArray(java.lang.String beanName,
                                                                RootBeanDefinition mbd,
                                                                ConstructorArgumentValues resolvedValues,
                                                                BeanWrapper bw,
                                                                java.lang.Class[] paramTypes,
                                                                java.lang.String[] paramNames,
                                                                java.lang.Object methodOrCtor,
                                                                boolean autowiring)
                                                         throws UnsatisfiedDependencyException
Create an array of arguments to invoke a constructor or factory method, given the resolved constructor argument values.

Throws:
UnsatisfiedDependencyException

resolvePreparedArguments

private java.lang.Object[] resolvePreparedArguments(java.lang.String beanName,
                                                    RootBeanDefinition mbd,
                                                    BeanWrapper bw,
                                                    java.lang.reflect.Member methodOrCtor,
                                                    java.lang.Object[] argsToResolve)
Resolve the prepared arguments stored in the given bean definition.


resolveAutowiredArgument

protected java.lang.Object resolveAutowiredArgument(MethodParameter param,
                                                    java.lang.String beanName,
                                                    java.util.Set<java.lang.String> autowiredBeanNames,
                                                    TypeConverter typeConverter)
Template method for resolving the specified argument which is supposed to be autowired.