IOC theory derivation of Spring learning

This article is a note of watching the Java video of ecstasy. Video link: https://space.bilibili.com/95256449

Using IDEA for the first time, recording the use process and IOC theoretical derivation process, is the summary and review of today's study.

Before the introduction of IOC, to develop MVC, the general mode is:

  • UserDao interface
  • UserDaoImpl implementation class
  • UserService business interface
  • UserServiceImpl business implementation class

According to the traditional programming, this mode is implemented one by one. The process is as follows:

1, IDEA create project

1. Create a project: "Create New Project" - > click "Maven" - > next on the left, and name "GroupId" (such as com.kuang), "ArtifactId" (such as spring study) - > next, and change the project name to "spring study", that is, add "-".

2. Import jar package: import the package required by Spring, click "pom.xml", and add the following code:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>
 </dependencies>

3. In order to create your own subproject, you can delete the src of spring study

4. Create Moudle: new -- > Moudle "- > artifactid is named:" spring-01-ioc1 "- > next, moubld name is named" spring-01-ioc1 "

5. Create a package: spring study - > spring-01-ioc1 - > SRC - > main - > java, create new packages "com.kuang.dao" and "com.kuang.service", create UserDao interface and UserDaoImpl implementation class under package dao, and create UserService business interface and UserServiceImpl business implementation class under package service

2, MVC implementation

The codes of each module are as follows:

  • UserDao interface
package com.kuang.dao;

public interface UserDao {
    void getUser();
}
  • UserDaoImpl implementation class
package com.kuang.dao;

public class UserDaoImpl implements UserDao{
    public void getUser(){
        System.out.println("Get user's data by default");
    }
}
  • UserService business interface
package com.kuang.service;

public interface UserService {
    void getUser();
}
  • UserServiceImpl business implementation class
package com.kuang.service;

import com.kuang.dao.UserDaoImpl;
import com.kuang.dao.UserDaoMysqlImpl;

public class UserServiceImpl implements UserService{
   private UserDao userDao = new UserDaoImpl();

    public void getUser(){
     //The business layer adjusts the DAO layer, using a combination method
        userDao.getUser();
    }

Next, you need to create a test class, spring study - > spring-01-ioc1 - > SRC - > test - > java, to create a "MyTest.java", as follows:

import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;

public class MyTest {
    public static void main(String[] args){
        //Three layer architecture, users call the business layer, and they don't need to touch the DAO layer
        UserServiceImpl userService = new UserServiceImpl();

        userService.getUser();
    }
}

Run MyTest.java, and the console will output "get user data by default", indicating that the operation is successful.

3, IOC thought introduction

If the user needs to change from "get user data by default" to "get user data by Mysql", you need to add a UserDaoMysqlImpl class under the dao package, and change the UserServiceImpl business implementation class to:

package com.kuang.service;

import com.kuang.dao.UserDaoImpl;
import com.kuang.dao.UserDaoMysqlImpl;

public class UserServiceImpl implements UserService{
   private UserDao userDao = new UserDaoMysqlImpl();

    public void getUser(){
     //The business layer tunes the DAO layer, using a combination method
        userDao.getUser();
    }

This brings about a problem: once the user requirements change, the original code needs to be changed. If the code volume is large, the maintenance and development costs are too large, time-consuming and laborious.

In order to solve this problem, it can be changed as follows:

The UserServiceImpl business implementation class changes to:

package com.kuang.service;

import com.kuang.dao.UserDao;
import com.kuang.dao.UserDaoImpl;
import com.kuang.dao.UserDaoMysqlImpl;

public class UserServiceImpl implements UserService{
   // private UserDao userDao = new UserDaoImpl();
   private UserDao userDao;

   //Dynamic value injection using set
   public void setUserDao(UserDao userDao){
       this.userDao = userDao;
   }

    public void getUser(){
     //The business layer tunes the DAO layer, using a combination method
        userDao.getUser();
    }
}

MyTest.java changes to:

import com.kuang.dao.*;
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;

public class MyTest {
    public static void main(String[] args){
        //Three layer architecture, users call the business layer, and they don't need to touch the DAO layer
        UserServiceImpl userService = new UserServiceImpl();

        userService.setUserDao(new UserDaoOracleImpl());
        userService.getUser();
    }
}

By introducing the set method, the control right can be changed from the programmer's hand to the user's hand. Before, the program actively creates the object, and after the change, the program becomes a passive object. This is the IOC prototype, which essentially solves the above problems, greatly reduces the coupling of the system, and enables programmers to focus on business implementation.  

 

 

 

 

Published 32 original articles, won praise 4, visited 5709
Private letter follow

Keywords: Spring Java Programming Maven

Added by sheila on Fri, 10 Jan 2020 20:02:35 +0200