<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          面試官:為什么要用 Spring?它到底解決了什么問題?

          共 2574字,需瀏覽 6分鐘

           ·

          2022-04-11 18:09

          程序員的成長之路
          互聯(lián)網(wǎng)/程序員/技術/資料共享?
          關注


          閱讀本文大概需要 2.8 分鐘。

          來自:https://blog.csdn.net/qq_38050259/article/details/113414419

          為什么要用 Spring?它到底解決了什么問題?
          目的:對比傳統(tǒng)方式和 Spring注入方式創(chuàng)建對象以達到解耦的目的,以Service層調(diào)用 Dao層為例。

          方式一:傳統(tǒng)方式

          1.Service層

          /**
          ?*?@author :Prannt
          ?*?@description :Service層
          ?*?@program?:?Test
          ?*/
          public?class?UserService?{
          ????public?void?add(){
          ????????System.out.println("service?add...");
          ????????UserDao?dao?=?new?UserDaoImpl();
          ????????dao.query();
          ????}
          }

          2.UserDao接口

          /**
          ?*?@description :接口中只有一個抽象方法
          ?*/
          public?interface?UserDao?{
          ????void?query();
          }

          3.UserDao接口的實現(xiàn)類

          /**
          ?*?@description :接口的實現(xiàn)類,重寫 query方法
          ?*/
          public?class?UserDaoImpl?implements?UserDao{
          ????@Override
          ????public?void?query()?{
          ????????System.out.println("dao?query...");
          ????}
          }
          //測試
          public?class?test?{
          ????@Test
          ????public?void?test1(){
          ????????UserService?service?=?new?UserService();
          ????????service.add();
          ????}
          }

          Spring Boot 基礎就不介紹了,推薦下這個實戰(zhàn)教程:https://github.com/javastacks/spring-boot-best-practice
          測試結果:
          結論一:由以上過程可以看出,在UserService類中直接調(diào)用實現(xiàn)類的query方法,一旦實現(xiàn)類出問題,UserService立即報錯,具有高度的耦合性

          方式二:Spring注入對象

          1.xml文件配置bean

          "1.0"?encoding="UTF-8"?>
          "http://www.springframework.org/schema/beans"
          ???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ???????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd">

          ????"userService"?class="Service.UserService">
          ????????
          ????????"dao"?ref="Dao">
          ????

          ????"Dao"?class="Dao.UserDaoImpl">

          2.Service層

          import?Dao.UserDao;
          /**
          ?*?@author :Prannt
          ?*?@description :
          ?*?@program?:?Demo01
          ?*/
          public?class?UserService?{
          ????public?void?add(){
          ????????System.out.println("service?add......");
          ????????dao.update();
          ????}

          ????//創(chuàng)建dao類型的屬性,生成對應的set方法
          ????private?UserDao?dao;

          ????public?void?setDao(UserDao?dao)?{
          ????????this.dao?=?dao;
          ????}
          }

          3.UserDao接口

          public?interface?UserDao?{
          ????void?update();
          }

          4.接口的實現(xiàn)類

          public?class?UserDaoImpl?implements?UserDao{
          ????@Override
          ????public?void?update()?{
          ????????System.out.println("Dao?update......");
          ????}
          }
          推薦一個 Spring Boot 基礎教程及實戰(zhàn)示例:https://github.com/javastacks/spring-boot-best-practice
          測試
          import?Service.UserService;
          import?org.junit.Test;
          import?org.springframework.context.ApplicationContext;
          import?org.springframework.context.support.ClassPathXmlApplicationContext;

          public?class?TestBean?{
          ????@Test
          ????public?void?testBean(){
          ????????ApplicationContext?context?=?new?ClassPathXmlApplicationContext("bean2.xml");
          ????????UserService?userService?=?context.getBean("userService",UserService.class);
          ????????userService.add();
          ????}
          }
          測試結果
          結論二:觀察以上過程,在UserService類中,沒有直接new實現(xiàn)類,而是通過將Dao注入外部配置文件中的方式,使用“第三方文件”來達到解耦的目的。

          總結

          第一種傳統(tǒng)方式創(chuàng)建對象,就像圖一中的齒輪組。如果有一個齒輪出了問題,就可能會影響到整個齒輪組的正常運轉(zhuǎn)。
          圖一:高度耦合的齒輪組
          而用Spring注入對象猶如圖二的齒輪,利用第三方xml文件使任意兩個齒輪之間無必然聯(lián)系,B齒輪壞了也影響不到A、C、D齒輪。齒輪組相互之間的依賴關系降到最低。
          圖二:解耦后的齒輪組

          推薦閱讀:

          在外企工作真爽嗎?

          超贊 ! 老外的一種避免遞歸查詢所有子部門的樹數(shù)據(jù)表設計與實現(xiàn)!

          互聯(lián)網(wǎng)初中高級大廠面試題(9個G)

          內(nèi)容包含Java基礎、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊列、高性能緩存、反射、Spring全家桶原理、微服務、Zookeeper、數(shù)據(jù)結構、限流熔斷降級......等技術棧!

          ?戳閱讀原文領取!? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??朕已閱?

          瀏覽 55
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久免费黄片视频 | а中文在线天堂 | 久久国产精品国语对白 | 麻豆成人视频在线观看 | 一级黄色电影在线免费观看 |