Spring bean load execution order instance parsing
This paper mainly studies the loading and execution sequence of spring beans, as follows.
Source of problem:
One bean is a and one bean is B. You want a to assign an attribute name of a when the container is instantiated to the return value of a method funb of B.
If it is simply written in a:
private B b; private String name = b.funb();
An error will be reported saying nullpointexception, because B has not been set at this time, so it is null.
The solution is the following code. At the same time, learn the execution sequence of initializingbean, object construction method and init method in spring.
Spring configuration file
Test code:
Program output is:
From here, we can see that the name attribute of a is also successfully set to the return value of B's funb method when the bean is loaded. The key point is to implement it with init method.
The loading order can also be seen as:
Constructor first ― > then set method injection of B ― > afterpropertiesset method of initializingbean ― > init method method
The summary is as follows:
The following content is excerpted from the book, but I find that even if I excerpt it again, I will have a deeper understanding of its content!
1、 Spring assembly bean process
1. Instantiation; 2. Set attribute value; 3. If the beannameaware interface is implemented, call setbeanname to set the ID or name of the bean; 4. If beanfactoryaware interface is implemented, call setbeanfactory to set beanfactory; 5. If applicationcontextaware is implemented, call setapplicationcontext to set ApplicationContext 6 Call the pre initialization method of beanpostprocessor; 7. Call the afterpropertieset() method of initializingbean; 8. Call the custom init method method; 9. Call the post initialization method of beanpostprocessor;
Spring container closing process
1. Call destroy() of disposablebean; 2. Call the customized destroy method method;
1、 Single bean
load
1. Instantiation; 2. Set attribute value; 3. If the beannameaware interface is implemented, call setapplicationcontext to set ApplicationContext 6 Call the pre initialization method of beanpostprocessor; 7. Call the afterpropertieset() method of initializingbean; 8. Call the custom init method method; 9. Call the post initialization method of beanpostprocessor;
Spring container closed
1. Call destroy() of disposablebean; 2. Call the customized destroy method method;
2、 Sequence of multiple beans
Give priority to loading the implementation of beanpostprocessor. Beans are loaded in the order of bean files and bean definitions, and in the order of bean loading (even if there is ID override when loading multiple spring files). When "setting attribute values" (step 2), if ref is encountered, it is in "instantiation" (step 1) after that, first load the subclass of the bean abstractfactorybean corresponding to the ID of Ref. after step 6, the createinstance method will be called, and then the getobjecttype method will be called. The beanfactoryutils class will also change the loading order of beans