Spring的IOC&DI


Spring

What is Spring?

The Spring we usually refer to refers to the Spring Framework (Spring Framework), which is an open source framework.

Summarized in one sentence: Sping is an IoC container that contains many tools and methods

What is IoC

Sping is an IoC container

What are IoCs? IoC = Inversion of Control, translated into Chinese means inversion of control.

In other words, Spring is an inversion of control container

traditional program development

Give an example

For example, let's build a car now.

The order of construction is: car -> body -> chassis -> tires

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

    /**
     * 汽车类
     */
    static class Car {
    
    
        public void run() {
    
    
            Framework frame = new Framework();
            frame.init();
        }
    }
    /**
     * 车身
     */
    static class Framework {
    
    
        public void init() {
    
    
            Chassis chassis = new Chassis();
            chassis.init();
        }
    }
    /**
     * 底盘
     */
    static class Chassis {
    
    
        public void init() {
    
    
            Tire tire = new Tire();
            tire.init();
        }
    }
    /**
     * 轮胎类
     */
    static class Tire {
    
    
        private int size = 20;
        public void init() {
    
    
            System.out.println("车轮大小为"+size);
        }
    }
}

Drawbacks of Traditional Program Development

The size of the wheel is fixed when the above program is developed, and the color of the body cannot be set. With the increasing demands of customers. So you have to set multiple wheel and body colors

  • When we modified the tire class and the chassis class, it was affected
  • We have to modify the chassis class, and after modifying the chassis class, we find that the body class is affected
  • We have to continue to modify the body class, and after modifying the body class, there are new problems
  • The car class is affected and has to be modified
public class NewCarDemo {
    
    
    public static void main(String[] args) {
    
    
        Car car = new Car();
        car.run(20);
    }

    /**
     * 汽车类
     */
    static class Car {
    
    
        public void run(int size) {
    
    
            //依赖车身
            Framework frame = new Framework();
            frame.init(size);
        }
    }
    /**
     * 车身
     */
    static class Framework {
    
    
        public void init(int size) {
    
    
            //依赖底盘
            Chassis chassis = new Chassis();
            chassis.init(size);
        }
    }
    /**
     * 底盘
     */
    static class Chassis {
    
    
        public void init(int size) {
    
    
            //依赖轮胎
            Tire tire = new Tire();
            tire.init(size);
        }
    }
    /**
     * 轮胎类
     */
    static class Tire {
    
    
        public void init(int size) {
    
    
            System.out.println("车轮大小为"+size);
        }
    }
}

As can be seen from the above code, when the bottom-level code is changed, the code of the entire call chain needs to be changed

When the wheels are changed, the chassis needs to be changed

And when the chassis code changes, it is necessary to change the body type

What if a color attribute is added to the car body? , the body category is changed to the car category and then changed

This phenomenon of traditional development is called code coupling problem

Then you can use IoC to achieve decoupling

Inversion of Control Program Development

public class IoCNewCarDemo {
    
    
    public static void main(String[] args) {
    
    
        //构造轮胎
        Tire tire = new Tire(30);
        //给底盘提供轮胎
        Chassis chassis = new Chassis(tire);
        //给车身提供底盘
        Framework framework = new Framework(chassis);
        //最后组成汽车
        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 Chassis chassis;
        public Framework(Chassis chassis) {
    
    
            this.chassis = chassis;
        }

        public void init() {
    
    
            chassis.init();
        }
    }
    /**
     * 底盘类
     */
    static class Chassis {
    
    
        private Tire tire;
        public Chassis(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("车轮大小为 "+this.size);
        }
    }
}

The order in which IoC creates classes: from small to large

Tires->Chassis->Body->Car

  • The order in which classes are created is changed (reversed)
  • After the underlying calling class is changed, the code on the entire calling chain of the IoC mode does not need to be modified to realize the decoupling of the class and the referenced class, but the traditional development method is not acceptable
  • If you need to use dependencies before, you need to create and manage the life cycle of the class yourself, IoC does not need to manage the life cycle of the object yourself

summary

insert image description here

The order in which traditional development creates a car is

Car -> Body -> Chassis -> Wheels -> Execute run: from big to small

The order in which IoC development creates classes is: from small to large

Tires->Chassis->Body->Car

The implementation code of the general program, the order of class creation is reversed

After the improvement, the inversion of the control right is no longer the upper-level object creating and controlling the lower-level object, but the lower-level object injecting the current object, and the lower-level control right is no longer controlled by the upper-level class, so even if Any changes in the lower class will not affect the current class. This is a typical inversion of control, which is the implementation idea of ​​IoC

DI (Dependency Injection)

A means to dynamically register objects into the container when the IoC container starts

When creating the current class, the process of dynamically injecting (registering) the dependent objects of the current class is called DI (Dependency Injection)

For example, Car is created here, and Car depends on the body class. When creating a car class, its dependent class Framework needs to be dynamically injected into it

/**
     * 车类
     */
    static class Car {
    
    
        private Framework framework;
        public Car(Framework framework) {
    
    
            this.framework = framework;
        }
        public void run() {
    
    
            framework.init();
        }
    }

The difference between IoC and DI

IOC is inversion of control, it is a design idea; DI is the means of realization

For example, I’m in a better mood now, I mean I’m going to have a supper in the evening, this is the thought

What are you having for dinner? Eating hot pot is the means of realization

IoC can achieve decoupling between dependent classes through inversion of control, so that we don't care about the specific implementation and life cycle of dependent classes. We only need to inject dependent classes into them when using dependent classes. It happens in the underlying dependent classes Don't care about its implementation when changing.

IoC is to hand over the objects we need to create ourselves to other people. We don’t need to care about the details of object creation, just take them and use them when we need them.

DI is a specific implementation method of IoC, which refers to an implementation mechanism that dynamically injects objects into the current class when the program is running.

If the current class needs to introduce dependent classes, the dependent class can be dynamically injected into the current class, so that the current class can refer to the dependent class

IoC is the guiding ideology, DI is the means of realization

Summarize

What is Spring? How to understand Spring?

Spring is an IoC container that contains many tools and methods. The rights to create and destroy objects are all managed by Spring. It also has the ability to store objects and obtain objects.

The core function of learning Spring is to learn how to store objects in Spring and then get objects from
Spring

The benefits of storing objects in containers: Storing objects in IoC containers is equivalent to making all the tools that may be used in the future and putting them in
the warehouse when you need them, and put them back after use to the warehouse. The method of new object is equivalent to that every time a tool is needed
, it is made now, and it will not be saved after being thrown away after use, and it has to be redone when it is used next time. This is the difference between IoC container and ordinary program development.
hair difference.

The difference between IoC and DI

IoC is the guiding ideology, DI is the means of realization

What is the core function of Spring?

management object

Dynamically register the object in Spring, and then take the object out of Spring


Guess you like

Origin blog.csdn.net/weixin_53946852/article/details/129528671