App下載

談?wù)剬?duì)于Spring中IOC的理解和認(rèn)知

猿友 2021-07-17 15:16:10 瀏覽數(shù) (1927)
反饋

學(xué)習(xí)過(guò) Spring 框架的小伙伴,一定聽(tīng)說(shuō)過(guò) IOC 和 DI 兩個(gè)概念,初學(xué) Spring 的小伙伴可能對(duì)這兩者概念比較模糊。本篇文章將和大家一起分享對(duì)于 Spring 的 IOC 的理解和認(rèn)知。

IOC的推導(dǎo)

1.1、模擬一個(gè)正常查詢信息的業(yè)務(wù)流程:

①mapper層:因?yàn)闆](méi)有連接數(shù)據(jù)庫(kù),這里我們寫一個(gè)mapper的實(shí)現(xiàn)類來(lái)模擬數(shù)據(jù)的查詢

public interface PerMapper {
    void getPerInfo();
}
public class StudentMapperImpl implements PerMapper {

    @Override
    public void getPerInfo() {
        System.out.println("我是一個(gè)學(xué)生");
    }
}

②service層:service的作用是查詢?nèi)说男畔?/p>

public interface PersonService {
    void getPersonInfo();
}
public class PersonServiceImpl implements PersonService {

    private PerMapper studentMapper = new StudentMapperImpl();

    @Override
    public void getPersonInfo() {
        studentMapper.getPerInfo();
    }
}

③contorller層

import service.PersonService;
import service.impl.PersonServiceImpl;

public class IOCTest {
    public static void main(String[] args) {
        PersonService service = new PersonServiceImpl();
        service.getStudentInfo();
    }
}

④執(zhí)行結(jié)果如下:

1.2、多個(gè)種類的查詢

①mapper,增加老師實(shí)現(xiàn)類

public class TeacherMapperImpl implements PerMapper {

    @Override
    public void getPerInfo() {
        System.out.println("我是一個(gè)老師");
    }
}

②這時(shí)候我們同時(shí)查詢教師和學(xué)生的信息該怎么做呢?

public class PersonServiceImpl implements PersonService {

    private PerMapper student = new StudentMapperImpl();
    private PerMapper teacher = new TeacherMapperImpl();

    @Override
    public void getPersonInfo() {
        student.getPerInfo();
        teacher.getPerInfo();
    }
}

③執(zhí)行結(jié)果:

④如果需求再次變更呢?只需要教師的信息怎么做呢?
毋庸置疑:兩種做法,
一種是直接更改PersonServiceImpl的getPersonInfo()

   @Override
    public void getPersonInfo() {
//        student.getPerInfo();
        teacher.getPerInfo();
    }

第二種是擴(kuò)展service層的接口,把老師和學(xué)生分別提供接口來(lái)查詢:

public interface PersonService {
    void getPersonInfo();
    void getPersonInfo1();
}
    public class PersonServiceImpl implements PersonService {

    private PerMapper student = new StudentMapperImpl();
    private PerMapper teacher = new TeacherMapperImpl();

    @Override
    public void getPersonInfo() {
        teacher.getPerInfo();
    }

    @Override
    public void getPersonInfo1() {
        student.getPerInfo();
    }
}

⑤看起來(lái)好像沒(méi)啥問(wèn)題是吧?那假如有一百種人呢?需要改來(lái)改去人都會(huì)瘋掉的。這種做法肯定不合理的!

1.3、優(yōu)化查詢方式

①聰明的孩子可以想到將查詢的對(duì)象剝離出來(lái):添加 set() 方法,對(duì)接口不實(shí)現(xiàn),只進(jìn)行預(yù)留的工作。

public class PersonServiceImpl implements PersonService {

    private PerMapper per;

    public void setPer(PerMapper per) {
        this.per = per;
    }

    @Override
    public void getPersonInfo() {
        per.getPerInfo();
    }

}

②controller層實(shí)現(xiàn)方式:

public class IOCTest {
    public static void main(String[] args) {
        PersonServiceImpl service = new PersonServiceImpl();
        //學(xué)生
        service.setPer(new StudentMapperImpl());
        service.getPersonInfo();
        //老師
        service.setPer(new TeacherMapperImpl());
        service.getPersonInfo();
    }
}

③總結(jié):這種方式就是把主動(dòng)權(quán)交給了調(diào)用者 ,程序不用去管怎么創(chuàng)建,怎么實(shí)現(xiàn)了 .,它只負(fù)責(zé)提供一個(gè)接口 。

我們不再去管理對(duì)象的創(chuàng)建了 , 更多的去關(guān)注業(yè)務(wù)的實(shí)現(xiàn) ,耦合性大大降低 ,這也就是IOC的原型 !

1.4、IOC本質(zhì)

控制反轉(zhuǎn)是一種通過(guò)描述(XML或注解)并通過(guò)第三方去生產(chǎn)或獲取特定對(duì)象的方式。在Spring中實(shí)現(xiàn)控制反轉(zhuǎn)的是IOC容器,其實(shí)現(xiàn)方法是依賴注入(Dependency Injection,DI)。

以上就是關(guān)于Spring之IOC的理解和認(rèn)知的詳細(xì)內(nèi)容,想要了解更多Spring之IOC或DI相關(guān)內(nèi)容請(qǐng)關(guān)注W3Cschool其它相關(guān)文章!


0 人點(diǎn)贊