Home  >  Article  >  Java  >  How spring injects objects and analysis of bean creation process

How spring injects objects and analysis of bean creation process

angryTom
angryTomforward
2019-11-28 17:47:533091browse

How spring injects objects and analysis of bean creation process

First you need to know a rough implementation

● This injection process must be implemented in BeanPostProcessor

●spring instantiates beans in beanFactory.getBean, that is, lazy loading

● According to the second article, that is to say, all BeanPostProcessors will be called only when getBean

● The second article mentioned that the refresh process of BeanFactory only registers BeanPostProcessor, and is actually executed in the getBean method

(Recommended video: java video tutorial

MergedBeanDefinitionPostProcessor is also a kind of BeanPostProcessor. It creates a new life cycle function, replacing the default life cycle function of BeanPostProcessor. Let’s see it this way. I will post a short source code

for (BeanPostProcessor bp : getBeanPostProcessors()) {
    if (bp instanceof MergedBeanDefinitionPostProcessor) {
        MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp;
        bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName);
    }
}

It allows you to modify the Bean definition in non-BeanFactoryProcess

InstantiationAwareBeanPostProcessor is also a BeanPostProcessor. It also redefines a life cycle function, which allows the property value to be injected into the property object

@Autowired The process of loading definitions

Let’s not look at the bean creation process first, but look at the implementation subclass of MergedBeanDefinitionPostProcessor. Here is the name guess

AutowiredAnnotationBeanPostProcessor This should be what it does, so we can look directly at the code of the postProcessMergedBeanDefinition method of

AutowiredAnnotationBeanPostProcessor.

Following the method call, you can know that buildAutowiringMetadata is where these annotations are actually found. Finally, checkConfigMembers registers Member into the bean definition. Readers who want to know how to find it can check the source code themselves.

Here we only register the Member into the bean definition, and the real instantiation occurs during the process of filling the bean. Let’s talk about the creation process of the bean below and you can know when it is injected.

Bean creation process

As mentioned earlier, spring creates Bean in the process of getBean. Creating a bean is divided into several steps

1. Get bean definition

2. new Bean()

3. Execute life cycle function (before)

4. Create dependencies

5 . Fill in the bean

6. Execute the life cycle function (after)

The entrance is BeanFactory.getBean, and the implementation class of BeanFactory is DefaultListableBeanFactory. You can find these in the refresh process of BeanFactory

According to the source code, if the bean does not exist yet, the bean creation process will be executed

Get the bean definition in this source code

final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

Follow it, search for its dependencies according to the Bean definition item, and create the bean, it can be seen that the bean is created recursively

String[] dependsOn = mbd.getDependsOn();
for (String dep : dependsOn) {
    getBean(dep);
}

Then the bean is created

if (mbd.isSingleton()) {
    createBean(beanName, mbd, args);
}
// 真正的执行在 doCreateBean 过程中
Object beanInstance = doCreateBean(beanName, mbdToUse, args);

The first step to create the bean is new Bean

if (instanceWrapper == null) {
    instanceWrapper = createBeanInstance(beanName, mbd, args);
}

The second step is to create the bean Step 1: Execute all processors, including MergedBeanDefinitionPostProcessor, so register the injection option in this step

applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);

Create the bean The third step is to fill the bean, the @Autowired injection done here

populateBean(beanName, mbd, instanceWrapper);

The final processing process In the postProcessPropertyValues ​​function of AutowiredAnnotationBeanPostProcessor

metadata.inject(bean, beanName, pvs);

Because the dependencies have been obtained previously and thrown into the container, so here you can just write them in directly using reflection

Creating the bean The fourth step is to initialize the bean. There is a method injection here. The method injection originally occurred in the process of initializing the bean, and the life cycle function is executed, including the pre-post life cycle of the BeanPostProcessor, the initialization method, etc.

Small explanation: AutowiredAnnotationBeanPostProcessor is both a MergedBeanDefinitionPostProcessor and an InstantiationAwareBeanPostProcessor

This article comes from the php Chinese website, java tutorial column, welcome to learn!

The above is the detailed content of How spring injects objects and analysis of bean creation process. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete