Class RootBeanDefinition

All Implemented Interfaces:
Serializable, Cloneable, BeanMetadataElement, BeanDefinition, AttributeAccessor

public class RootBeanDefinition extends AbstractBeanDefinition
A root bean definition represents the merged bean definition at runtime that backs a specific bean in a Spring BeanFactory. It might have been created from multiple original bean definitions that inherit from each other, e.g. GenericBeanDefinitions from XML declarations. A root bean definition is essentially the 'unified' bean definition view at runtime.

Root bean definitions may also be used for registering individual bean definitions in the configuration phase. This is particularly applicable for programmatic definitions derived from factory methods (e.g. @Bean methods) and instance suppliers (e.g. lambda expressions) which come with extra type metadata (see setTargetType(ResolvableType)/setResolvedFactoryMethod(Method)).

Note: The preferred choice for bean definitions derived from declarative sources (e.g. XML definitions) is the flexible GenericBeanDefinition variant. GenericBeanDefinition comes with the advantage that it allows for dynamically defining parent dependencies, not 'hard-coding' the role as a root bean definition, even supporting parent relationship changes in the bean post-processor phase.

Author:
Rod Johnson, Juergen Hoeller, Sam Brannen
See Also:
  • Constructor Details

    • RootBeanDefinition

      public RootBeanDefinition()
      Create a new RootBeanDefinition, to be configured through its bean properties and configuration methods.
      See Also:
    • RootBeanDefinition

      public RootBeanDefinition(@Nullable Class<?> beanClass)
      Create a new RootBeanDefinition for a singleton.
      Parameters:
      beanClass - the class of the bean to instantiate
      See Also:
    • RootBeanDefinition

      public RootBeanDefinition(@Nullable ResolvableType beanType)
      Create a new RootBeanDefinition for a singleton.
      Parameters:
      beanType - the type of bean to instantiate
      Since:
      6.0
      See Also:
    • RootBeanDefinition

      public RootBeanDefinition(@Nullable Class<T> beanClass, @Nullable Supplier<T> instanceSupplier)
      Create a new RootBeanDefinition for a singleton bean, constructing each instance through calling the given supplier (possibly a lambda or method reference).
      Parameters:
      beanClass - the class of the bean to instantiate
      instanceSupplier - the supplier to construct a bean instance, as an alternative to a declaratively specified factory method
      Since:
      5.0
      See Also:
    • RootBeanDefinition

      public RootBeanDefinition(@Nullable Class<T> beanClass, String scope, @Nullable Supplier<T> instanceSupplier)
      Create a new RootBeanDefinition for a scoped bean, constructing each instance through calling the given supplier (possibly a lambda or method reference).
      Parameters:
      beanClass - the class of the bean to instantiate
      scope - the name of the corresponding scope
      instanceSupplier - the supplier to construct a bean instance, as an alternative to a declaratively specified factory method
      Since:
      5.0
      See Also:
    • RootBeanDefinition

      public RootBeanDefinition(@Nullable Class<?> beanClass, int autowireMode, boolean dependencyCheck)
      Create a new RootBeanDefinition for a singleton, using the given autowire mode.
      Parameters:
      beanClass - the class of the bean to instantiate
      autowireMode - by name or type, using the constants in this interface
      dependencyCheck - whether to perform a dependency check for objects (not applicable to autowiring a constructor, thus ignored there)
    • RootBeanDefinition

      public RootBeanDefinition(@Nullable Class<?> beanClass, @Nullable ConstructorArgumentValues cargs, @Nullable MutablePropertyValues pvs)
      Create a new RootBeanDefinition for a singleton, providing constructor arguments and property values.
      Parameters:
      beanClass - the class of the bean to instantiate
      cargs - the constructor argument values to apply
      pvs - the property values to apply
    • RootBeanDefinition

      public RootBeanDefinition(String beanClassName)
      Create a new RootBeanDefinition for a singleton, providing constructor arguments and property values.

      Takes a bean class name to avoid eager loading of the bean class.

      Parameters:
      beanClassName - the name of the class to instantiate
    • RootBeanDefinition

      public RootBeanDefinition(String beanClassName, ConstructorArgumentValues cargs, MutablePropertyValues pvs)
      Create a new RootBeanDefinition for a singleton, providing constructor arguments and property values.

      Takes a bean class name to avoid eager loading of the bean class.

      Parameters:
      beanClassName - the name of the class to instantiate
      cargs - the constructor argument values to apply
      pvs - the property values to apply
    • RootBeanDefinition

      public RootBeanDefinition(RootBeanDefinition original)
      Create a new RootBeanDefinition as deep copy of the given bean definition.
      Parameters:
      original - the original bean definition to copy from
  • Method Details

    • getParentName

      public String getParentName()
      Description copied from interface: BeanDefinition
      Return the name of the parent definition of this bean definition, if any.
    • setParentName

      public void setParentName(@Nullable String parentName)
      Description copied from interface: BeanDefinition
      Set the name of the parent definition of this bean definition, if any.
    • setDecoratedDefinition

      public void setDecoratedDefinition(@Nullable BeanDefinitionHolder decoratedDefinition)
      Register a target definition that is being decorated by this bean definition.
    • getDecoratedDefinition

      @Nullable public BeanDefinitionHolder getDecoratedDefinition()
      Return the target definition that is being decorated by this bean definition, if any.
    • setQualifiedElement

      public void setQualifiedElement(@Nullable AnnotatedElement qualifiedElement)
      Specify the AnnotatedElement defining qualifiers, to be used instead of the target class or factory method.
      Since:
      4.3.3
      See Also:
    • getQualifiedElement

      @Nullable public AnnotatedElement getQualifiedElement()
      Return the AnnotatedElement defining qualifiers, if any. Otherwise, the factory method and target class will be checked.
      Since:
      4.3.3
    • setTargetType

      public void setTargetType(@Nullable ResolvableType targetType)
      Specify a generics-containing target type of this bean definition, if known in advance.
      Since:
      4.3.3
    • setTargetType

      public void setTargetType(@Nullable Class<?> targetType)
      Specify the target type of this bean definition, if known in advance.
      Since:
      3.2.2
    • getTargetType

      @Nullable public Class<?> getTargetType()
      Return the target type of this bean definition, if known (either specified in advance or resolved on first instantiation).
      Since:
      3.2.2
    • getResolvableType

      public ResolvableType getResolvableType()
      Return a ResolvableType for this bean definition, either from runtime-cached type information or from configuration-time setTargetType(ResolvableType) or AbstractBeanDefinition.setBeanClass(Class), also considering resolved factory method definitions.
      Specified by:
      getResolvableType in interface BeanDefinition
      Overrides:
      getResolvableType in class AbstractBeanDefinition
      Returns:
      the resolvable type (potentially ResolvableType.NONE)
      Since:
      5.1
      See Also:
    • getPreferredConstructors

      @Nullable public Constructor<?>[] getPreferredConstructors()
      Determine preferred constructors to use for default construction, if any. Constructor arguments will be autowired if necessary.
      Returns:
      one or more preferred constructors, or null if none (in which case the regular no-arg default constructor will be called)
      Since:
      5.1
    • setUniqueFactoryMethodName

      public void setUniqueFactoryMethodName(String name)
      Specify a factory method name that refers to a non-overloaded method.
    • setNonUniqueFactoryMethodName

      public void setNonUniqueFactoryMethodName(String name)
      Specify a factory method name that refers to an overloaded method.
      Since:
      5.2
    • isFactoryMethod

      public boolean isFactoryMethod(Method candidate)
      Check whether the given candidate qualifies as a factory method.
    • setResolvedFactoryMethod

      public void setResolvedFactoryMethod(@Nullable Method method)
      Set a resolved Java Method for the factory method on this bean definition.
      Parameters:
      method - the resolved factory method, or null to reset it
      Since:
      5.2
    • getResolvedFactoryMethod

      @Nullable public Method getResolvedFactoryMethod()
      Return the resolved factory method as a Java Method object, if available.
      Returns:
      the factory method, or null if not found or not resolved yet
    • setInstanceSupplier

      public void setInstanceSupplier(@Nullable Supplier<?> supplier)
      Description copied from class: AbstractBeanDefinition
      Specify a callback for creating an instance of the bean, as an alternative to a declaratively specified factory method.

      If such a callback is set, it will override any other constructor or factory method metadata. However, bean property population and potential annotation-driven injection will still apply as usual.

      Overrides:
      setInstanceSupplier in class AbstractBeanDefinition
      See Also:
    • markAsPostProcessed

      public void markAsPostProcessed()
      Mark this bean definition as post-processed, i.e. processed by MergedBeanDefinitionPostProcessor.
      Since:
      6.0
    • registerExternallyManagedConfigMember

      public void registerExternallyManagedConfigMember(Member configMember)
      Register an externally managed configuration method or field.
    • isExternallyManagedConfigMember

      public boolean isExternallyManagedConfigMember(Member configMember)
      Determine if the given method or field is an externally managed configuration member.
    • getExternallyManagedConfigMembers

      public Set<Member> getExternallyManagedConfigMembers()
      Get all externally managed configuration methods and fields (as an immutable Set).
      Since:
      5.3.11
    • registerExternallyManagedInitMethod

      public void registerExternallyManagedInitMethod(String initMethod)
      Register an externally managed configuration initialization method — for example, a method annotated with JSR-250's PostConstruct annotation.

      The supplied initMethod may be the simple method name for non-private methods or the qualified method name for private methods. A qualified name is necessary for private methods in order to disambiguate between multiple private methods with the same name within a class hierarchy.

    • isExternallyManagedInitMethod

      public boolean isExternallyManagedInitMethod(String initMethod)
      Determine if the given method name indicates an externally managed initialization method.

      See registerExternallyManagedInitMethod(java.lang.String) for details regarding the format for the supplied initMethod.

    • getExternallyManagedInitMethods

      public Set<String> getExternallyManagedInitMethods()
      Return all externally managed initialization methods (as an immutable Set).

      See registerExternallyManagedInitMethod(java.lang.String) for details regarding the format for the initialization methods in the returned set.

      Since:
      5.3.11
    • resolveDestroyMethodIfNecessary

      public void resolveDestroyMethodIfNecessary()
      Resolve the inferred destroy method if necessary.
      Since:
      6.0
    • registerExternallyManagedDestroyMethod

      public void registerExternallyManagedDestroyMethod(String destroyMethod)
      Register an externally managed configuration destruction method — for example, a method annotated with JSR-250's PreDestroy annotation.

      The supplied destroyMethod may be the simple method name for non-private methods or the qualified method name for private methods. A qualified name is necessary for private methods in order to disambiguate between multiple private methods with the same name within a class hierarchy.

    • isExternallyManagedDestroyMethod

      public boolean isExternallyManagedDestroyMethod(String destroyMethod)
      Determine if the given method name indicates an externally managed destruction method.

      See registerExternallyManagedDestroyMethod(java.lang.String) for details regarding the format for the supplied destroyMethod.

    • getExternallyManagedDestroyMethods

      public Set<String> getExternallyManagedDestroyMethods()
      Get all externally managed destruction methods (as an immutable Set).

      See registerExternallyManagedDestroyMethod(java.lang.String) for details regarding the format for the destruction methods in the returned set.

      Since:
      5.3.11
    • cloneBeanDefinition

      public RootBeanDefinition cloneBeanDefinition()
      Description copied from class: AbstractBeanDefinition
      Clone this bean definition. To be implemented by concrete subclasses.
      Specified by:
      cloneBeanDefinition in class AbstractBeanDefinition
      Returns:
      the cloned bean definition object
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class AbstractBeanDefinition
    • toString

      public String toString()
      Overrides:
      toString in class AbstractBeanDefinition