Spring~ What is Spring? The core function of Spring? What is IoC? What is DI? What's the difference?

We have learned before and have a Servletgeneral understanding of the development process of server-side code. Servlet can be understood as a framework, but Servlet is inconvenient to use. Every time we need to configure Tomcat by ourselves, we cannot quickly introduce dependencies and add jar packages. etc., so now the mainstream frameworks do not use Sevlet, but use Spring.

What is Spring?

Spring refers to Spring Framework(Spring Framework), which is an open source framework with a very active and large community support. Spring supports a wide range of application scenarios and can make Java enterprise-level development easier.

Summarize Spring in one sentence: Spring is an IoC container that includes many tool methods.

There are many modules in the Spring framework, such as Spring JDBC, Spring MVC, Spring Security, Spring AOP, Spring ORM, Spring Test and other modules. By using these modules, development efficiency can be greatly improved.
Now the most used and relatively new framework is Spring Boot. Spring Boot is the advancement and extension of Spring, and Spring is the basis of Spring Boot.

As mentioned above, Spring is a container, so what is a container?

What is a container?

A container is a basic device used to hold something.
We have been exposed to some containers before, such as
List/Map- data storage container -
Tomcatweb container

The Spring we are learning here is also a container, and it is a IoCcontainer

What is IoC?

IoC= inversion of Control(Inversion of Control), this is a design idea, not a specific technology. Inversion of control is to hand over the objects we designed to the container instead of our direct control. This container for controlling and managing objects is also known as an IoC container.

For example, if we create a program to build a "car", we need to have a Car class, then create a body Framework in the Car class, create a chassis Bottom in the chassis, and create a tire class in the chassis. Under normal circumstances, we all have this design idea, calling between classes.

public class NewCarExample {
    
    
    public static void main(String[] args) {
    
    
        Car car = new Car();
        car.init();
   }
    /**
     * 汽车对象
     */
    static class Car {
    
    
        public void init() {
    
    
            // 依赖车身
            Framework framework = new Framework();
            framework.init();
       }
   }
    /**
     * 车身类
     */
    static class Framework {
    
    
        public void init() {
    
    
            // 依赖底盘
            Bottom bottom = new Bottom();
            bottom.init();
       }
   }
    /**
     * 底盘类
     */
    static class Bottom {
    
    
        public void init() {
    
    
            // 依赖轮胎
            Tire tire = new Tire();
            tire.init();
       }
   }
    /**
     * 轮胎类
     */
    static class Tire {
    
    
// 尺寸
        private int size = 30;
        public void init() {
    
    
            System.out.println("轮胎尺寸:" + size);
       }
   }
}

The disadvantage of this design is that if we want to obtain tires of different sizes, we need to modify not only the underlying tire class, but also the classes on the entire call chain, which is very inconvenient. In order to avoid this situation, we adopt the design idea of ​​IoC, and do not create the class that needs to be used in the class, but inject it directly.

public class IocCarExample {
    
    
    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.run();
   }
    static class Car {
    
    
        private Framework framework;
        public Car(Framework framework) {
    
    
            this.framework = framework;
       }
        public void run() {
    
    
            framework.init();
       }
   }
    static class Framework {
    
    
        private Bottom bottom;
        public Framework(Bottom bottom) {
    
    
            this.bottom = bottom;
       }
        public void init() {
    
    
            bottom.init();
       }
   }
    static class Bottom {
    
    
        private Tire tire;
        public Bottom(Tire tire) {
    
    
            this.tire = tire;
       }
        public void init() {
    
    
            tire.init();
       }
   }
static class Tire {
    
    
        private int size;
        public Tire(int size) {
    
    
            this.size = size;
       }
        public void init() {
    
    
            System.out.println("轮胎:" + size);
       }
   }
}

This is the inversion. We first create the tire class, then inject it into the chassis, the chassis into the body class, and the body into the Car. If the size of the tire changes, we only need to change the underlying tire class, not the Then modify the entire call chain, the idea is IoC.

What is the role of IoC design thinking?

The design idea of ​​IoC can reduce the coupling between codes, that is, the degree of association between codes, and then design loosely coupled and better codes. In traditional programming, we actively create dependent objects in the class, and then use some resources of this object, which leads to high coupling between classes and difficult to test. Under the design idea of ​​IoC, we give the permission to create and manage objects to the container, and the container injects and combines the objects, so the objects and objects, and between classes are loosely coupled, which is convenient for function reuse and convenient. Testing will also make our code structure very flexible.

What is DI?

DI: Dependency Injection, Dependency Injection.
Dependency injection is IoCa specific implementation method and specific implementation technology of design ideas.

That is, a dependency is dynamically injected into a component by the IoC container. For example, class A needs to connect to the database. Usually, we create a Connection object, and dependency injection does not require us to create it. We directly tell the container that class A needs a Connection object, and the container will dynamically Inject this Conection object into class A. It is not up to us to decide when this object is created or destroyed, but by the container itself.

The implementation of DI actually relies on an important feature of Java: reflection. It allows programs to dynamically generate objects, execute methods of objects, and modify properties of objects at runtime.

Dependency injection can improve the efficiency of component operation and build a flexible and extensible platform for the system.

IOC vs DI?

IoC is Inversion of Control, which is a design idea, and DI is Dependency Injection, which is the implementation of Inversion of Control.

The core function of Spring?

The core function of Spring is to manage objects, dynamically inject objects into the Spring framework, and then take them out.

Guess you like

Origin blog.csdn.net/Merciful_Lion/article/details/123870193