Spring from entry to soil-IOC overview and derivation

In the first article, we learned about what Spring is and through the guidance of the official website, we wrote and started the first Spring program that belongs to us. Although the program is started, we are still not very clear about the content and how to implement it. Then we will first understand a core concept in Spring-IOC

IoC (Control Inversion) Derivation

Previous code architecture

  • Usually when we implement the project before, we will be divided into these steps:

    • First write a UserDao interface
public interface UserDao {
   public void getUser();
}
    • Then write a Dao implementation class
public class UserDaoImpl implements UserDao {
   @Override
   public void getUser() {
       System.out.println("get");
  }
}
    • Then write the interface of UserService
public interface UserService {
   public void getUser();
}
    • Finally, write the implementation class of Service (combination)
public class UserServiceImpl implements UserService {
   private UserDao userDao = new UserDaoImpl();

   @Override
   public void getUser() {
       userDao.getUser();
  }
}
    • Finally use it (test).
@Test
public void test(){
   UserService service = new UserServiceImpl();
   service.getUser();
}

The above is the overall logic of our previous implementation of a function, which is also very clear, but there is a problem, that is, the programmer controls the generation of the code. For every change in demand, we need to change our corresponding code. Start the whole body, and the coupling of the code is too high.

IOC philosophy

In order to solve the frequent code changes due to changes in requirements, we do not implement it where we need it, but use set to provide an interface. In this way, we give the caller the initiative to create the object to control the flip. The program does not need to worry about how to create and implement, only need to provide an interface. Let the caller decide,

public class UserServiceImpl implements UserService {
    private UserDao userDao;
    // 利用set来进行动态实现值的注入
    public void setUserDao(UserDao userDao){
        this.UserDao=userDao;
    }
}

The essence of IoC

Inversion of Control (IoC) is a design idea, and DI (Dependency Injection) is a method of implementing IoC.

  • Ioc is the core content of the Spring framework. IoC is perfectly implemented in a variety of ways. You can use XML configuration or annotations. The new version of Spring can also implement IoC with zero configuration.
  • The Spring container first reads the configuration file when it is initialized, creates the organization object according to the configuration file or metadata and stores it in the container, and then takes out the required objects from the IoC container when the program is used.
    image

​ When configuring the Bean in XML mode, the definition information of the Bean is separated from the implementation, and the two can be integrated in the way of annotations. The definition information of the Bean is directly defined in the implementation class in the form of annotations, so as to achieve The purpose of zero configuration.

  • Inversion of control is a way to produce or obtain specific objects through description (XML or annotation) and through a third party. It is the IoC container that implements the inversion of control in Spring, and its implementation method is Dependency Injection (DI)

At last

  • If you feel that it is rewarding after reading, I hope to give me a thumbs up, this will be my biggest motivation for updating, thank you for your support
  • Welcome everyone to pay attention to my public account [Java Fox], focus on the basic knowledge of java and computer, and ensure that you will get something after reading it. If you don’t believe it, hit me
  • If you have different opinions or suggestions after reading, welcome to comment and share. Thank you for your support and love.

image

Guess you like

Origin blog.csdn.net/issunmingzhi/article/details/109747006