IOC underlying principle-Spring framework

1. IOC concept

Inversion of Control (IOC) is a design principle in object-oriented programming that can be used to reduce the coupling between computer codes. The most common method is called Dependency Injection (DI for short), and another method is called Dependency Lookup. Through inversion of control, when an object is created, an external entity that regulates all objects in the system passes the references of the objects it depends on to it. It can also be said that the dependency is injected into the object.

(1) Inversion of control, the process of object creation and invocation between objects is handed over to Spring for management

(2) Purpose of using IOC: to reduce coupling

(3) Doing an introductory case is the realization of IOC

2. The underlying principle of IOC

  • The underlying principle is implemented by three parts including: xml parsing, factory mode, reflection.
    Program design requires high cohesion and low coupling
    (1) original method call-coupling is too high
    Insert picture description here
    (2) factory mode-reducing user and UserService The degree of coupling between, but there is still a certain degree of coupling
    Insert picture description here

(3) In order to further reduce the degree of coupling-IOC thinking

The first step is the XML configuration file, and the second step has service and dao classes to create a factory class.
Insert picture description here

3. IOC interface

1. The idea of ​​IOC is based on the IOC container, and the bottom layer of the IOC container is the object factory

2. Spring provides two ways of IOC container implementation-2 interfaces

(1) BeanFactory interface

BeanFactory is the basic implementation of the IOC container. It is an internal interface used by Spring. It is not provided for developers to use. * Objects are not created when the configuration file is loaded. Objects are created only when they are obtained (used). So one is generally not used.

/*一个例子*/
public class testSpring {
    
    
    //新建测试单元
    @Test
    public void testUser(){
    
    
        //1.加载Spring配置文件
        //ApplicationContext context=
        BeanFactory context=
                new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取创建对象
        User user=context.getBean("user", User.class);
        //3.执行方法
        System.out.println(user);
        user.HelloSpring();
    }
}

(2) ApplicationContext interface

The sub-interface of the BeanFactory interface provides more and more powerful functions, and is generally used by developers. * When loading the configuration file, the configuration file object will be created

/*一个例子*/
public class testSpring {
    
    
    //新建测试单元
    @Test
    public void testUser(){
    
    
        //1.加载Spring配置文件
        ApplicationContext context=
                new ClassPathXmlApplicationContext("bean1.xml");
        //2.获取创建对象
        User user=context.getBean("user", User.class);
        //3.执行方法
        System.out.println(user);
        user.HelloSpring();
    }
}

(3) The difference between the two:

BeanFactory creates objects only when it is used, while ApplicationContext can create objects when it is loaded. In actual projects, this time-consuming and labor-intensive process is generally loaded when the server starts, so ApplicationContext is generally used to implement IOC.

3. ApplicationContext interface implementation class and BeanFactory interface implementation class

Ctrl+H view implementation class

  • It mainly includes two classes: ClassPathXmlApplicationContext and FileSystemXmlApplicationContext
  • ClassPathXmlApplicationContext is used to obtain the context object, the parameter is the file path name in the Src directory
  • FileSystemXmlApplicationContext is used to obtain the context object, the parameter is the path of the actual directory, starting from the drive letter

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44307065/article/details/107254146