Spring IoC [Inversion of Control] DI [Dependency Injection]

insert image description here


IoC is the abbreviation of Inversion of Control, translated as "inversion of control", it is not a technology, but a design idea, is an important object-oriented programming principle, can guide us how to design loosely coupled, better program

Spring manages the instantiation and initialization of all Java objects through the IoC container, and controls the dependencies between objects. We call the Java objects IoC 容器managed by Spring Bean, Spring Beans no different from Java objects created with the new keyword.

The IoC container is one of the most important core components in the Spring framework, which runs through the entire process of Spring from its birth to its growth.

Inversion of Control (IoC)

insert image description here

In traditional Java applications, if a class wants to call a property or method in another class, it usually first creates the latter object in its code by means of new Object(), and then the property or method can be called. . That is to say, the traditional development method, the control of object creation is handed over to the programmer

In Spring applications, the control of Java object creation is given to the container, which is控制反转

1. Developers define Java objects through XML configuration files, annotations, Java configuration classes, etc., such as using tags in XML configuration files, using @Component annotations on Java classes, etc.
2. When Spring starts, the IoC container will automatically create and manage these objects according to the object definition. These objects created and managed by the IoC container are called Spring Beans.
3. When we want to use a Bean, we can get it directly from the IoC container (for example, through the getBean() method of ApplicationContext), instead of newmanually creating an object

The biggest change brought by IoC is not at the code level, but at the ideological level. In Spring applications, the IoC container holds the initiative, and the caller becomes the passive party, passively waiting for the IoC container to create the objects (Beans) it needs.

In this process, the inversion of control occurs, and the creation of the object that was originally implemented by the caller through the code is reversed to the IoC container to help realize it. Therefore, we call this process Spring's "inversion of control".

Dependency Injection (DI)

Over time, designers found that IoC has many frameworks, so Spring is reluctant to stop there. On the basis of Ioc, the concept of dependency injection is introduced.

Dependency Injection (Denpendency Injection, abbreviated as DI), in object-oriented, there is a relationship called "dependency" between objects and objects. Simply put, a dependency relationship is that one object needs to use another object, that is, there is an attribute in the object, which is an object of another class.
insert image description here

IoC principle and decoupling

In the process of software development, there are more or less certain coupling relationships between various objects in the system, between various modules, between the software system and the hardware system.

If the coupling of a system is too high, it will cause problems that are difficult to maintain, but almost no work can be done by code without coupling at all, because almost all functions require mutual cooperation and interdependence between codes to complete. Therefore, when we design the program, the idea that we uphold is generally to minimize the coupling degree without affecting the function of the system.

The bottom layer of IoC reduces the coupling of code to a minimum through technologies such as factory mode, Java's reflection mechanism, and XML parsing.

Since the basic information of the object and the dependencies between the objects are defined in the configuration file, they are not tightly coupled in the code, so even if the object changes, we only need to modify it in the configuration file, without the need for Modify the Java code, which is the principle of Spring IoC implementation decoupling.

Two Implementations of IoC Containers

The idea of ​​IoC is implemented based on the IoC container. The bottom layer of the IoC container is actually a Bean factory. Spring framework provides us with two different types of IoC containers, they are BeanFactoryandApplicationContext

BeanFactory

BeanFactory is the basic implementation of the IoC container and the simplest IoC container provided by Spring. It provides the most basic functions of the IoC container and is defined by the org.springframework.beans.factory.BeanFactoryinterface .

BeanFactory adopts 懒加载(lazy-load)机制, the container will not create the Java object immediately when loading the configuration file, only when the program obtains (uses) this pair of objects.

ApplicationContext

ApplicationContext is a sub-interface of the BeanFactory interface and is an extension of the BeanFactory. ApplicationContext adds many enterprise-level functions on the basis of BeanFactory, such as AOP (Aspect Oriented Programming), internationalization, transaction support and so on. The ApplicationContext interface has two commonly used implementation classes, ClassPathXmlApplicationContext, which loads the XML configuration file specified in the classpath ClassPath and completes the instantiation of the ApplicationContext. FileSystemXmlApplicationContext, loads the XML configuration file specified in the specified file system path, and completes the instantiation of ApplicationContext

public class TestSpring {
    
    
    public static void main(String[] args) {
    
    
        //启动工厂
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        //获取工厂中创建好的对象,参数:获取工厂中指定对应的唯一标识
        UserDAO userDAO = (UserDAO) context.getBean("userDAO");
        userDAO.save("Hello Spring ClassPathXmlApplicationContext");
        BeanFactory context1 = new ClassPathXmlApplicationContext("spring.xml");
        //FileSystemXmlApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("D://xxx");
        //UserDAO userDAO2 = fileSystemXmlApplicationContext.getBean("userDAO", userDAO.getClass());
        UserDAO userDAO1 = (UserDAO) context1.getBean("userDAO");
        userDAO1.save("hello spring beanFactory");
    }
}

result

userDAO save name = Hello Spring ClassPathXmlApplicationContext
userDAO save name = hello spring beanFactory

insert image description here

Guess you like

Origin blog.csdn.net/m0_53321320/article/details/123529160
Recommended