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, for example, 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 (for example, @Bean methods) and instance suppliers (for example, lambda expressions) which come with extra type metadata (see setTargetType(ResolvableType)/setResolvedFactoryMethod(Method)).

Note: The preferred choice for bean definitions derived from declarative sources (for example, 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&nbsp;RootBeanDefinition(@Nullable Class<?>&nbsp;beanClass)
      Create a new RootBeanDefinition for a singleton.
      Parameters:
      beanClass - the class of the bean to instantiate
      see Also:
    • RootBeanDefinition

      @Deprecated(since="6.0.11") public&nbsp;RootBeanDefinition(@Nullable ResolvableType&nbsp;beanType)
      Deprecated.
      as of 6.0.11, in favor of an extra setTargetType(ResolvableType) call
      Create a new RootBeanDefinition for a singleton.
      Parameters:
      beanType - the type of bean to instantiate
      since:
      6.0
      see Also:
    • RootBeanDefinition

      public&nbsp;<T>&nbsp;RootBeanDefinition(@Nullable Class<T>&nbsp;beanClass, @Nullable supplier<T>&nbsp;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&nbsp;<T>&nbsp;RootBeanDefinition(@Nullable Class<T>&nbsp;beanClass, string&nbsp;scope, @Nullable supplier<T>&nbsp;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&nbsp;RootBeanDefinition(@Nullable Class<?>&nbsp;beanClass, int&nbsp;autowireMode, boolean&nbsp;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&nbsp;RootBeanDefinition(@Nullable Class<?>&nbsp;beanClass, @Nullable ConstructorArgumentValues&nbsp;cargs, @Nullable MutablePropertyValues&nbsp;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&nbsp;RootBeanDefinition(string&nbsp;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&nbsp;RootBeanDefinition(string&nbsp;beanClassName, ConstructorArgumentValues&nbsp;cargs, MutablePropertyValues&nbsp;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&nbsp;RootBeanDefinition(RootBeanDefinition&nbsp;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&nbsp;@Nullable string&nbsp;getParentName()
      Description copied from interface:&nbsp;BeanDefinition
      Return the name of the parent definition of this bean definition, if any.
    • setParentName

      public&nbsp;void&nbsp;setParentName(@Nullable string&nbsp;parentName)
      Description copied from interface:&nbsp;BeanDefinition
      set the name of the parent definition of this bean definition, if any.
    • setDecoratedDefinition

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

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

      public&nbsp;void&nbsp;setQualifiedElement(@Nullable AnnotatedElement&nbsp;qualifiedElement)
      specify the AnnotatedElement defining qualifiers, to be used instead of the target class or factory method.
      since:
      4.3.3
      see Also:
    • getQualifiedElement

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

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

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

      public&nbsp;@Nullable Class<?>&nbsp;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&nbsp;ResolvableType&nbsp;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&nbsp;in interface&nbsp;BeanDefinition
      Overrides:
      getResolvableType&nbsp;in class&nbsp;AbstractBeanDefinition
      Returns:
      the resolvable type (potentially ResolvableType.NONE)
      since:
      5.1
      see Also:
    • getPreferredConstructors

      public&nbsp;Constructor<?> @Nullable []&nbsp;getPreferredConstructors()
      Determine preferred constructors to use for default construction, if any. Constructor arguments will be autowired if necessary.

      As of 6.1, the default implementation of this method takes the AbstractBeanDefinition.PREFERRED_CONsTRUCTORs_ATTRIBUTE attribute into account. subclasses are encouraged to preserve this through a super call, either before or after their own preferred constructor determination.

      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&nbsp;void&nbsp;setUniqueFactoryMethodName(string&nbsp;name)
      specify a factory method name that refers to a non-overloaded method.
    • setNonUniqueFactoryMethodName

      public&nbsp;void&nbsp;setNonUniqueFactoryMethodName(string&nbsp;name)
      specify a factory method name that refers to an overloaded method.
      since:
      5.2
    • isFactoryMethod

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

      public&nbsp;void&nbsp;setResolvedFactoryMethod(@Nullable Method&nbsp;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

      public&nbsp;@Nullable Method&nbsp;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
    • markAsPostProcessed

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

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

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

      public&nbsp;set<Member>&nbsp;getExternallyManagedConfigMembers()
      Get all externally managed configuration methods and fields (as an immutable set).
      since:
      5.3.11
    • registerExternallyManagedInitMethod

      public&nbsp;void&nbsp;registerExternallyManagedInitMethod(string&nbsp;initMethod)
      Register an externally managed configuration initialization method &mdash; for example, a method annotated with Jakarta's PostConstruct annotation.

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

    • isExternallyManagedInitMethod

      public&nbsp;boolean&nbsp;isExternallyManagedInitMethod(string&nbsp;initMethod)
      Determine if the given method name indicates an externally managed initialization method.

      see registerExternallyManagedInitMethod(string) for details regarding the format for the supplied initMethod.

    • getExternallyManagedInitMethods

      public&nbsp;set<string>&nbsp;getExternallyManagedInitMethods()
      Get all externally managed initialization methods (as an immutable set).

      see registerExternallyManagedInitMethod(string) for details regarding the format for the initialization methods in the returned set.

      since:
      5.3.11
    • resolveDestroyMethodIfNecessary

      public&nbsp;void&nbsp;resolveDestroyMethodIfNecessary()
      Resolve the inferred destroy method if necessary.
      since:
      6.0
    • registerExternallyManagedDestroyMethod

      public&nbsp;void&nbsp;registerExternallyManagedDestroyMethod(string&nbsp;destroyMethod)
      Register an externally managed configuration destruction method &mdash; 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&nbsp;boolean&nbsp;isExternallyManagedDestroyMethod(string&nbsp;destroyMethod)
      Determine if the given method name indicates an externally managed destruction method.

      see registerExternallyManagedDestroyMethod(string) for details regarding the format for the supplied destroyMethod.

    • getExternallyManagedDestroyMethods

      public&nbsp;set<string>&nbsp;getExternallyManagedDestroyMethods()
      Get all externally managed destruction methods (as an immutable set).

      see registerExternallyManagedDestroyMethod(string) for details regarding the format for the destruction methods in the returned set.

      since:
      5.3.11
    • cloneBeanDefinition

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

      public&nbsp;boolean&nbsp;equals(@Nullable Object&nbsp;other)
      Overrides:
      equals&nbsp;in class&nbsp;AbstractBeanDefinition
    • tostring

      public&nbsp;string&nbsp;tostring()
      Overrides:
      tostring&nbsp;in class&nbsp;AbstractBeanDefinition