3

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.

image-20220510095942810

image-20220510100014497

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.

image-20220510101204396

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.

image-20220510121115810

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

  1. Initialize through reflection for singleton beans that do not have the lazy-init attribute set.
  2. Complete Bean dependency injection.

image-20220510115646893

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.

image-20220511103240376

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.

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Reprint please indicate the source Mic带你学架构 !
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!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。