How is injected into the subject and spring bean creation process analysis

Article Directory:

  1. beanFactory and bean life cycle start
  2. BeanFactory refresh the whole process
  3. BeanFactoryPostProcessor and resolve BeanPostProcessor
  4. Use BeanPostProcessor achieve aop and related notes to achieve springboot Cache
  5. How to spring [paper] is injected into an object

First of all we need to know to achieve a general

  • This injection process is definitely in BeanPostProcessorthe realization of

  • spring is beanFactory.getBeanfor instance of the bean, i.e. lazy loading

  • According to the second, that is to say when the call will go to all of getBean BeanPostProcessor

  • When it comes to the second article, refresh registration process BeanFactory only BeanPostProcessorreal method of execution in getBean

  • MergedBeanDefinitionPostProcessorIs a kind of BeanPostProcessorit is designed to handle injection, such as @Autowiredand @Value, it again broke a life-cycle function, instead of the BeanPostProcessordefault lifecycle functions, so see, I posted a short Source

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

    It allows you non BeanFactoryProcessto modify the definition of Bean

@Autowired Loading process definition

Let us not look at the process of creating the bean, to see MergedBeanDefinitionPostProcessorimplementation subclasses, here to see the names of speculation AutowiredAnnotationBeanPostProcessorshould be is doing it, so we can look at the code postProcessMergedBeanDefinition next method AutowiredAnnotationBeanPostProcessor directly.

Calling down method, it is possible to know the buildAutowiringMetadatalocal real find these notes, the final checkConfigMemberswill be Memberregistered into the bean definitions, the reader how to find their own specific view the source code.

Here are just a Memberregistered into the bean definitions, the real instantiation in the process of filling the Bean, the following comes to the creation of the bean can know when injected.

Bean creation process

Speaking in front of spring is in the process of creation Bean getBean, create bean divided into several steps

  1. Gets bean definition
  2. new Bean()
  3. Perform lifecycle functions (front)
  4. Creating Dependencies
  5. Bean filling
  6. Perform life cycle function (after)

Entrance is BeanFactory.getBean, BeanFactory implementation class as DefaultListableBeanFactorythose you can find in the refresh process BeanFactory

According to the source, if the bean does not exist, it will execute the bean creation process

Gets bean definitions in this source code

final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

Followed, search its dependencies, and create a bean, it can be seen according to Bean is defined recursively create bean

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

Then create a bean

if (mbd.isSingleton()) {
    createBean(beanName, mbd, args);
}

// 真正的执行在 doCreateBean 过程中
Object beanInstance = doCreateBean(beanName, mbdToUse, args);

The first step to create a new Bean bean

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

Creation bean second step, the implementation of all the processor, contained MergedBeanDefinitionPostProcessor, so this step registration injected options

applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);

The third step is to create a bean, bean filling, where do @Autowiredinjection

populateBean(beanName, mbd, instanceWrapper);

Source can see this in depth

// 这里就是常说的根据名称注入,根据类型注入了
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
    autowireByName(beanName, mbd, bw, newPvs);
}
if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
    autowireByType(beanName, mbd, bw, newPvs);
}

Since the previously acquired through dependency, and thrown into the container, where it is reflected directly written opinions it

The fourth step to create a bean, bean initialization, there is a method of injection, injection method originally occurred in the bean initialization process, there is the life cycle of the function performed, containing BeanPostProcessorthe pre- and post-lifecycle initialization methods

Little promotion

Writing is not easy, I hope the support of open source software, and my gadgets, welcome to gitee point star, fork, put bug.

Excel common import and export, support Excel formulas
blog address: https://blog.csdn.net/sanri1993/article/details/100601578
gitee: https://gitee.com/sanri/sanri-excel-poi

Use the template code, the gadget generate code from the database, and some projects can often be used in the
blog address: https://blog.csdn.net/sanri1993/article/details/98664034
gitee: https://gitee.com/ sanri / sanri-tools-maven

Guess you like

Origin www.cnblogs.com/sanri1993/p/11876485.html