Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of BeanDefinition?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about the principle of BeanDefinition. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

First, BeanDefinition analysis 1. Basic concept understanding

First of all, I'd like to ask a question: what's the difference between a java object and a Spring Bean?

This is a classic interview question. What is java Object? Everything is an object, and all the classes within Java can be called an object after being created. SpringBean is also a java Object, but SpringBean is separated from JAVA Object. Why? Because a class can be called an object if it only needs a new to become an object, but a class needs to go through a series of life cycles if it wants to become a Spring Bean. What life cycle? We'll talk about it later!

At least from the above, we can know that Spring Bean is a special Java Object, so he must have something different from JAVA Object!

The Class object in Java can describe a JAVA Object, but because SpringBean is a special JAVA Object, the Class object cannot fully describe a SpringBean, so Spring officially developed a separate class called BeanDefinition to describe a SpringBean!

two。 Approximate structure

BeanDefinition describes a lot of things, roughly as follows:

Image-20200907164143213

It stores all the raw materials Spring needs in the process of creating bean!

3. What does he do (Spring builds its advantages)? Improve efficiency: Spring creates a class through reflection. When creating a class, you need some creation information, such as Class, such as annotations, etc., which can be cached in advance and can be directly obtained from the cache when creating bean to achieve the purpose of improving creation efficiency. Easy to modify: when spring creates an object, all the information created is created through the information stored in BeanDefinition, so we can change the result of Spring creating object by modifying the specific value within BeanDefinition! Easy to expand: we can get all the BeanDefinition information of a class through some specific interfaces, so as to complete the implementation of some specific functions! II. Spring life cycle

Through the above introduction, then you have a general understanding of BeanDefinition, then when we understand the entire Spring declaration cycle, we need to understand two concepts BeanFactoryPostProcessor, BeanPostProcessor, of course, here is just to popularize the concept, in order to enable readers to understand the Spring declaration cycle more deeply!

1. What is BeanFactoryPostProcessor?

We now know one thing from the above understanding, that is, Spring converts class into a BeanDefinition before creating the object. At this time, Spring provides us with an extension point. After reading all the class conversion into BeanDefinition, he can call back all the implementation classes that implement the BeanFactoryPostProcessor interface and pass it into the factory object, so that the user can modify the internal properties of the factory object, such as modifying the information in the BeanDefinition. To achieve the goal of manipulating the final instantiation of bean!

To put it bluntly, he will make a callback of the interface before instantiating after scanning the project to convert Class to BeanDefinition!

two。 What is BeanPostProcessor?

This class is very similar to the above class. It has two methods, and the timing of calling the two methods is also different. After instantiation, he will make the first method callback (postProcessBeforeInitialization) before calling the initialization method, and after executing the initialization method, he will make another callback (postProcessAfterInitialization). Each callback class will pass the currently created bean to the inside of the method. So that developers can customize some of the definitions of the current bean!

3. Analysis of Spring life cycle

So at this point, after we understand the three concepts of BeanDefinition, BeanPostProcessor, and BeanFactoryPostProcessor, we can try to learn the life cycle of Spring. Learning the Spring declaration cycle plays an important role in mastering the Spring source code! Only by understanding the declaration cycle of Spring, can we have a detailed source code grasp of the subsequent Spring series of technologies!

The entire life cycle of Spring is roughly divided into the following stages in terms of text description:

Initialize the bean container to facilitate the storage of all subsequent read information! Initialize the built-in class file conversion to bd initialize the bean factory and set some default values! Register some of your own built-in Bean post processors inside BeanFactory to execute the built-in BeanFactoryPostProcessor scan project to all @ Bean, @ Component.... Or xml configuration and other Spring read corresponding classes are parsed into BeanDefinition and stored in the container! Execute our custom BeanFactoryPostProcessor to register all BeanPostProcessor into the container! Initialization internationalized resource initialization event resource instantiation class populates attributes according to rules (automatic injection) callback BeanPostProcessors.postProcessBeforeInitialization method calls bean initialization method callback BeanPostProcessors.postProcessAfterInitialization method 123. BeanDefinition details 1. AbstractBeanDefinition

Although we can create a custom BeanDefinition by implementing the BeanDefinition interface, have you found that you need to set dozens of properties in order to implement this interface and create a BeanDefinition that is extremely complex?

In order to simplify this step, Spring officially provides an abstract AbstractBeanDefinition, which implements most of the methods of BeanDefinition by default and assigns default values to some attributes, which greatly simplifies the difficulty for users to implement a BeanDefinition!

I. GenericBeanDefinition

It is a subclass of AbstractBeanDefinition, and the bean we configure through annotations as well as the BeanDefiniton type of our configuration class (except @ Bean) are of type GenericBeanDefinition!

II. RootBeanDefinition

When Spring starts, it instantiates several initialized BeanDefinition, all of which are of type RootBeanDefinition, and the BeanDefinition, which includes subsequent Spring, will do a merge (all later) of type RootBeanDefinition!

The BeanDefinition we created through @ Bean is also of type RootBeanDefinition, which of course belongs to its subclass (described later)!

2. AnnotatedBeanDefinition

This interface directly inherits BeanDefinition, which extends two methods:

Image-20200907180423084

These two methods are dedicated to read annotations! All bean identified by annotations are bean of this type!

I. AnnotatedGenericBeanDefinitionimage-20200907180954450

The first case is that the configuration class, that is, the class marked with the @ Configuration annotation, will be resolved to AnnotatedGenericBeanDefinition.

The second case is that classes imported through @ Import will be resolved to AnnotatedGenericBeanDefinition

II. ConfigurationClassBeanDefinitionimage-20200907182021917

Classes imported through the @ Bean annotation will be resolved to ConfigurationClassBeanDefinition

III. ScannedGenericBeanDefinitionimage-20200907182139696

Bean created by @ Service, @ Compent, and so on will exist in the form of ScannedGenericBeanDefinition!

The above is the principle of BeanDefinition shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report