One, foreword

  • Springboot source code parsing is a big project, line by line to study the code, can be boring, and not easy to stick to.
  • We do not pursue big and complete, but try to study a small knowledge point every time, and eventually together, this is our Springboot source tube leopard series.

Second, the BeanDefinition

  • Spring helps us manage beans through BeanDefinition. If you want to dig into the source code, you can’t get around BeanDefinition
  • Let’s look at the source code and see what BeanDefinition does. Okay

Third, source code analysis

Let’s take a look at this class: BeanDefinition

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement { String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON; String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE; int ROLE_APPLICATION = 0; int ROLE_SUPPORT = 1; int ROLE_INFRASTRUCTURE = 2; void setParentName(@Nullable String parentName); @Nullable String getParentName(); void setBeanClassName(@Nullable String beanClassName); @Nullable String getBeanClassName(); void setScope(@Nullable String scope); @Nullable String getScope(); void setLazyInit(boolean lazyInit); boolean isLazyInit(); void setDependsOn(@Nullable String... dependsOn); @Nullable String[] getDependsOn(); void setAutowireCandidate(boolean autowireCandidate); boolean isAutowireCandidate(); void setPrimary(boolean primary); boolean isPrimary(); void setFactoryBeanName(@Nullable String factoryBeanName); @Nullable String getFactoryBeanName(); void setFactoryMethodName(@Nullable String factoryMethodName); @Nullable String getFactoryMethodName(); ConstructorArgumentValues getConstructorArgumentValues(); default boolean hasConstructorArgumentValues() { return ! getConstructorArgumentValues().isEmpty(); } MutablePropertyValues getPropertyValues(); default boolean hasPropertyValues() { return ! getPropertyValues().isEmpty(); } boolean isSingleton(); boolean isPrototype(); boolean isAbstract(); int getRole(); @Nullable String getDescription(); @Nullable String getResourceDescription(); @Nullable BeanDefinition getOriginatingBeanDefinition(); }Copy the code

Defines various attributes of a bean:

  • Parent: setParentName
  • Class name: setBeanClassName
  • Scope: setScope
  • Lazy loading: setLazyInit
  • Rely on: setDependsOn
  • Dependent: setAutowireCandidate
  • Automatic loading: setPrimary
  • The factory bean: setFactoryBeanName
  • Factory method: setFactoryMethodName
  • Singleton: isSingleton
  • Example: isPrototype
  • Static: isAbstract

Let’s open the graph properties with idea:

  • Inherits AttributeAccessor and has the ability to get attributes
  • Inherit from BeanMetadataElement and have the ability to get sources
  • Interfaces are defined to describe a bean

We are looking at its implementation class:

  • Spring’s usual approach: defining interfaces, writing abstract classes to implement interfaces (core functionality is written here), various implementations
  • Interface: BeanDefinition
  • Abstract class: AbstractBeanDefinition
  • Implementation class: RootBeanDefinition ChildBeanDefinition, GenericBeanDefinition AnnotatedGenericBeanDefinition, ScannedGenericBeanDefinition
  • Root and Child were used early, and Geniric has been used since spring2.5

There is also an encapsulation class: BeanDefinitionHolder

public class BeanDefinitionHolder implements BeanMetadataElement { private final BeanDefinition beanDefinition; private final String beanName; @Nullable private final String[] aliases; . }Copy the code
  • This is used when extending aliases

  • In this video, we’re going to take a look at the entire beanDefinition family, which we’ll use when we talk about the source code.

Welcome to follow the wechat public number: Fengji, more technical learning and sharing.