preface

Using Java as the first development language friends, I believe that we have more or less used the Spring development framework, can say that the Spring framework is the Spring of our Java programmers, in the Spring Bean is one of the most important concepts, is the basis for learning other advanced knowledge, A Bean is simply an object managed by the Spring framework. Today we will look at how beans are created in Spring.

1. How to define a Bean

Let’s say you have a Programmer class that looks like this. This Programmer class has three attributes: name, age, and whether you have a girlfriend (P.S. The hasGirlFriend property would normally be false), as well as showMaterial, a method to display your profile.

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

    private String name;

    private Integer age;

    private Boolean hasGirlFriend;

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

Now think about designing how to describe such a Programmer object in a Spring container.

You just need the following information:

1.1 the name of the class

First of all, the class name must be required, so that the class can be loaded by the class name.

1.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, we can easily describe the instance by setting an alias.

1.3 Constructors

We know that creating an instance of a class in Java first calls the constructor of the class. When there are multiple constructors, we need to specify which constructor to use to create the object, for example, by passing in different parameter types to select different constructors.

1.4 Class property Settings

If we don’t pass a property into the constructor, for example, the constructor of “Programmer” can be used directly. If we need to set the property of the instance, we need to call the method of setting the property. Therefore, the property method is also necessary.

1.5 Initialization Method

Sometimes we need to do some of our own custom business logic after an instantiation is complete, for example, we want the above example of Programmer to show the personal data once the instantiation is complete (calling the showMaterial() method). In this case, the initialization method is appropriate.

1.6 Destruction Method

For example, there is a common understanding that resource release classes are usually put in the finally code block to ensure that resources can be released. Similarly, when a Bean is later connected to some resources and you want to release those resources after destruction, At this point, resources can be released through their destruction method.

1.7 scope

Some beans may require only one in the entire container, that is, a singleton, while others may require a different Bean for each request. In this case, the concept of scope can be used to distinguish beans of different requirements. When the container finds that the class is a singleton, it will reuse the existing Bean. Otherwise, create it again.

Of course, this is just a list of attributes that I think are important, but there are other attributes that need to be added. The definition of a Bean in the Spring framework is described through a BeanDefinition class.

Before using SpringBoot, we used XML configuration and Spring parsing to generate beans. Alternatively, we can use BeanDefinitionBuilder code to generate beans, as shown below:

/ * * *@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(); }}Copy the code

The running results are as follows:

When using XML approach is generally by calling the ClassPathXmlApplicationContext to register the beans, the constructor can be introduced to the specific XML configuration file path, can be one or more, and even can be a wildcard. The familiar Refresh method is called inside the constructor.

Digging deeper into the Refresh method, you can see that the obtainFreshBeanFactory method is called in this method to get the generated Bean, This method is invoked the abstract implementation class actually AbstractRefreshableApplicationContext refreshBeanFactory method, this method is first to determine whether there is the beanFactory at this time, If any will destroy the beanFactory first, and then to create a the beanFactory (actually DefaultListableBeanFactory type), Finally, we call loadBeanDefinitions to load the XMl configuration we defined. XmlBeanDefinitionReader is used to read the XMl configuration. Let’s take a look at Spring’s Bean generation process.

2. The process of creating beans

First let’s look at the BeanFactory class diagram, as shown below:

The overall Bean creation process is as follows:

3. Summary

This article briefly describes the main process of Spring to create beans, there are many details need to read the source code to understand, here first give yourself a small goal, the follow-up will be their own implementation of a simple version of Spring (IOC, AOP), see the next blog post…