“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Registered Bean

1.Registering beans includes three parts: bean information resolution, container creation, and registering beans into containers2.Bean information parsing, which translates XML, annotations, configuration classes, and so on into BeanDefinition and adds it to the bean factory3.This part we focus on is spring BeanFactoryPostProcessor, this is for the use of the spring container left us extension, we can add the custom before the container to initialize some of the things, the most common is the registration information to add bean.4.Spring BeanFactoryPostProcessor and BeanPostProcessor ahead of this chapter is initialized, BeanPostProcessor bean initialization created in the late for interventionCopy the code

Spring BeanFactoryPostProcessor inheritance figure

Usually we pay more attention to spring BeanFactoryPostProcessor subinterface BeanDefinitionRegistryPostProcessor exposed way Add bean definitions of the information we need to register directly BeanDefinition, spring BeanFactoryPostProcessor main intervention bean factory registration

Initialize the Bean

1.Initialization of singleton beans is divided into creating original beans, performing dependency injection (using beanFactoryProcessor dependency injection method), and initialization (performing beanFactoryProcessor initialization method) after instance creation.2.Creating the original object is as simple as using reflection to call the no-argument constructor3.Doing dependency injection, which assigns values to properties, solves the basic problem of circular dependencies4.Initialized instance has been created, execute beanPostProcessor initialization method of postProcessBeforeInitialization PostProcessAfterInitialization InitializingBean afterPropertiesSet methodCopy the code

BeanPostProcessor inheritance figure

BeanPostProcessor generally intervenes in the process of bean creation, such as bean property injection, dependency, initialization, AOP and other implementations. You can pay more attention to the PostProcessor sub-interface. Some frameworks integrated with Spring will intervene in the process of bean creation to obtain an expected bean

Note the PostProcessor

BeanFactoryPostProcessor

1.ConfigurationClassPostProcessor built-in, used to resolve configuration class, help registered bean BeanPostProcessor1.AutowiredAnnotationBeanPostProcessor built-in, used for dependency injection properties (IOC)2.ApplicationContextAwareProcessor built-in for callback implementations Aware interface, injection of some objects3.AspectJAwareAdvisorAutoProxyCreator integration, are used to implement aopCopy the code

Spring addresses the principle of loop dependency

Principle:1.The process for getBean to create an object is to create an object using a no-argument constructor, inject a dependency, and finally initialize it2.Once the original object is created, the reference to the object is exposed and the call to getSingleton returns the bean3.Example of a recursive call to getBean:1.Start by creating object A by initializing a primitive bean2.Suppose object A injects an object B, and object B injects an object A3.When object A is created, it is immediately referenced to the level 3 cachenull)4.Start assembling property B of object A, recursively call getBean(b), initialize b, and immediately leak reference to level 3 cachenull)5.Start assembling property A of object B, recursively call getBean(a), find a reference to a in getSingleton(a), complete assembly of object B and creation of object B6.Continue to return to the property B of the assembly object A, the assembly of the property B of the object A is complete, and the creation of the object A is completeCopy the code

conclusion

As you can see, the entire process revolves around BeanFactoryPostProcessor registering beans. GetBean initializes and enhances bean instances with BeanPostProcessor to perform dependency injection (IOC) for the entire container. At the same time if the integration of aop, then registers AspectJAwareAdvisorAutoProxyCreator type BeanPostProcessor, to enhance of bean, to achieve the function of aop.