Hi, I'm Mic
A fan who has worked for 4 years encountered such a question during an interview.
"Introduction to the workflow of Spring IOC"
He said that the answer was not very good, and I hope I can help him sort it out.
Regarding this question, let's take a look at the answers of ordinary people and experts.
Ordinary people:
Um. . . . . . . .
Expert:
OK, I will answer this question in several ways.
- What is IOC
- Bean declaration method
- Workflow of IOC
The full name of IOC is Inversion Of Control, which is the inversion of control. Its core idea is to give the management authority of the object to the container.
If an application needs to use an object instance, it can be obtained directly from the IOC container. The advantage of this design is to reduce the coupling between objects in the program.
Makes the entire architecture of the program more flexible.
There are many ways to define beans in Spring, such as <bean> tags in XML, @Service, @Component, @Repository, @Bean annotations in @Configuration configuration classes, and so on.
When Spring starts, it will parse these beans and save them in the IOC container.
The workflow of Spring IOC can be roughly divided into two stages.
The first stage is the initialization of the IOC container
This stage is mainly based on the declaration method of beans such as XML or annotations defined in the program
The BeanDefinition is generated after parsing and loading, and then the BeanDefinition is registered to the IOC container.
The bean declared through annotation or xml will be parsed to get a BeanDefinition entity, which contains the basic attributes defined in the bean.
Finally, this BeanDefinition is saved to a Map collection, thus completing the initialization of the IOC.
The role of the IoC container is to process and maintain the definition information of these registered beans, which is the core of the inversion of control of the IoC container.
The second stage, complete Bean initialization and dependency injection
Then enter the second stage, this stage will do two things
- Initialize through reflection for singleton beans that do not have the lazy-init attribute set.
- Complete Bean dependency injection.
The third stage, the use of Beans
Usually we get the specified bean instance from the IOC container through @Autowired or BeanFactory.getBean().
In addition, for setting the layy-init attribute and instantiation of non-singleton beans, each time a bean object is obtained, the initialization method of the bean is called to complete the instantiation, and the Spring IOC container will not manage these beans.
The above is my understanding of the problem.
Summarize
For questions about the working principle or workflow, we must pay attention to the structure and rhythm of the answer.
Otherwise, the interviewer will feel very confused and incomprehensible, which will greatly reduce the effect of the interview.
The master's answer logic is very clear, you can refer to it.
Friends who like my works remember to like, favorite and follow.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Reprint please indicate the sourceMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。