Spring basic concepts and core ideas

Table of contents

1. What is Spring?

1. Get to know loC

2. Understand Spring loC

3. DI Concept Description



1. What is Spring?

What we usually call Spring refers to the Spring Framework (Spring Framework). It is an open source framework with an active and large community. This is why it has been able to endure for a long time. Spring supports a wide range of application scenarios, and it can make Java enterprise-level application development easier.

To sum it up in one sentence: Spring is an IoC container that contains many tool methods.


1. Get to know loC

What is IoC?

IoC = Inversion of Control in Chinese means "Inversion of Control", that is to say, Spring is a container of "Inversion of Control", so how do you understand this sentence?

Let’s look at a few examples first:

If you want to write a car program, the general process is as follows:

There is a coupling problem in traditional development:

In traditional development, when we change one of the parameters, the parameters in the call chain will also be affected and must be changed accordingly. This will lead to assuming that a call chain is very long. When its underlying layer changes, Other parameters also need to be changed, which will be very troublesome.

Therefore, we need to decouple the code

The improved code is a low-coupling code. When Tire is modified, the entire call chain does not need to be modified in any way.

In the two codes, the order of object creation has also changed:

Now, class A refers to class B, class B refers to class C, and class C refers to class D.

The previous approach was to use new directly when class A refers to class B. At this time, there will be a problem: when its construction method changes, the entire call chain must change.

One day, we no longer new it, but pass in the current object. At this time, although the entire call chain is still A calling B, B calling C, and C calling D, when D changes, the entire The call chain does not require any code modification, thus solving the coupling of the code. This is dependency injection, or loC.


2. Understand Spring loC

At the beginning, we will talk about: Spring is an IoC container that contains multiple tool methods. This is the core summary of Spring.

Since Spring is a loC (Inversion of Control) container, the two most basic functions it has are:

1. Store the object in the container

2. Remove the object from the container

In other words, the core function of Spring is the process of storing objects into Spring and then obtaining objects from Spring.

Benefits of storing objects in containers:

Storing objects in the loC container is equivalent to making all the tools that may be used in the future and putting them in the warehouse. You can just take them out when needed and put them back into the warehouse after use. However, the new object in the traditional code mentioned before The process can be understood as when you need to use a tool, make one now, and do not save it after use, but throw it away directly.

Spring is a loC container, which means that the creation and destruction rights of objects are managed by Spring, and Spring itself has the ability to store objects and obtain objects.


3. DI Concept Description

DI: Dependency Injection, dependency injection

DI is the so-called "dependency injection". The so-called dependency injection means that the loC container dynamically injects certain dependencies into the object during operation.

DI: During the running of the program, the (behavior) mechanism of dynamically introducing an object into the current class is called dependency injection.

In a broad sense, IoC = DI, just describing the same problem from different dimensions

But in a narrow sense, the two are different. IoC is a design idea, while DI is a specific implementation technology.

DI is a specific method of IoC implementation. For example, I have been tired for a day and want to eat something. This kind of thinking is IoC, but my specific implementation method, such as eating Haidilao, etc., is DI.

Guess you like

Origin blog.csdn.net/weixin_73616913/article/details/132689739