[JavaEE Advanced] Spring Core and Design Ideas

insert image description here

insert image description here


1. What is Spring?

What we usually refer to Springis Spring Framework(Spring framework), which is a lightweight Java open source framework with a large and active community. Spring was created to solve the complexity of enterprise application development. It not only supports a wide range of application scenarios, but also makes Java enterprise-level application development easier.

How to summarize Spring in one sentence: Spring is an IoC container that contains many tools and methods.

There are two key points in the above sentence, one is that it contains many tools and methods , and the other is the IoC container .

It is not difficult for us to understand the tool method, but what is the IoC container? As for the question of IoC container, we divide it into two sub-questions, what is IoC? What is a container?

1.1, What is a container?

A container is a (basic) device used to hold something. ——From: Baidu Encyclopedia

In daily life, containers can be seen everywhere. For example, the water cup we use is a container, which is a device used to hold water; the bookshelf is also a container, which is a device used to hold books...

And in programming, containers are everywhere. For example, the integer array we use is a container, which is a device used to store integer data; map is also a container, which is a device used to store key-value pairs...

Spring is also a container, so what kind of container is Spring? Spring is an IoC container.

1.2, what is IoC?

IoC is Inversion of Controlthe abbreviation of , translated into Chinese means inversion of control, that is to say, Spring is an inversion of control container .

So how to understand this inversion of control? We understand from the following example.

Example premise: Build a car (Car Class), but the car needs to rely on the body (FrameWork Class), and the body needs to rely on the chassis (Bottom Class), and the chassis needs to rely on tires (Tire Class), the tire size is specified as 17

There are two ways to implement the above example, namely traditional program development and inversion of control program development.

1) Traditional program development

The principle of traditional program development is to create subordinate classes in each current class, so as to realize the dependency relationship between classes.

Traditional program development –> the implementation code of the final program is as follows:

1. Car (vehicle) class

public class Car {
    
    
    private Framework framework;
    public Car(){
    
    
        framework = new Framework();
    }
    public void init(){
    
    
        System.out.println("do car ...");
        framework.init();
    }
}

2. Framework (body) class

public class Framework {
    
    
    private Bottom bottom;
    public Framework(){
    
    
        bottom = new Bottom();
    }
    public void init(){
    
    
        System.out.println("do framework ...");
        bottom.init();
    }
}

3. Bottom (chassis) class

public class Bottom {
    
    
    private Tire tire;
    public Bottom(){
    
    
        tire = new Tire();
    }
    public void init(){
    
    
        System.out.println("do bottom ...");
        tire.init();
    }
}

4. Tire category

public class Tire {
    
    
    private int size = 17;
    public Tire(){
    
    
    }
    public void init(){
    
    
        System.out.println("do tire >> size = " + size);
    }
}

5, Test (test) class

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Car car = new Car();
        car.init();
    }
}

6. Test run results

insert image description here

2) Inversion of control program development

The principle of inversion of control program development is to change the creation of lower-level classes in each current class to transfer and inject lower-level classes in the current class, so as to realize the dependency relationship between classes.

Inversion of control program development –> the implementation code of the final program is as follows:

1. Car (vehicle) class

public class Car {
    
    
    private Framework framework;
    public Car(Framework framework){
    
    
        this.framework = framework;
    }
    public void init(){
    
    
        System.out.println("do car ...");
        framework.init();
    }
}

2. Framework (body) class

public class Framework {
    
    
    private Bottom bottom;
    public Framework(Bottom bottom){
    
    
        this.bottom = bottom;
    }
    public void init(){
    
    
        System.out.println("do framework ...");
        bottom.init();
    }
}

3. Bottom (chassis) class

public class Bottom {
    
    
    private Tire tire;
    public Bottom(Tire tire){
    
    
        this.tire = tire;
    }
    public void init(){
    
    
        System.out.println("do bottom ...");
        tire.init();
    }
}

4. Tire category

public class Tire {
    
    
    private int size = 17;
    public Tire(){
    
    
    }
    public void init(){
    
    
        System.out.println("do tire ... >> size = " + size);
    }
}

5, Test (test) class

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Tire tire = new Tire();
        Bottom bottom = new Bottom(tire);
        Framework framework = new Framework(bottom);
        Car car = new Car(framework);
        car.init();
    }
}

6. Test run results

insert image description here

3) Defects of traditional program development

In the above procedure, the tire size is fixed. However, as the demand for cars increases, the individual demand will also increase. At this time, we need to process tires of various sizes. At this time, it is necessary to modify the above program code. The modified code is as follows:

1. Car (vehicle) class

public class Car {
    
    
    private Framework framework;
    public Car(int size){
    
    
        framework = new Framework(size);
    }
    public void init(){
    
    
        System.out.println("do car ...");
        framework.init();
    }
}

2. Framework (body) class

public class Framework {
    
    
    private Bottom bottom;
    public Framework(int size){
    
    
        bottom = new Bottom(size);
    }
    public void init(){
    
    
        System.out.println("do framework ...");
        bottom.init();
    }
}

3. Bottom (chassis) class

public class Bottom {
    
    
    private Tire tire;
    public Bottom(int size){
    
    
        tire = new Tire(size);
    }
    public void init(){
    
    
        System.out.println("do bottom ...");
        tire.init();
    }
}

4. Tire category

public class Tire {
    
    
    private int size = 17;
    public Tire(int size){
    
    
        this.size = size;
    }
    public void init(){
    
    
        System.out.println("do tire >> size = " + size);
    }
}

5, Test (test) class

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Car car = new Car(20);
        car.init();
    }
}

6. Test run results

insert image description here

According to the modified code above, it is not difficult to see that the problem in traditional program development is: when the bottom class is changed, all codes in the entire call chain need to be modified.

The inversion of control program development can solve the above problem very well. No matter how the underlying class changes and changes, the entire call chain does not need to be changed.

4) Comparison and summary of development methods

The order of object creation in traditional program development is: Car -> Framework -> Bottom -> Tire

The order of object creation in inversion of control program development is: Tire -> Bottom -> Framework -> Car

The comparison effect diagram of the two methods:

From the above comparison, it is not difficult to see: the implementation code of program development, the creation order of classes is reversed.

The implementation code of traditional program development is that Car controls and creates Framework, Framework creates and creates Bottom, and so on.

However, the control rights of inversion-of-control program development have been reversed. It is no longer the upper-level objects that create and control the lower-level objects, but the lower-level objects are injected into the current object. At this time, the lower-level control rights are no longer controlled by the upper-level class. In this way, no matter what changes occur in the lower class, the current class will not be affected. This is a typical inversion of control, which is the implementation idea of ​​IoC.

5) IoC summary

From the above example, we know what inversion of control is and its implementation ideas.

IoC is an idea, not a technology. Its implementation idea is to inject subordinate objects into the current class by way of delivery, so as to realize the inversion of control rights.

The idea of ​​IoC realizes the function of decoupling, reduces the dependency between modules, improves the independence of the program, and greatly reduces the difficulty of maintaining and debugging the program code.

Coupling can also be called program dependency. The coupling (correlation between codes) of a good program code is very low, which means that the dependencies between programs are relatively weak.

Deep understanding of IoC control inversion:

In traditional program development, we create objects directly inside the object through new. The program actively creates and directly obtains dependent objects, and the object actively receives dependent objects, which is the forward rotation.

In the inversion of control program development, IoC has a special container to create and store these objects, that is, use the Ioc container to control the creation and storage of objects, and help us find and inject dependent objects. Objects passively accept dependencies object, which is the inversion.


2. Understanding of Spring IoC

Understand clearly what is a container? And what is IoC? These two questions, then we will continue to talk about how to understand the sentence that Spring is an IoC container?

The two most basic functions of the IoC container are: storing objects in the container and taking objects out of the container.

That is to say, the core function of learning Spring is the process of learning how to store objects in Spring and then get objects from Spring.

IoC is to hand over the object creation and calling process to Spring for management through the IoC container, eliminating the need to use the "new class name" method to create objects.

Summary: Spring is an IoC container, which means that the rights to create and destroy objects are all managed by Spring, and it has the ability to store and retrieve objects.

Induction: The difference between inversion of control program development and traditional program development:

The inversion of control program development is to create and store all the tool classes that may be used in future operations into the IoC container, obtain them directly from the IoC container when needed, and put them back into the IoC container after use; while traditional programs The development is to create and use it directly every time it is needed, and discard it without saving it after use, and it needs to be recreated and called the next time it is used.

From this, the advantages of Spring IoC can be summarized:

  1. Decoupling reduces the dependency between classes, improves the independence of the program, and reduces the difficulty of maintaining the program code.
  2. It is more convenient to use , no need to manually create objects, and no need to pay attention to the dependencies behind this object.
  3. Efficiency is more efficient . It can be obtained directly from the IoC container when it needs to be used, and put back into the IoC container after use. It does not need to be recreated every time it is used.

3. What is DI?

DI is Dependency Injectionthe abbreviation of DI, translated into Chinese means dependency injection.

The so-called dependency injection is the behavior that the IoC container dynamically injects certain dependencies into objects during runtime.

Dependency Injection (DI) and Inversion of Control (IoC) describe the same thing from different angles.

And this matter refers to the decoupling between objects through the introduction of IoC containers and the use of dependency injection.

loC is a kind of ideological goal, which belongs to the guiding principle. In order to realize the ideological goal, there must be a feasible implementation plan in the end, and DI belongs to the specific implementation method.


Core summary

1. What is Spring? How to understand Spring?

Answer: Spring is a popular framework. Summarize Spring in one sentence. Spring is an IoC container that contains many method tools. He has two functions: storing objects into containers and getting objects from containers.

2. What are IoC and DI? What's the difference?

Answer: IoC is Inversion of Controlan acronym for Inversion of Control, and DI is Dependency Injectionan abbreviation for Dependency Injection. The two describe the same thing from different angles. The difference between the two is that IoC is an idea, while DI is a concrete implementation of IoC, which is the behavior of dynamically injecting certain dependencies into objects.

3. What is the core function of Spring?

Answer: The core part of Spring is the IoC container. The core function of learning Spring is to learn how to store objects in Spring and then get objects from Spring.


Guess you like

Origin blog.csdn.net/m0_64338546/article/details/131582772