classBeanDefinition
The inheritance system of
Through the in-depth analysis of the BeanFactory in the previous chapter, we found that the BeanFactory abstracts the instantiation process of the Bean object into the BeanDefinition class. Similarly, by analyzing the class inheritance structure of BeanDefinition (see figure below).
Interface inheritance
From the figure, we can see that BeanDefination mainly inherits from AttributeAccessor and BeanMetadataElement. The former encapsulates operations related to object attributes (such as add, delete, modify). The latter is the original definition source that describes BeanDefination (the configuration source {@code Object} for this metadata element), For example, if it is a Spring Bean based Annotation configuration, The source might correspond to StandardMethodMetadata (defined by the @Bean annotation) or File (defined by the @Component annotation).
Class implements
From the diagram, we found that AttributeAccessorSupport provide general properties of encapsulation, operation BeanMetadataAttributeAccessor interface providing Source related, speaking, reading and writing; Based on AttributeAccessorSupport, the attribute format is unified into the class BeanMetadataAttribute. Finally, by inheriting class BeanMetadataAttribute to implement interface BeanDefinition, we get abstract class AbstractBeanDefinition, the basic BeanDefination.
AbstractBeanDefinition has three implementation classes, depending on the usage scenario: RootBeanDefinition (used to instantiate bean objects, Other types of BeanDefinitions need to be converted to RootBeanDefinitions to be instantiated by BeanFactory), childBeanDefinitions (beanDefinitions are supported through the parent property Data inheritance), GenericBeanDefinition (generic BeanDefination). By @ Bean, @ Component annotation registered BeanDefination, are generally not AnnotatedGenericBeanDefinition or ScannedGenericBeanDefinition, The difference between the two is that the former is based on the Class object instance to get related attributes, while the latter is based on ASM bytecode parsing, without Class loading.
interfaceBeanDefinition
Related field definitions
Field name (bold fields support both write and read properties, other fields support read fields) | meaning |
---|---|
parentName | The parent of BeanDefination, used for inheritance of related attributes, can be null |
beanClassName | The Class name of the bean, which can be null |
scope | Scope of bean. IoC container provides singleton and Prototype scope to support business expansion. Default is Singleton |
lazyInit | Lazy loading or not: The default is false, meaning that the Bean is initialized in the IoC container, not when the application acquires the Bean |
dependsOn | Describes the Spring beans that the current bean depends on before initialization, in which order the BeanFactory ensures |
autowireCandidate | Is a set of candidates to inject the current bean as a type of another bean when the user does not specify a specific reference. The default is true |
primary | Combine with the previous fieldautowireCandidate , whether the bean is the preferred object when it appears in the candidate set. The default is false |
factoryBeanName | The Bean Name of the factory class used to instantiate the Bean, which can be null |
factoryMethodName | The method name of the factory class used to instantiate the Bean, which can be null |
constructorArgumentValues | Functions that describe Bean initialization |
propertyValues | Describes the value of the bean-related properties |
initMethodName | Describes a custom initialization method inside a Bean |
destroyMethodName | Describes a custom destruction method inside a Bean |
role | The role of Bean, generally divided intoAPPLICATION ,SUPPORT ,INFRASTRUCTURE |
description | Bean description |
ResolvableType | The Abstract representation of the Type object in Spring provides user-friendly extraction of generic types |
Abstract | Whether the type is Abstract |
ResourceDescription | The description of the BeanDefination definition source is used to display scenarios such as logs |
classAbstractBeanDefinition
Extended field of
AbstractBeanDefinition provides the following fields in addition to the interface BeanDefinition
- AutowireMode: Describes the pattern for auto-assembly beans, divided into
AUTOWIRE_BY_NAME
(Indicating auto-assembly Bean properties by name, applies to all Bean property Settings and constructor parameter Settings),AUTOWIRE_BY_TYPE
Automatic assembly of Bean property constants by type (applies to all Bean property Settings and constructor parameter Settings),AUTOWIRE_CONSTRUCTOR
(Automatic assembly, suitable for constructor parameter Settings),AUTOWIRE_NO
(Do not set related types). - DependencyCheck: Checks the property dependency of the Bean
DEPENDENCY_CHECK_NONE
(No dependency check is performed),DEPENDENCY_CHECK_OBJECTS
(only object references are checked),DEPENDENCY_CHECK_SIMPLE
(Check simple types only),DEPENDENCY_CHECK_ALL
(Detect all dependencies), etc - Qualifiers: type is
Map<String, AutowireCandidateQualifier>
, describing qualifier objects for type-dependent autowiring candidates. - MethodOverrides: method rewrite set, which describes the rewrite information performed by the Bean method, implementatively divided into
LookupOverride
和ReplaceOverride
, corresponding to LookupMethod and ReplaceMethod. For details, seeMethod Injection. - EnforceInitMethod: Whether the custom initialization method is enforceable. The default is false, logical reference
org.springframework.beans.factory.support.AbstractBeanDefinition#applyDefaults
- EnforceDestroyMethod: Whether the custom destruction method is enforceable. The default is false, logical reference
org.springframework.beans.factory.support.AbstractBeanDefinition#applyDefaults
- Synthetic: Whether the current class is a synthetic class
BeanDefination inspection
BeanDefination in these fields has certain constraints, specific logic in the org. Springframework. Beans. Factory. Support. AbstractBeanDefinition# validate, MethodOverrides and FactoryMethodName are not allowed to appear repeatedly due to conflicting initialization strategies.