Class AbstractBeanDefinition

All Implemented Interfaces:
Serializable, Cloneable, BeanMetadataElement, BeanDefinition, AttributeAccessor
Direct Known Subclasses:
ChildBeanDefinition, GenericBeanDefinition, RootBeanDefinition

public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor implements BeanDefinition, Cloneable
Base class for concrete, full-fledged BeanDefinition classes, factoring out common properties of GenericBeanDefinition, RootBeanDefinition, and ChildBeanDefinition.

The autowire constants match the ones defined in the AutowireCapableBeanFactory interface.

Author:
Rod Johnson, Juergen Hoeller, Rob Harrop, Mark Fisher
See Also:
  • Field Details

    • SCOPE_DEFAULT

      public static final String SCOPE_DEFAULT
      Constant for the default scope name: "", equivalent to singleton status unless overridden from a parent bean definition (if applicable).
      See Also:
    • AUTOWIRE_NO

      public static final int AUTOWIRE_NO
      Constant that indicates no external autowiring at all.
      See Also:
    • AUTOWIRE_BY_NAME

      public static final int AUTOWIRE_BY_NAME
      Constant that indicates autowiring bean properties by name.
      See Also:
    • AUTOWIRE_BY_TYPE

      public static final int AUTOWIRE_BY_TYPE
      Constant that indicates autowiring bean properties by type.
      See Also:
    • AUTOWIRE_CONSTRUCTOR

      public static final int AUTOWIRE_CONSTRUCTOR
      Constant that indicates autowiring a constructor.
      See Also:
    • AUTOWIRE_AUTODETECT

      @Deprecated public static final int AUTOWIRE_AUTODETECT
      Deprecated.
      as of Spring 3.0: If you are using mixed autowiring strategies, use annotation-based autowiring for clearer demarcation of autowiring needs.
      Constant that indicates determining an appropriate autowire strategy through introspection of the bean class.
      See Also:
    • DEPENDENCY_CHECK_NONE

      public static final int DEPENDENCY_CHECK_NONE
      Constant that indicates no dependency check at all.
      See Also:
    • DEPENDENCY_CHECK_OBJECTS

      public static final int DEPENDENCY_CHECK_OBJECTS
      Constant that indicates dependency checking for object references.
      See Also:
    • DEPENDENCY_CHECK_SIMPLE

      public static final int DEPENDENCY_CHECK_SIMPLE
      Constant that indicates dependency checking for "simple" properties.
      See Also:
    • DEPENDENCY_CHECK_ALL

      public static final int DEPENDENCY_CHECK_ALL
      Constant that indicates dependency checking for all properties (object references as well as "simple" properties).
      See Also:
    • INFER_METHOD

      public static final String INFER_METHOD
      Constant that indicates the container should attempt to infer the destroy method name for a bean as opposed to explicit specification of a method name. The value "(inferred)" is specifically designed to include characters otherwise illegal in a method name, ensuring no possibility of collisions with legitimately named methods having the same name.

      Currently, the method names detected during destroy method inference are "close" and "shutdown", if present on the specific bean class.

      See Also:
  • Constructor Details

    • AbstractBeanDefinition

      protected AbstractBeanDefinition()
      Create a new AbstractBeanDefinition with default settings.
    • AbstractBeanDefinition

      protected AbstractBeanDefinition(@Nullable ConstructorArgumentValues cargs, @Nullable MutablePropertyValues pvs)
      Create a new AbstractBeanDefinition with the given constructor argument values and property values.
    • AbstractBeanDefinition

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

    • overrideFrom

      public void overrideFrom(BeanDefinition other)
      Override settings in this bean definition (presumably a copied parent from a parent-child inheritance relationship) from the given bean definition (presumably the child).
      • Will override beanClass if specified in the given bean definition.
      • Will always take abstract, scope, lazyInit, autowireMode, dependencyCheck, and dependsOn from the given bean definition.
      • Will add constructorArgumentValues, propertyValues, methodOverrides from the given bean definition to existing ones.
      • Will override factoryBeanName, factoryMethodName, initMethodName, and destroyMethodName if specified in the given bean definition.
    • applyDefaults

      public void applyDefaults(BeanDefinitionDefaults defaults)
      Apply the provided default values to this bean.
      Parameters:
      defaults - the default settings to apply
      Since:
      2.5
    • setBeanClassName

      public void setBeanClassName(@Nullable String beanClassName)
      Specify the bean class name of this bean definition.
      Specified by:
      setBeanClassName in interface BeanDefinition
      See Also:
    • getBeanClassName

      @Nullable public String getBeanClassName()
      Return the current bean class name of this bean definition.
      Specified by:
      getBeanClassName in interface BeanDefinition
      See Also:
    • setBeanClass

      public void setBeanClass(@Nullable Class<?> beanClass)
      Specify the class for this bean.
      See Also:
    • getBeanClass

      public Class<?> getBeanClass() throws IllegalStateException
      Return the specified class of the bean definition (assuming it is resolved already).

      NOTE: This is an initial class reference as declared in the bean metadata definition, potentially combined with a declared factory method or a FactoryBean which may lead to a different runtime type of the bean, or not being set at all in case of an instance-level factory method (which is resolved via getFactoryBeanName() instead). Do not use this for runtime type introspection of arbitrary bean definitions. The recommended way to find out about the actual runtime type of a particular bean is a BeanFactory.getType(java.lang.String) call for the specified bean name; this takes all of the above cases into account and returns the type of object that a BeanFactory.getBean(java.lang.String) call is going to return for the same bean name.

      Returns:
      the resolved bean class (never null)
      Throws:
      IllegalStateException - if the bean definition does not define a bean class, or a specified bean class name has not been resolved into an actual Class yet
      See Also:
    • hasBeanClass

      public boolean hasBeanClass()
      Return whether this definition specifies a bean class.
      See Also:
    • resolveBeanClass

      @Nullable public Class<?> resolveBeanClass(@Nullable ClassLoader classLoader) throws ClassNotFoundException
      Determine the class of the wrapped bean, resolving it from a specified class name if necessary. Will also reload a specified Class from its name when called with the bean class already resolved.
      Parameters:
      classLoader - the ClassLoader to use for resolving a (potential) class name
      Returns:
      the resolved bean class
      Throws:
      ClassNotFoundException - if the class name could be resolved
    • getResolvableType

      public ResolvableType getResolvableType()
      Return a resolvable type for this bean definition.

      This implementation delegates to getBeanClass().

      Specified by:
      getResolvableType in interface BeanDefinition
      Returns:
      the resolvable type (potentially ResolvableType.NONE)
      Since:
      5.2
      See Also:
    • setScope

      public void setScope(@Nullable String scope)
      Set the name of the target scope for the bean.

      The default is singleton status, although this is only applied once a bean definition becomes active in the containing factory. A bean definition may eventually inherit its scope from a parent bean definition. For this reason, the default scope name is an empty string (i.e., ""), with singleton status being assumed until a resolved scope is set.

      Specified by:
      setScope in interface BeanDefinition
      See Also:
    • getScope

      @Nullable public String getScope()
      Return the name of the target scope for the bean.
      Specified by:
      getScope in interface BeanDefinition
    • isSingleton

      public boolean isSingleton()
      Return whether this a Singleton, with a single shared instance returned from all calls.
      Specified by:
      isSingleton in interface BeanDefinition
      See Also:
    • isPrototype

      public boolean isPrototype()
      Return whether this a Prototype, with an independent instance returned for each call.
      Specified by:
      isPrototype in interface BeanDefinition
      See Also:
    • setAbstract

      public void setAbstract(boolean abstractFlag)
      Set if this bean is "abstract", i.e. not meant to be instantiated itself but rather just serving as parent for concrete child bean definitions.

      Default is "false". Specify true to tell the bean factory to not try to instantiate that particular bean in any case.

    • isAbstract

      public boolean isAbstract()
      Return whether this bean is "abstract", i.e. not meant to be instantiated itself but rather just serving as parent for concrete child bean definitions.
      Specified by:
      isAbstract in interface BeanDefinition
    • setLazyInit

      public void setLazyInit(boolean lazyInit)
      Set whether this bean should be lazily initialized.

      If false, the bean will get instantiated on startup by bean factories that perform eager initialization of singletons.

      Specified by:
      setLazyInit in interface BeanDefinition
    • isLazyInit

      public boolean isLazyInit()
      Return whether this bean should be lazily initialized, i.e. not eagerly instantiated on startup. Only applicable to a singleton bean.
      Specified by:
      isLazyInit in interface BeanDefinition
      Returns:
      whether to apply lazy-init semantics (false by default)
    • getLazyInit

      @Nullable public Boolean getLazyInit()
      Return whether this bean should be lazily initialized, i.e. not eagerly instantiated on startup. Only applicable to a singleton bean.
      Returns:
      the lazy-init flag if explicitly set, or null otherwise
      Since:
      5.2
    • setAutowireMode

      public void setAutowireMode(int autowireMode)
      Set the autowire mode. This determines whether any automagical detection and setting of bean references will happen. Default is AUTOWIRE_NO which means there won't be convention-based autowiring by name or type (however, there may still be explicit annotation-driven autowiring).
      Parameters:
      autowireMode - the autowire mode to set. Must be one of the constants defined in this class.
      See Also:
    • getAutowireMode

      public int getAutowireMode()
      Return the autowire mode as specified in the bean definition.
    • getResolvedAutowireMode

      public int getResolvedAutowireMode()
      Return the resolved autowire code, (resolving AUTOWIRE_AUTODETECT to AUTOWIRE_CONSTRUCTOR or AUTOWIRE_BY_TYPE).
      See Also:
    • setDependencyCheck

      public void setDependencyCheck(int dependencyCheck)
      Set the dependency check code.
      Parameters:
      dependencyCheck - the code to set. Must be one of the four constants defined in this class.
      See Also:
    • getDependencyCheck

      public int getDependencyCheck()
      Return the dependency check code.
    • setDependsOn

      public void setDependsOn(@Nullable String... dependsOn)
      Set the names of the beans that this bean depends on being initialized. The bean factory will guarantee that these beans get initialized first.

      Note that dependencies are normally expressed through bean properties or constructor arguments. This property should just be necessary for other kinds of dependencies like statics (*ugh*) or database preparation on startup.

      Specified by:
      setDependsOn in interface BeanDefinition
    • getDependsOn

      @Nullable public String[] getDependsOn()
      Return the bean names that this bean depends on.
      Specified by:
      getDependsOn in interface BeanDefinition
    • setAutowireCandidate

      public void setAutowireCandidate(boolean autowireCandidate)
      Set whether this bean is a candidate for getting autowired into some other bean.

      Note that this flag is designed to only affect type-based autowiring. It does not affect explicit references by name, which will get resolved even if the specified bean is not marked as an autowire candidate. As a consequence, autowiring by name will nevertheless inject a bean if the name matches.

      Specified by:
      setAutowireCandidate in interface BeanDefinition
      See Also:
    • isAutowireCandidate

      public boolean isAutowireCandidate()
      Return whether this bean is a candidate for getting autowired into some other bean.
      Specified by:
      isAutowireCandidate in interface BeanDefinition
    • setPrimary

      public void setPrimary(boolean primary)
      Set whether this bean is a primary autowire candidate.

      If this value is true for exactly one bean among multiple matching candidates, it will serve as a tie-breaker.

      Specified by:
      setPrimary in interface BeanDefinition
    • isPrimary

      public boolean isPrimary()
      Return whether this bean is a primary autowire candidate.
      Specified by:
      isPrimary in interface BeanDefinition
    • addQualifier

      public void addQualifier(AutowireCandidateQualifier qualifier)
      Register a qualifier to be used for autowire candidate resolution, keyed by the qualifier's type name.
      See Also:
    • hasQualifier

      public boolean hasQualifier(String typeName)
      Return whether this bean has the specified qualifier.
    • getQualifier

      @Nullable public AutowireCandidateQualifier getQualifier(String typeName)
      Return the qualifier mapped to the provided type name.
    • getQualifiers

      public Set<AutowireCandidateQualifier> getQualifiers()
      Return all registered qualifiers.
      Returns:
      the Set of AutowireCandidateQualifier objects.
    • copyQualifiersFrom

      public void copyQualifiersFrom(AbstractBeanDefinition source)
      Copy the qualifiers from the supplied AbstractBeanDefinition to this bean definition.
      Parameters:
      source - the AbstractBeanDefinition to copy from
    • setInstanceSupplier

      public void setInstanceSupplier(@Nullable Supplier<?> instanceSupplier)
      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.

      Since:
      5.0
      See Also:
    • getInstanceSupplier

      @Nullable public Supplier<?> getInstanceSupplier()
      Return a callback for creating an instance of the bean, if any.
      Since:
      5.0
    • setNonPublicAccessAllowed

      public void setNonPublicAccessAllowed(boolean nonPublicAccessAllowed)
      Specify whether to allow access to non-public constructors and methods, for the case of externalized metadata pointing to those. The default is true; switch this to false for public access only.

      This applies to constructor resolution, factory method resolution, and also init/destroy methods. Bean property accessors have to be public in any case and are not affected by this setting.

      Note that annotation-driven configuration will still access non-public members as far as they have been annotated. This setting applies to externalized metadata in this bean definition only.

    • isNonPublicAccessAllowed

      public boolean isNonPublicAccessAllowed()
      Return whether to allow access to non-public constructors and methods.
    • setLenientConstructorResolution

      public void setLenientConstructorResolution(boolean lenientConstructorResolution)
      Specify whether to resolve constructors in lenient mode (true, which is the default) or to switch to strict resolution (throwing an exception in case of ambiguous constructors that all match when converting the arguments, whereas lenient mode would use the one with the 'closest' type matches).
    • isLenientConstructorResolution

      public boolean isLenientConstructorResolution()
      Return whether to resolve constructors in lenient mode or in strict mode.
    • setFactoryBeanName

      public void setFactoryBeanName(@Nullable String factoryBeanName)
      Specify the factory bean to use, if any. This the name of the bean to call the specified factory method on.
      Specified by:
      setFactoryBeanName in interface BeanDefinition
      See Also:
    • getFactoryBeanName

      @Nullable public String getFactoryBeanName()
      Return the factory bean name, if any.
      Specified by:
      getFactoryBeanName in interface BeanDefinition
    • setFactoryMethodName

      public void setFactoryMethodName(@Nullable String factoryMethodName)
      Specify a factory method, if any. This method will be invoked with constructor arguments, or with no arguments if none are specified. The method will be invoked on the specified factory bean, if any, or otherwise as a static method on the local bean class.
      Specified by:
      setFactoryMethodName in interface BeanDefinition
      See Also:
    • getFactoryMethodName

      @Nullable public String getFactoryMethodName()
      Return a factory method, if any.
      Specified by:
      getFactoryMethodName in interface BeanDefinition
    • setConstructorArgumentValues

      public void setConstructorArgumentValues(ConstructorArgumentValues constructorArgumentValues)
      Specify constructor argument values for this bean.
    • getConstructorArgumentValues

      public ConstructorArgumentValues getConstructorArgumentValues()
      Return constructor argument values for this bean (never null).
      Specified by:
      getConstructorArgumentValues in interface BeanDefinition
      Returns:
      the ConstructorArgumentValues object (never null)
    • hasConstructorArgumentValues

      public boolean hasConstructorArgumentValues()
      Return if there are constructor argument values defined for this bean.
      Specified by:
      hasConstructorArgumentValues in interface BeanDefinition
    • setPropertyValues

      public void setPropertyValues(MutablePropertyValues propertyValues)
      Specify property values for this bean, if any.
    • getPropertyValues

      public MutablePropertyValues getPropertyValues()
      Return property values for this bean (never null).
      Specified by:
      getPropertyValues in interface BeanDefinition
      Returns:
      the MutablePropertyValues object (never null)
    • hasPropertyValues

      public boolean hasPropertyValues()
      Return if there are property values defined for this bean.
      Specified by:
      hasPropertyValues in interface BeanDefinition
      Since:
      5.0.2
    • setMethodOverrides

      public void setMethodOverrides(MethodOverrides methodOverrides)
      Specify method overrides for the bean, if any.
    • getMethodOverrides

      public MethodOverrides getMethodOverrides()
      Return information about methods to be overridden by the IoC container. This will be empty if there are no method overrides.

      Never returns null.

    • hasMethodOverrides

      public boolean hasMethodOverrides()
      Return if there are method overrides defined for this bean.
      Since:
      5.0.2
    • setInitMethodNames

      public void setInitMethodNames(@Nullable String... initMethodNames)
      Specify the names of multiple initializer methods.

      The default is null in which case there are no initializer methods.

      Since:
      6.0
      See Also:
    • getInitMethodNames

      @Nullable public String[] getInitMethodNames()
      Return the names of the initializer methods.
      Since:
      6.0
    • setInitMethodName

      public void setInitMethodName(@Nullable String initMethodName)
      Set the name of the initializer method.

      The default is null in which case there is no initializer method.

      Specified by:
      setInitMethodName in interface BeanDefinition
      See Also:
    • getInitMethodName

      @Nullable public String getInitMethodName()
      Return the name of the initializer method (the first one in case of multiple methods).
      Specified by:
      getInitMethodName in interface BeanDefinition
    • setEnforceInitMethod

      public void setEnforceInitMethod(boolean enforceInitMethod)
      Specify whether the configured initializer method is the default.

      The default value is true for a locally specified init method but switched to false for a shared setting in a defaults section (e.g. bean init-method versus beans default-init-method level in XML) which might not apply to all contained bean definitions.

      See Also:
    • isEnforceInitMethod

      public boolean isEnforceInitMethod()
      Indicate whether the configured initializer method is the default.
      See Also:
    • setDestroyMethodNames

      public void setDestroyMethodNames(@Nullable String... destroyMethodNames)
      Specify the names of multiple destroy methods.

      The default is null in which case there are no destroy methods.

      Since:
      6.0
      See Also:
    • getDestroyMethodNames

      @Nullable public String[] getDestroyMethodNames()
      Return the names of the destroy methods.
      Since:
      6.0
    • setDestroyMethodName

      public void setDestroyMethodName(@Nullable String destroyMethodName)
      Set the name of the destroy method.

      The default is null in which case there is no destroy method.

      Specified by:
      setDestroyMethodName in interface BeanDefinition
      See Also:
    • getDestroyMethodName

      @Nullable public String getDestroyMethodName()
      Return the name of the destroy method (the first one in case of multiple methods).
      Specified by:
      getDestroyMethodName in interface BeanDefinition
    • setEnforceDestroyMethod

      public void setEnforceDestroyMethod(boolean enforceDestroyMethod)
      Specify whether the configured destroy method is the default.

      The default value is true for a locally specified destroy method but switched to false for a shared setting in a defaults section (e.g. bean destroy-method versus beans default-destroy-method level in XML) which might not apply to all contained bean definitions.

      See Also:
    • isEnforceDestroyMethod

      public boolean isEnforceDestroyMethod()
      Indicate whether the configured destroy method is the default.
      See Also:
    • setSynthetic

      public void setSynthetic(boolean synthetic)
      Set whether this bean definition is 'synthetic', that is, not defined by the application itself (for example, an infrastructure bean such as a helper for auto-proxying, created through <aop:config>).
    • isSynthetic

      public boolean isSynthetic()
      Return whether this bean definition is 'synthetic', that is, not defined by the application itself.
    • setRole

      public void setRole(int role)
      Set the role hint for this BeanDefinition.
      Specified by:
      setRole in interface BeanDefinition
      See Also:
    • getRole

      public int getRole()
      Return the role hint for this BeanDefinition.
      Specified by:
      getRole in interface BeanDefinition
      See Also:
    • setDescription

      public void setDescription(@Nullable String description)
      Set a human-readable description of this bean definition.
      Specified by:
      setDescription in interface BeanDefinition
    • getDescription

      @Nullable public String getDescription()
      Return a human-readable description of this bean definition.
      Specified by:
      getDescription in interface BeanDefinition
    • setResource

      public void setResource(@Nullable Resource resource)
      Set the resource that this bean definition came from (for the purpose of showing context in case of errors).
    • getResource

      @Nullable public Resource getResource()
      Return the resource that this bean definition came from.
    • setResourceDescription

      public void setResourceDescription(@Nullable String resourceDescription)
      Set a description of the resource that this bean definition came from (for the purpose of showing context in case of errors).
    • getResourceDescription

      @Nullable public String getResourceDescription()
      Return a description of the resource that this bean definition came from (for the purpose of showing context in case of errors).
      Specified by:
      getResourceDescription in interface BeanDefinition
    • setOriginatingBeanDefinition

      public void setOriginatingBeanDefinition(BeanDefinition originatingBd)
      Set the originating (e.g. decorated) BeanDefinition, if any.
    • getOriginatingBeanDefinition

      @Nullable public BeanDefinition getOriginatingBeanDefinition()
      Return the originating BeanDefinition, or null if none. Allows for retrieving the decorated bean definition, if any.

      Note that this method returns the immediate originator. Iterate through the originator chain to find the original BeanDefinition as defined by the user.

      Specified by:
      getOriginatingBeanDefinition in interface BeanDefinition
    • validate

      public void validate() throws BeanDefinitionValidationException
      Validate this bean definition.
      Throws:
      BeanDefinitionValidationException - in case of validation failure
    • prepareMethodOverrides

      public void prepareMethodOverrides() throws BeanDefinitionValidationException
      Validate and prepare the method overrides defined for this bean. Checks for existence of a method with the specified name.
      Throws:
      BeanDefinitionValidationException - in case of validation failure
    • prepareMethodOverride

      protected void prepareMethodOverride(MethodOverride mo) throws BeanDefinitionValidationException
      Validate and prepare the given method override. Checks for existence of a method with the specified name, marking it as not overloaded if none found.
      Parameters:
      mo - the MethodOverride object to validate
      Throws:
      BeanDefinitionValidationException - in case of validation failure
    • clone

      public Object clone()
      Public declaration of Object's clone() method. Delegates to cloneBeanDefinition().
      Overrides:
      clone in class Object
      See Also:
    • cloneBeanDefinition

      public abstract AbstractBeanDefinition cloneBeanDefinition()
      Clone this bean definition. To be implemented by concrete subclasses.
      Returns:
      the cloned bean definition object
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class AttributeAccessorSupport
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class AttributeAccessorSupport
    • toString

      public String toString()
      Overrides:
      toString in class Object