Understanding of Spring control inversion - understanding of IOC

Let's give a small example to illustrate what is control inversion IOC?

In the initial stage, we write code as follows:

  1. Create a new UserDao and its implementation class
public interface UserDao {
    void save();
}
public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("UserDao.save");
    }
}
  1. Create a new UserService interface and its implementation class, and call UserDao to implement the corresponding business
public interface UserService {
    void addUser();
}
public class UserServiceImpl implements UserService {
    //At the initial stage of the code, we are all directly new, a UserDao implementation class, and then call it.
    private UserDao userDao = new UserDaoImpl();

    public void addUser() {
        System.out.println("UserServiceImpl.addUser");
        userDao.save();
    }
}

There will be a serious disadvantage when we write code like this in the initial stage!!!

  • The coupling between Service layer and Dao layer is too high

  • If we are adding other UserDao implementation classes, such as MysqlUserDaoImpl stored in MySQL or UserDao implementation class stored in Oracle, we need to change the code. This is obviously impractical. We can't change a UserDao implementation class and change the code once

  • What is coupling? That is, we directly new a specific UserDao implementation class in UserServiceImpl. At this time, the Service layer is associated with the specific UserDao. If the UserDao is discarded, the whole code cannot continue to run and the code needs to be changed again;

Next, we add a MySQL userdaoimpl:

public class MySqlUserDaoImpl implements UserDao {
    public void save() {
        System.out.println("MySqlUserDaoImpl.save: use mysql Implement database storage");
    }
}
  • In this way, we need to change our Service layer code and create a new implementation class
public class UserServiceImpl implements UserService {
    private UserDao userDao = new MySqlUserDaoImpl();

    public void addUser() {
        System.out.println("UserServiceImpl.addUser");
        userDao.save();
    }
}

Note: focus!!

  • At this time, we give the right to create userdao to the program. When running, the program creates userdao without the control of our programmers (that is, we create whatever userdao we want)

Next, we provide an interface for UserService to enable programmers to control the creation of UserDao:

public class UserServiceImpl implements UserService {
    private UserDao userDao;

    //By adding a set method, the user can pass in a userdao and assign it to userdao
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void addUser() {
        System.out.println("UserServiceImpl.addUser");
        userDao.save();
    }
}

test

@Test
public void test1() {
    UserService userService = new UserServiceImpl();
    userService.setUserDao(new MySqlUserDaoImpl());
    userService.addUser();
}

You may also feel that the specific UserDao is created by the program. Indeed, the above code is still controlled by the program

In this way, we add a switch judgment to it, which is more obvious

@Test
public void test1() {
    UserService userService = new UserServiceImpl();
    int i = 1;
    switch (i){
        case 0:
            userService.setUserDao(new UserDaoImpl());
            break;
        case 1:
            userService.setUserDao(new MySqlUserDaoImpl());
            break;
    }
    userService.addUser();
}

At this time, we can control the creation of objects through keyboard input

In this way, we can create specific UserDao implementation classes by programmers (users)

  • In this way, the process of giving the right to create an object to the user instead of the program is called inversion of control (IOC);

  • The key to control inversion is that we do not directly instantiate specific objects in the code, but dynamically generate them by the user during operation; Provide interfaces for external transmission of parameters; It can also be realized by dynamic agent

The above is my understanding of control inversion IOC

Keywords: JavaEE Spring ioc

Added by bob2588 on Sat, 01 Jan 2022 17:05:07 +0200