Spring core design idea

Table of contents

Foreword:

what is spring

What is IoC

traditional development ideas

IoC development ideas

Spring IoC

What is DI

summary:


Foreword:

    官网中提出:Spring makes programming Java quicker, easier, and safer for everybody. Spring’s focus on speed, simplicity, and productivity has made it the world's most popular Java framework.

    Spring makes programming Java faster, easier, and safer for everyone. Spring's focus on speed, simplicity, and productivity has made it the world's most popular Java framework.

what is spring

    Spring is an IoC container with many methods. How to understand this sentence? What is a container? What is IoC?

    There are also some containers in Java, such as ArrayList, LinkedList, HashMap and some collections. These collections are actually containers. When we use these collection classes, we can directly use some methods provided by the collection. It encapsulates our commonly used operations into collections, and we only need to clarify the methods provided by the collections to use various collections.

    The core operation of the Spring IoC container is to store and retrieve (dynamically inject DI) objects. Such an object is called a bean in Spring . As long as an object needs to be used in multiple places, then this object is a bean.

What is IoC

    IoC: Inversion of Control (Inversion of Control). It's a thought, inversion of control.

Such an idea can reduce the coupling     between modules . The so-called coupling is the correlation between codes. If the coupling is too high, it is possible to modify one code and other dependent codes need to be modified. Reduce coupling, that is, the correlation between codes is also reduced, the difference between modules is more obvious, and development efficiency is improved.

traditional development ideas

    The Car class depends on the Framework class and instantiates this object in its own class. The Framework class relies on the Bottom class and instantiates this object in its own class. The Bottom class relies on the Tire class and instantiates this object in its own class. Tire is the bottom class. If the construction method in this class is changed, then a series of other classes that depend on it need to modify the code.

    If the current class depends on other classes, instantiate this object directly in your own class. Then you control the life cycle of the dependent object yourself. The so-called IoC idea is the inversion of control, that is, when I need to rely on this object, I don't instantiate this object in my own class, and I don't control it. When I want to use this dependent object, tell others, let others pass (inject) it to me, that is, the life cycle of the dependent object is no longer controlled by me, and it is controlled by others (inversion of control) .

    With such a design idea, even if the code in the dependent object is modified, it will not affect the code in the current class. The current class only needs to get the dependent object for use.

IoC development ideas

    It can be clearly seen that the object that the current class depends on is no longer instantiated by myself, but passed by others (inversion of control) , and I only need to use it. The life cycle is no longer controlled by myself, but handed over to others. Compared with traditional development ideas, the coupling of code is reduced. 

    In this mode, even if the dependent object code is modified, it will not affect the code in the current class. The current class only needs to be concerned with the use of the objects it depends on.

    IoC思想模式下依赖对象实例的顺序:Tire --> Bottom --> Framework --> Car。传统模式下依赖对象实例顺序:Car --> Framework --> Bottom --> Tire。明显也可以感受到控制权的反转。

Spring IoC

    Spring 是一个 IoC 容器,说的是对象的创建和销毁的权利都交给Spring来管理了,它本身又具备了存储对象和获取对象的能力。

    IoC控制权反转。当自己类中需要所依赖的类的时候,不在自己类中实例这个类了(自己不控制)。当我需要时,我给别人说,由别人将这个类的实例传递(注入)过来,我直接使用即可。这个时候所依赖类的实例生命周期不在是我自己掌控了,而是交给别人(控制权反转)。这样就可以降低代码之间的耦合性,所依赖的类就算改变也不会影响当前类中的代码。

    Spring容器中的对象默认只需要实例一次,以后使用的时候直接从Spring中获取就可以了。用完后在交给Spring容器。程序效率就得到了提升。

什么是DI

    DI: Dependency Injection。就是依赖注入的意思。IoC是一种思想(控制权反转),Spring IoC是一个容器,包含了存储和获取对象的能力。获取对象就是程序在运行期间,动态的将对象注入到当前类中。

    所谓依赖注入,就是当Spring IoC容器在运行期间,动态的将依赖关系注入到对象之中。IoC和依赖注入是从不同角度描述同一件事。当Spring IoC容器在运行的时候,使用DI技术动态的将目前所依赖的对象注入到当前要使用的位置上。实现对象之间的解耦。

    Spring IoC is an idea, and the container controls the object by itself. When other classes want to use the object dependency in the Spring IoC container, use DI to dynamically inject this object into the target location.         

    IoC is a "goal" and a kind of thought, and the goal and thought are just a kind of guiding principle. In the end, there must be a feasible landing plan, and DI belongs to the specific realization.

summary:

    Spring Ioc is an idea, and DI is a technology. Use the Spring Ioc container to dynamically inject objects into the target location during program running, which is DI.

Guess you like

Origin blog.csdn.net/weixin_62353436/article/details/130062016