Introduction to the

The Spring container is the heart of the Spring framework. The container creates objects, wires them together, configures them, and manages their entire life cycle from creation to destruction. The Spring container uses dependency injection (DI) to manage the components that make up a medical application. These objects are called Spring Beans.

IOC: Inversion of control

Popular understanding: in the past, objects were used in the form of new, using that depends on that, through the current object to call, serious coupling; These days it’s all single load instantiation, and all objects are managed by the Spring container, the IOC container, without us having to worry about dependencies.

DI: dependency injection

Popular understanding: after all Bean objects are loaded and initialized by IOC inversion of control, there are dependencies and calls between objects, and dependency injection is to inject the current dependencies into the object.

Dependency injection (DI) is a method of assigning dependencies to objects created in the Spring container. For example, spring is given a list of B objects and other objects that need to be created. When spring creates an object, it looks at which objects the object needs to depend on. Then check to see if the list contains any of these dependent objects, create them, and pass them to the object. Depending on many objects, you don’t need to know whether other objects exist or where or how they were created, whereas the Spring container creates dependent objects and injects them.

IOC container is a container with dependency injection function, which is responsible for the instantiation and initialization of objects, the configuration of dependencies between objects, the destruction of objects, and the search for externally provided objects. The whole life cycle of objects is controlled by the container. The objects we need to use are managed by the IOC container. We do not need to manually create objects in the new way. The IOC container will directly assemble them for us and we can directly obtain them from the IOC container when we need to use them.

So how does the Spring IOC container know which objects to manage?

We need to provide the IOC container with a configuration list that supports XML and Java annotations, lists the objects that need to be managed by the IOC container in the configuration file, and can specify how the IOC container should build these objects. When the Spring container starts, the configuration file will be loaded. These objects are then assembled for use by external visitors.

Bean concept:

The objects managed by the Spring container are collectively called Bean objects. Beans are common Java objects, and our own new objects are the same, but these objects are created and managed by Spring. We need to tell the Spring container in the configuration file which Bean objects need to be created, so we need to define the Bean objects to be created in the configuration file. These configurations, collectively known as bean definition configuration metadata, are read by the Spring container to build and assemble the objects we need.

Bean definition

Objects called beans are the backbone of the application and are managed by the Spring IoC container. A bean is an object that is instantiated, assembled, and managed through the Spring IoC container. These beans are created using configuration metadata provided by the container and defined in XML forms.

Spring configuration metadata:

The SpringIOC container is completely decoupled from the format of the configured metadata that is actually written. There are three main ways to provide configuration metadata to the Spring container:

  • Configuration files based on XML files
  • Annotation-based configuration
  • Java-based configuration

What is a DI:

The key to understanding DI is: “Who depends on whom, why depends on whom, who infuses whom, what infuses”. Let’s take a closer look:

  • Who depends on whom: Applications depend on the IoC container, of course;
  • Why dependencies are needed: Applications need IoC containers to provide external resources that objects need;
  • Who injects whom: It is obvious that the IoC container injects an application object, an object that the application depends on;
  • What is injected: The external resources (including objects, resources, and constant data) needed to inject an object.

 

Conclusion:

  1. IOC inversion of control is a design concept that hands the active control of object creation and assembly to the Spring container. The control action is reversed, which reduces the coupling degree of the system and is conducive to system maintenance and expansion. It mainly refers to that the assembly control of the object to be used is reversed. Now it’s up to the Spring container.

  2. DI refers to the method of setting dependent objects when creating objects in the Spring container. Some injection methods can make the system more flexible, such as automatic injection, which will be discussed in a later article.

  3. Spring container: Is responsible for the creation, assembly, object lookup, and object lifecycle management of objects in the container.

    Inversion of control is the end, dependency injection is the means