To learn About Spring IOC, just finish reading this article

instructions

website

The Spring framework implementation of inversion of Control (IOC) principle is introduced. IoC is also known as dependency injection (DI). In this process, objects define their dependencies (that is, other objects used with them) only by constructor parameters, factory method parameters, or properties set on the object instance after construction or return from the factory method. The container then injects those dependencies when the bean is created. This process is essentially a reverse process (hence Control Inversion) of the bean itself that controls the instantiation or location of its dependencies using a direct construction of the class or a mechanism such as a service locator pattern. The basic concepts of IOC cannot be left out of the discussion of beans, which are governed by IOC.

This diagram illustrates graphically that the process of obtaining dependent objects has been reversed.

Bean definition

  • Id
  • Class
  • Scope
  • Constructor arguments
  • Properties
  • Autowiring mode
  • lazy-initialization mode
  • initialization/destruction method
  • Id and Class are not explained here. They must be unique in the project, otherwise the startup will be reported.

  • The Scope Scope

    1. Singleton: a singleton in which only one Bean exists in a container. The default is singleton mode. In view of this situation, pooling management encapsulation can be carried out to achieve fast response.

    The purpose of pooling is to improve object reuse rate and customize encapsulation. Pooling is suitable for concurrent requests with the level of 10w or higher.

  1. Prototype: multiple instances, each time the object is new from the container. Singletons and multiinstances can be viewed based on the object’s hash value haseCode. Objects injected through annotations are singletons.

  2. Request: Each HTTP request creates an instance that is valid only for the current request

  3. Session and Global Session: repeat the preceding steps

  • Constructor Constructor injection

  • Properties Set value injection

  • Autowiring Mode Automatic assembly mode

Bean life cycle

  • Initialize the
  • The assignment
  • call
  • The destruction

This leads to question one: How are interdependent beans initialized?

If not, the problem of loop dependencies is thrown.

During instantiation, the Bean calls its own constructor to initialize, and then assigns values to its member variables.

Member variable assignments take two forms

    1. Constructor injection
    2. Set injection (usually via the Set method)

If you instantiate beans that depend on each other, you cannot inject them through the constructor.

Question 2: How do YOU ensure that beans are unique throughout the container

The double lock pattern from the singleton pattern is used here

Bean assembly

Divided into automatic transfer and manual assembly

Assembly by using annotations or XML

I won’t elaborate too much here

Say something about the interview

In addition to explaining dependency injection and inversion of control to the interviewer, you can also talk about beans. IOC is related to beans, including the following points of knowledge, interested in the source code or the official website

  • Bean definition
  • Bean life cycle
  • Bean assembly

And you can also derive that

  • Bean pooling processing
  • Bean creation process
  • Bean dependencies
  • The three-level cache of the container