How does Spring create a bean

Preface

Friends who use Java as the first development language, I believe that everyone has used the Spring development framework more or less. It can be said that the Spring framework is really the spring of our Java programmers. In Spring, Bean is one of the most important concepts. , Is the basis for learning other advanced knowledge. To put it plainly, Bean is actually an object managed by the Spring framework. Today we will take a look at how Bean is created in Spring.

How to define Bean

If you have a Programmer class like the following, this programmer class has three attributes: name (name), age (age), whether there is a girlfriend (hasGirlFriend) (PS Normally, the hasGirlFriend attribute should be false), and A method showMaterial to display personal information.


/**
 * @Author: mghio
 * @Date: 2020-10-05
 * @Description: Programmer.
 */
public class Programmer {

    private String name;

    private Integer age;

    private Boolean hasGirlFriend;

    public void showMaterial() {
        System.out.println("name: " + name + ", age: " + age + ", hasGirlFriend: " + hasGirlFriend);
    }
}

Now, please think about it, if you are asked to design how to describe such a Programmer object in a Spring container?

It is nothing more than the following information:

2.1

Class name

First of all, the class name is definitely needed, so that it can be loaded into this class by the class name.

2.2

Instance alias

When we have multiple instances of a class in a container or do not want to describe an instance by a class name, then it is convenient to describe the instance by setting an alias.

2.3

Constructor

We know that creating an instance of a class in Java will first call the constructor of the class. When there are multiple constructors, you need to clearly describe which constructor to use to create the object, such as by passing in different parameter types to select Different constructors.

2.4

Class attribute settings

When we don’t pass in properties in the constructor, for example, the above Programmer can be created directly through the parameterless constructor. If you need to set the properties of the instance later, you need to call the property setting method to set it, so the property method It is also necessary.

2.5

Initialization method

Sometimes we need to do some custom business logic after an instantiation is completed. For example, we want the Programmer in the above example to display personal information after instantiation is completed (call the showMaterial() method). This scenario uses the initialization method It's very suitable.

2.6

Destruction method

When it comes to destruction, everyone may think about resources. For example, a consensus is that everyone generally puts the work of releasing resources in the finally code block to ensure that resources can be released. Similarly, when a Bean is connected to use some resources afterwards, When you want to release these resources after destruction, you can release the resources through its destruction method.

2.7

Scope

Some beans may need to have only one in the entire container, which is a singleton, and some may require that the corresponding bean for each request is different. At this time, a scope concept can be used to distinguish beans with different requirements. When the container finds This class is singleton, it will reuse the existing Bean, otherwise it will be recreated.

Of course, here are only some attributes that I think are more important, and there are other attributes that need to be added. Bean definition in the Spring framework is described by a BeanDefinition class.

How does Spring create a bean

Before using SpringBoot, we used XML configuration and Spring to parse and generate Beans. At the same time, we can also use BeanDefinitionBuilder to generate Beans by code. The specific code is as follows:


/**
 * @Author: mghio
 * @Date: 2020-10-05
 * @Description:
 */
public class ProgrammerTest {

    public static void main(String[] args) {
        new Programmer().showMaterial();
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(Programmer.class);
        beanDefinitionBuilder.addPropertyValue("name", "mghio");
        beanDefinitionBuilder.addPropertyValue("age", 18);
        beanDefinitionBuilder.addPropertyValue("hasGirlFriend", false);

        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerBeanDefinition("programmer", beanDefinitionBuilder.getBeanDefinition());

        Programmer programmer = (Programmer) beanFactory.getBean("programmer");
        programmer.showMaterial();
    }

}

The results are as follows:

How does Spring create a bean

When using the XML method, the Bean is generally registered by calling ClassPathXmlApplicationContext. Its constructor can pass in the path of the specific XMl configuration file, which can be one or more, or even a wildcard. The familiar refresh method will be called inside the constructor.

How does Spring create a bean

Going deep into the refresh method, you can find that the obtainFreshBeanFactory method is called in this method to obtain the generated Bean. This method actually calls the refreshBeanFactory method of the abstract implementation class AbstractRefreshableApplicationContext. This method first determines whether there is a beanFactory at this time, if there is BeanFactory will be destroyed first, and then a BeanFactory will be recreated (actually of type DefaultListableBeanFactory), and finally loadBeanDefinitions will be called to load the XMl configuration we defined. The method uses the XMl configuration read by XmlBeanDefinitionReader. Let’s take a closer look. Let's look at the process of Spring generating Bean.

The process of creating Bean

First we look at the BeanFactory class diagram, as shown below:

How does Spring create a bean
The overall creation process of Bean is as follows:
How does Spring create a bean

to sum up

This article briefly describes the main process of Spring's creation of Beans. There are many details that need to be studied in-depth to understand the source code. Here first give yourself a small goal, and then I will implement a simple version of Spring (IOC, AOP) by myself. What's going on, please listen to the next breakdown. . .

Guess you like

Origin blog.51cto.com/15075507/2607599