IOC theory derivation of Spring basic learning

preface:

Hello, guys, I'm running snail rz. Of course, you can call me snail Jun. I'm a rookie who has studied Java for more than half a year. At the same time, I also have a great dream, that is, to become an excellent Java Architect one day.
This Spring basic learning series is used to record the whole process of learning the basic knowledge of Spring framework (this series is written with reference to the latest Spring 5 tutorial of crazy God in station B. because it was sorted out before, but it was not released at that time, there may be errors in some places. I hope you can correct them in time!)
After that, I will update this series at a faster rate day by day. If you haven't learned the spring 5 framework, you can learn from my blog; Of course, the little friends who have studied can also review the basics with me.
Finally, I hope I can make progress with you! Come on! Boys!
No more nonsense. Let's start today's learning content. Today we come to the second stop of Spring basic learning: IOC theory derivation!

2.IOC theoretical derivation

2.1 general mode

2.1.1 write UserDao interface and its implementation class

1. Write UserDao interface

package com.kuang.dao;
public interface UserDao {
    void getUser();
}

2. Write UserDaoImpl implementation class

package com.kuang.dao;
public class UserDaoImpl implements UserDao{
    public void getUser() {
        System.out.println("Get user data by default");
    }
}

3. Write UserDaoMysqlImpl implementation class

package com.kuang.dao;
public class UserDaoMysqlImpl implements UserDao{
    public void getUser() {
        System.out.println("Mysql Get user data!");
    }
}

4. Write UserDaoOracleImpl implementation class

package com.kuang.dao;
public class UserDaoOracleImpl implements UserDao {
    public void getUser() {
        System.out.println("Oracle Get user data!");
    }
}

5. Write UserDaoSqlserverImpl implementation class

package com.kuang.dao;
public class UserDaoSqlserverImpl implements UserDao{
    public void getUser() {
        System.out.println("Sqlserver Get user data!");
    }
}

2.1.2 write UserService business interface and its implementation class

1. Write UserService business interface

public interface UserService {
    void getUser();
}

2. Write UserServiceImpl implementation class

public class UserServiceImpl implements UserService{

    //The business layer does one thing: call the DAO layer to query
    //You need to take the initiative to create objects
    private UserDao userDao = new UserDaoImpl();
    //If you need to access multiple objects, you need to create them separately
//     private UserDao userDao1 = new UserDaoMysqlImpl();
//    private UserDao userDao2 = new UserDaoOracleImpl();
//	private UserDao userDao3 = new UserDaoSqlserverImpl();
    public void getUser() {
        userDao.getUser();
    }
}

3. Execution process

General mode:

4. Test conclusion

In our previous business, the needs of users may affect our original code. We need to modify the original code according to the needs of users! If the amount of code is very large, the cost of modifying once is very expensive!

2.2 using control reversal

2.2.1 modify UserServiceImpl implementation class

We use a Set interface implementation, which has undergone revolutionary changes

public class UserServiceImpl implements UserService{
private UserDao userDao; //Introducing userDao object
    //Dynamic value injection using set 
    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
    public void getUser() {
        userDao.getUser();
    }
}

2.2.2 write MyTest test class

package com.kuang.dao;
//test method
public class MyTest {
    public static void main(String[] args) {
        //Users actually call the business layer, dao layer, they do not need to contact!
        UserService userService = new UserServiceImpl();
        //Creating objects actively and accepting objects passively is called inversion of control (IOC)
        ((UserServiceImpl) userService).setUserDao(new UserDaoImpl());
//        ((UserServiceImpl) userService).setUserDao(new UserDaoMysqlImpl());
//      ((UserServiceImpl) userService).setUserDao(new UserDaoOracleImpl());
//        ((UserServiceImpl) userService).setUserDao(new UserDaoSqlserverImpl());
        userService.getUser();
    }
}

2.2.3 execution process

Control reversal:

2.2.4 test results

2.2.5 test conclusion

  • Before, the program was actively creating objects! Control is in the hands of programmers

  • After using set injection, the program is no longer active, but becomes a passive receiving object!

  • This idea essentially solves the problem. Our programmers don't have to manage the creation of objects (give the initiative to users). The coupling of the system is greatly reduced, and we can focus more on the implementation of business! This is the prototype of IOC!

2.3 IOC essence

2.3.1 understanding of IOC and DI concepts

  • Inversion of Control (IOC) is a design idea, and Dependency Injection (DI) is a method to implement IOC (DI can also be understood as another term of IOC). In programs without IOC, object-oriented programming is used. The creation of objects and their dependencies are completely coded in the program. The creation of objects is controlled by the program itself. After the control is reversed, the creation of objects is transferred to a third party. In fact, the so-called control is that the way to obtain dependent objects is reversed.

2.3.2 implementation mode of IOC

  • IOC is the core content of the Spring framework. IOC can be perfectly implemented in many ways, including XML configuration and annotation. The new version of Spring can also implement IOC with zero configuration

2.3.3 IOC implementation process diagram

  • During initialization, the Spring container reads the configuration file first, creates and organizes objects according to the configuration file or metadata, and stores them in the container. When the program is used, it takes out the required objects from the IOC container.

  • When configuring a Bean in XML, the definition information of the Bean is separated from the implementation
  • The two can be integrated by annotation. The definition information of Bean is directly defined in the implementation class in the form of annotation, so as to achieve the purpose of zero configuration.

2.3.4 IOC summary of control reversal

  • Inversion of control is a way to produce or obtain specific objects through description (XML or annotation) and through a third party.
  • In Spring, IOC container is used to implement control inversion, and its method is Dependency Injection (DI)

2.4 using Spring IOC container to create management assembly

2.4.1 create beans.xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
     <bean id="userDaoImpl"
          class="com.kuang.dao.UserDaoImpl">
    </bean>
    <bean id="mysqlImpl"
          class="com.kuang.dao.UserDaoMysqlImpl">
    </bean>
    <bean id="oracleImpl"
          class="com.kuang.dao.UserDaoOracleImpl">
    </bean>
    <bean id="sqlserverImpl"
          class="com.kuang.dao.UserDaoSqlserverImpl">
    </bean>
    <bean id="userServiceImpl" class="com.kuang.service.UserServiceImpl">
        <!--property: Interface implementation class properties
            name: The name of the interface implementation class property
            ref: quote Spring Created object in container
            value: Specific path, basic data type-->
        <property name="userDao" ref="mysqlImpl"/>
    </bean>
</beans>

2.4.2 modify UserServiceImpl business implementation class

public class UserServiceImpl implements UserService{
    private UserDao userDao;
    //Dynamic value injection using set
    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }
    public void getUser() {
        userDao.getUser();
    }
}

2.4.3 repeat the method test

public class MyTest2 {
    public static void main(String[] args) {
        //Get ApplicationContext: get the Spring container
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //The container is in hand. I have it in the world. I can get what I need directly
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
        userServiceImpl.getUser();
    }
}

2.4.4 test results

Well, today's study on IOC theory derivation of Spring basic learning is over. Welcome to actively study and discuss. If you like, you can pay attention to Mr. snail. By the way, we'll see you next time. Bye!

Reference video link: https://www.bilibili.com/video/BV1WE411d7Dv([crazy God says Java] the latest tutorial of spring 5, the IDEA version is easy to understand)

Keywords: Java ioc

Added by phat_hip_prog on Wed, 06 Oct 2021 18:03:16 +0300