Spring IOC, a nanny-level guide to DI principles, let the interviewer feel your charm

Spring IOC, a nanny-level guide to DI principles, let the interviewer feel your charm

1. What is IOC?

Insert image description here

1. Start: Creation of Spring IoC container.

Container initialization: Initialize the IoC container, including loading configuration files, parsing configuration files, etc.
Load XML/Java configuration file: Load the configuration information defined by the Bean from the file.
Parse the configuration file: parse the information in the configuration file and create the corresponding BeanDefinition object.
Create BeanDefinition object: Create a corresponding BeanDefinition object according to the definition of the configuration file.

2. Register the bean definition to the IoC container:

Register BeanDefinition objects with the IoC container so that the container can manage these beans.

3.Resolve dependencies:

According to the dependencies in the configuration file, parse and create corresponding dependencies.
Instantiate Bean: Use reflection mechanism to instantiate each Bean.
Execute the Bean's custom initialization method: After instantiation, execute the Bean's custom initialization method (such as init-method).

4. Fill in the Bean properties:

Set the attribute value for each Bean based on the attribute information in the configuration file.
If the Bean implements the BeanNameAware interface, set the Bean name: If the Bean implements the BeanNameAware interface, a unique name will be set for it.
If the Bean implements the BeanFactoryAware interface, inject a BeanFactory instance: If the Bean implements the BeanFactoryAware interface, a BeanFactory instance will be injected into it so that it can access other beans in the container.
If the Bean implements the ApplicationContextAware interface, inject an ApplicationContext instance: If the Bean implements the ApplicationContextAware interface, an ApplicationContext instance will be injected into it so that it can access other resources in the container.
Execute pre-processing of BeanPostProcessor: Before initialization, process each Bean through the BeanPostProcessor interface.

5. Execute custom destruction method:

When destroyed, execute the Bean's custom destruction method (such as destroy-method).

6. Save the Bean instance to the IoC container:

Save each initialized Bean instance to the IoC container for subsequent use.

7.oC container can provide/inject Bean instances:

The IoC container can provide or inject initialized Bean instances for use by other components.
End: Complete the entire process.

2. What is DI?

Insert image description here

1.Spring DI container:

A container provided by Spring for managing beans. It can obtain or inject Bean instances by name. Containers can automatically manage the life cycle of beans, including instantiation, initialization, use and destruction, etc.

2. Load the configuration file:

Load configuration files from the file system, such as XML or Java configuration files. Configuration files can also be loaded in other ways, such as through annotations or programmatically.

3. Parse the configuration file:

Parse the information in the configuration file and convert it into an internal data structure for subsequent processing. The parser will vary according to the type of configuration file. For example, XML configuration files use XML parsers, Java configuration files use Java parsers, etc.

4. Create a BeanDefinition object:

Create a corresponding BeanDefinition object according to the definition of the configuration file. Each Bean is represented by a BeanDefinition object. The BeanDefinition object contains Bean name, type, attributes and other information.

5. Register the bean definition to the container:

Register BeanDefinition objects with the DI container so that the container can manage these beans. Each bean definition has a unique name by which the bean can be obtained or injected. The container can also save other information about the Bean, such as dependencies, custom methods, etc.

6.Resolve dependencies:

According to the dependencies in the configuration file, parse and create corresponding dependencies. These dependencies are used to describe the dependencies between beans so that dependencies can be injected during instantiation. Resolvers vary depending on the type of dependency, such as resolving dependencies by name or by resolving dependencies through constructor parameters.
Instantiate Bean: Use reflection mechanism to instantiate each Bean. According to the dependency relationship, the required dependencies are injected into the Bean. In this step, the container will automatically manage the bean life cycle using IoC. The container can also automatically call Bean's custom methods as needed.

7. Fill in the Bean properties:

Set property values ​​for each Bean based on the property information in the configuration file. These property values ​​can be set through configuration files or annotations. The container can also automatically set attribute values ​​or call custom attribute filling methods as needed.

8. Execute custom initialization method:

After instantiation, execute the Bean's custom initialization method (such as init-method). This method can perform some custom logic during Bean initialization. The container can also automatically call custom initialization methods as needed.

9. Execute custom destruction method:

When destroyed, execute the Bean's custom destruction method (such as destroy-method). This method can execute some custom logic when the Bean is destroyed. The container can also automatically call custom destruction methods as needed.

3. IOC and DI interaction

Insert image description here
1. Added a choice of configuration file types, including XML configuration files, Java configuration files, annotation configuration files and other configuration methods. These configuration files are used to define the properties and dependencies of the beans. After loading the configuration file, the parser will parse the configuration file and convert it into an internal data structure for subsequent processing. The parser will vary according to the type of configuration file. For example, XML parser is used to parse XML configuration files, Java parser is used to parse Java configuration files, etc. The parser registers the parsed information into the IoC container so that the container can manage these beans. The container will create the corresponding Bean instance based on the registered Bean definition and automatically manage its life cycle. After instantiating the bean, the container will automatically inject the required dependencies and automatically call the custom initialization method and destruction method. Finally, the container saves the bean instance to the container for subsequent use. When a bean needs to be used, the container can obtain bean instances by name or other means and use them to complete various tasks. When using beans, you can call their various methods, including custom methods and injected dependencies.

The following is a flowchart summary of how Spring IOC and DI interact:

Spring IoC container is a container used to manage beans. It registers bean definitions by loading configuration files, instantiates beans based on dependencies, and automatically manages their life cycles.
Configuration files are used to define Bean properties and dependencies, including XML configuration files, Java configuration files, annotation configuration files and other configuration methods.
When the container is initialized, the ApplicationContext object will be created and the BeanDefinition object will be loaded. ApplicationContext is a high-level interface for IoC containers, providing more features, such as automatic assembly and custom life cycle methods.
After instantiating the Bean, the container will automatically inject the required dependencies and automatically call the custom initialization method and destruction method.
The container saves the Bean instance into the container for subsequent use. When you need to use beans, you can obtain bean instances through the container and use them to complete various tasks.
Through the flow chart, you can learn more about the process of how Spring IOC and DI interact, including loading configuration files, parsing configuration files, creating BeanDefinition objects, registering Bean definitions, parsing dependencies, and instantiating Beans. , fill in Bean properties, execute custom initialization methods and destruction methods and other steps.

Fourth, the interviewer will usually ask about IOC and DI.

IOC and DI are important concepts in the Spring framework, which are used to achieve object decoupling. IOC is a design idea, that is, inversion of control. At its core, it moves the creation and management of objects from application code into a container or framework. The traditional development method is that application code directly controls and instantiates objects, while inversion of control allows the framework to control the creation, management, assembly, and invocation of objects. Application code only needs to use objects indirectly through interfaces or abstract classes. Implemented object decoupling. DI is one of the specific implementation methods of IOC idea, that is, dependency injection. It achieves loose coupling between objects by injecting other objects on which the object depends when the object is created. Dependency injection can be implemented through constructor, setter method or interface injection. With dependency injection, an object is no longer responsible for creating or managing other objects it depends on, but is managed through the IOC container. Therefore, it can be explained that IOC is a design idea that transfers the creation and management rights of objects to the framework through inversion of control to achieve decoupling of objects;And DI realizes the IOC idea One of the specific implementation methods is to achieve decoupling between objects through dependency injection.

Guess you like

Origin blog.csdn.net/qq_49841284/article/details/134864049