Interface represents a capability, implements an interface, that is, has a capability.

BeanDefinition relates to beans just as a class relates to an object. The data structure of a class in Spring is called a BeanDefinition. The object we get from BeanDefinition is the Bean we need.

I think understanding beans and BeanDefinitions is fundamental and key to understanding Spring’s entire architecture. I’ll start with beans and BeanDefinitions and work my way through spring.

Studying beans and BeanDefinitions, I think it can be divided into two parts for step-by-step parsing.

  • Start with definitions: Explore the Bean/BeanDefinition definition in Spring.
  • On the other hand, start with actions (operations) : investigate Bean/BeanDefinition operations.

BeanDefinition

BeanDefinition is the bean description in Spring, With BeanDefinition we can create a Bean. BeanDefinition is the Bean definition form in Spring. Now let’s look at the interfaces and classes associated with BeanDefinition.

define

  • The BeanDefinition interface is a top-level basic interface used to describe beans, which contains Bean metadata such as Bean class name, scope, properties, constructor parameter list, dependent Bean, whether it is a singleton class, whether it is lazy to load, and so on.

upward

  • BeanMetadataElement interface: BeanDefinition metadata that returns the source of the Bean
  • AttributeAccessor interface: Provides the ability to manipulate BeanDefinition attributes,

down

  • AbstractBeanDefinition class: An abstract class implements a subset of BeanDefinition operations and defines many of the default properties of a BeanDefinition. It is based on AbstractBeanDefinitions that Spring has derived a series of BeaDefinitions.

    Here we can look at the overridden equals(), HashCode (), and toString() methods

    In addition, the initMethodName and destroyMethodName properties are related to the life cycle of the bean.

The next. Let’s look at a couple of classes derived from AbstractBeanDefinition

  • RootBeanDefinition: Represents an XML, Java Config BeanDefinition

  • ChildBeanDefinition: can a ChildBeanDefinition have the ability to inherit a configuration from its parent

  • GenericBeanDefinition: GenericBeanDefinition is preferred for beans registered after spring2.5. GenericBeanDefinition allows dynamic setting parent bean. GenericBeanDefinition can serve as RootBeanDefinition and ChildBeanDefinition substitute.

  • AnnotatedBeanDefinition interface: Represents the annotation type BeanDefinition. There are two important properties, AnnotationMetadata and MethodMetadata that represent AnnotationMetadata and MethodMetadata for beandefinitions that implement this interface and get AnnotationMetadata and MethodMetadata, respectively.

  • AnnotatedGenericBeanDefinition class: said @ Configuration to annotate BeanDefinition annotation class

  • ScannedGenericBeanDefinition class: said @ Component, @ Service, @ Controller annotations such as annotation Bean class

operation

Actions can also be divided into two types: one is self-directed operations: self-provided actions that can operate itself and the other is external operations on BeanDefinition

  • BeanDefinitionRegistry interface: Ability to add, view, or delete BeanDefinitions. Only one BeanDefinition can be registered at a time.

    The implementation class SimpleBeanDefinitionRegistry DefaultListableBeanFactory, GenericApplicationContext etc in general implementation class has a private final Map < String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap() to store BeanDefinition.

  • BeanDefinitionReader interface: Either the BeanDefinitionRegistry construct can be used. Configurations can also be loaded as multiple BeanDefinitions and registered with the BeanDefinitionRegistry via loadBeanDefinitions. Efficient version of BeanDefinitionRegistry. The implementation class has XmlBeanDefinitionReader that reads BeanDefinition from XML; PropertiesBeanDefinitionReader BeanDefinition read from the Properties file

  • AnnotatedBeanDefinitionReader class with annotations BeanDefinition to register

  • ClassPathBeanDefinitionScanner class: can scan to @ Component @ Repository @ Service @ Controller BeanDefinition registered into the container.

Other form

  • BeanDefinitionHolder: BeanDefinition wrapper class.

Bean

The Bean is the object we need, the result we get from Spring, which is the object instance

define

At the definition level, beans are exactly what we need.

operation

Let’s take a look at what interfaces or classes beans have to do with spring.

  • SingletonBeanRegistry interface: corresponding to the registration of BeanDefinition. Bean has a similar interface to operation Bean. SingletonBeanRegistry interface provides registration of Bean, access, existence judgment, etc.
  • InitializingBean: For the Bean that implements InitializingBean, it will execute afterPropertiesSet(); After all the bean properties have been set.
  • InstantiationStrategy: provides a policy interface for Bean instantiation,
  • DisposableBean: For a Bean that implements DisposableBean, it will run destroy() after the Spring container releases the Bean
  • FactoryBean: The Bean that produces beans.

Other form

  • BeanWrapper: Wrapper around beans. BeanWrapper can be thought of as a product of the BeanDefinition to Bean process. It can be called a “low-level Bean” and we would not normally use it in a real project. BeanWrapper is an important component class in the Spring framework. It acts as a proxy class. Spring delegates BeanWrapper to fill in Bean properties. After the bean instance is InstantiatioonStrategy created, the Spring container will bean instance through BeanWrapper wrapped, is through the BeanWrapper setWrappedInstance ()

Conclusion:

BeanDefinition is material and Bean is finished product. Understanding the relationship between BeanDefinition and Bean is the basis for understanding Spring.

Next share material warehouse, finished product warehouse, embodied in the operation of “storage”. Welcome everyone to pay attention to my public number [source code action], the latest personal understanding timely.