[Spring] Initial Guide

1. Spring initial

1. Why learn the framework

The learning framework is equivalent to an upgrade from a "small workshop" to a "factory". The small workshop has to do everything, and the factory is a component assembly, which is characterized by high efficiency.

2. The advantages of the framework (SpringBoot Vs Servlet)

Using the SpringBoot project demonstration framework has the following advantages over Servlets:

  1. No need to configure Tomcat, click the "Run" button to run the project, SpringBoot has a built-in web container (can be run directly)
  2. Quickly add jar package
  3. Quick release project (you can release it using java-jar method)
  4. Object Autowiring

2. Spring foundation and core concepts

1. What is Spring?

Spring refers to Spring Framwork (Spring framework). In a word, Spring is an IoC container that contains many tools and methods.

1.1 What is a container

A container is a device used to hold something.

  • List/Map -> data storage container
  • Tomcat -> Web container (store many Servlet programs)

1.2 What is IoC

IoC = Inversion of Control (Inversion of Control), which means that Spring is an "inversion of control" container, which sounds abstract, and the following example can explain it well.

Traditional program development:

If we develop a car program, the general implementation idea is this.

Building a car (Car) needs to rely on the frame (Framworl), the frame needs to rely on the chassis (Bottom), and the chassis needs to rely on tires (Tire). The code is roughly as follows:

public class Car {
    
    
    public void init(){
    
    
        Framework framework = new Framework();
        System.out.println("do car");
        framework.init();
    }
    public static void main(String[] args) {
    
    
        Car car = new Car();
        car.init();
    }
}
public class Framework {
    
    
    public void init(){
    
    
        Bottom bottom = new Bottom();
        System.out.println("do framework");
        bottom.init();
    }
}

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

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

If our system is upgraded, customers can choose the wheel size, and then we will find a big defect. When the bottom layer changes, the entire call chain needs to be modified, indicating that the code coupling is too strong.

public class Tire {
    
    
    private int size = 17;
    public Tire(int size){
    
    
        this.size = size;
    }
    public void init(){
    
    
        System.out.println("size ->>" + size);
    }
}
public class Bottom {
    
    
    private Tire tire;
    public Bottom(int size){
    
    
        tire = new Tire(size);
    }
    public void init(){
    
    
        System.out.println("do bottom");
        tire.init();
    }
}
public class Framework {
    
    
    private Bottom bottom;
    public Framework(int size){
    
    
        bottom = new Bottom(size);
    }
    public void init(){
    
    
        System.out.println("do framework");
        bottom.init();
    }
}
public class Car {
    
    
    private Framework framework;
    public Car(int size){
    
    
        framework = new Framework(size);
    }
    public void init(){
    
    
        System.out.println("do car");
        framework.init();
    }
    public static void main(String[] args) {
    
    
        Car car = new Car(25);
        car.init();
    }
}

Then, with the iterative upgrade of the system, people have more needs, and the bottom layer is ever-changing. We will find that it takes a lot of time to modify the entire call chain, so decoupling is required.

Addressing deficiencies in traditional development

The main defect of traditional development is that a module depends on another module and also controls its life cycle (new subordinate). Therefore, when the downstream module changes, the upstream must also change accordingly. The way we solve this problem is to convert the original Create the lower-level class by yourself, and change it to the way of delivery (that is, the way of injection), so that when the lower-level class changes, the current class does not need to modify any code, which solves the decoupling of the program.

Decoupling refers to solving the coupling of code. Coupling can also be called program dependency in another way. The coupling (correlation between codes) of good code programs is very low.

This is like when building a car, if all the accessories are made by ourselves, then when the customer's needs change, such as modifying the size, we need to do it ourselves. If we take the tires out, then even the tires If the size changes, we only need to place an order with the agent factory.

Inversion of Control Program Development

Based on the above ideas, we modify the program example of calling a car, and change the way of creating a subclass to calling a subclass. The specific implementation code is as follows:

public class Test {
    
    
    //交给 Test 这个第三方类来控制
    public static void main(String[] args) {
    
    
        Tire tire = new Tire(20, "白色");
        Bottom bottom = new Bottom(tire);
        Framework framework = new Framework(bottom);
        Car car = new Car(framework);
        car.init();
    }
}
public class Tire {
    
    
    private int size; //客户不再满足与17尺寸
    private String color;//客户新增颜色需求
//    新增 尺寸 和 颜色需求

    public Tire(int size, String color) {
    
    
        this.size = size;
        this.color = color;
    }

    public void init() {
    
    
        System.out.println("size ->>" + size + " color ->>" + color);
    }
}
public class Bottom {
    
    
    private Tire tire;

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

    public Framework(Bottom bottom){
    
    
        this.bottom = bottom;
    }

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

    public Car(Framework framework){
    
    //不再控制 Framework 的生命周期, 只使用并不关心谁创建(解耦).
        this.framework = framework;
    }
    public void init(){
    
    
        System.out.println("do cat...");
        framework.init();
    }
}

1.3 Understanding Spring IoC

Spring IoC core operations:
  1. store the object in the container
  2. remove the object from the container

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

The benefits of putting objects into containers: Storing objects in IoC containers is equivalent to putting all the tools that may be used in the future into the warehouse. When you need them, you can directly fetch them, and put them back into the warehouse after use. And the new object The method is equivalent to, every time you need a tool, you have to do it yourself (new object), and discard it directly after use (destroy the object), and you have to rewrite it when you use it next time. This is IoC It is different from ordinary program development.

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 obtain objects.

Advantages of Spring IoC:
  1. decoupling
  2. It is more convenient to use (no need to manually create and pay attention to the dependencies behind this class object)
  3. More efficient (similar to the difference between using threads and thread pools)

If A depends on B, and B depends on CDE, if we use the traditional development method, we not only need to manually implement BCDE, but also the downstream subclass changes, and the upstream parent class also changes accordingly. But if we use Spring IoC , there is no need to manually implement dependencies, and there is no need to pay attention to subsequent dependent methods.


1.4 DI concept description

When it comes to IoC, we have to mention another word "DI". DI is the abbreviation of Dependency Injection, which is "Dependency Injection".

The so-called dependency injection is the behavior ( active ) of dynamically injecting a dependent object into the current class during the running of the IoC container . Therefore, dependency injection (DI) and inversion of control (IoC) describe the same object from different perspectives. The thing is to introduce the IoC container and use the dependency injection method to achieve decoupling between objects.

IoC is an idea, and DI is a specific implementation technology, that is, the IoC idea is realized through DI (Dependency Injection).

For example: students will ask the teacher a lot of questions every day, and the teacher passively accepts the questions is the traditional development model. If the teacher has the initiative and supervises the students every day, this is equivalent to dependency injection.

Guess you like

Origin blog.csdn.net/liu_xuixui/article/details/130420409