<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?它到底解決了什么問題?

          共 2441字,需瀏覽 5分鐘

           ·

          2022-01-06 18:37

          點擊上方藍色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達

          目的:對比傳統(tǒng)方式和 Spring注入方式創(chuàng)建對象以達到解耦的目的,以Service層調(diào)用 Dao層為例

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

          1.Service層

          /**
          ?*?@author :Prannt
          ?*?@description :Service層
          ?*?@program?:?Test
          ?*?@date :2021/1/30 9:16
          ?*/
          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();
          ????}
          }

          測試結(jié)果:

          結(jié)論一:由以上過程可以看出,在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
          ?*?@date :2021/1/30 8:40
          ?*/
          public?class?UserService?{
          ????public?void?add(){
          ????????System.out.println("service?add......");
          ????????dao.update();
          ????}

          ????//創(chuàng)建dao類型的屬性,生成對應(yīng)的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......");
          ????}
          }


          測試

          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();
          ????}
          }


          測試結(jié)果

          結(jié)論二:觀察以上過程,在UserService類中,沒有直接new實現(xiàn)類,而是通過將Dao注入外部配置文件中的方式,使用“第三方文件”來達到解耦的目的。

          總結(jié)論:
          第一種傳統(tǒng)方式創(chuàng)建對象,就像圖一中的齒輪組。如果有一個齒輪出了問題,就可能會影響到整個齒輪組的正常運轉(zhuǎn)。

          圖一:高度耦合的齒輪組


          而用Spring注入對象猶如圖二的齒輪,利用第三方xml文件使任意兩個齒輪之間無必然聯(lián)系,B齒輪壞了也影響不到A、C、D齒輪。齒輪組相互之間的依賴關(guān)系降到最低。

          圖二:解耦后的齒輪組



          作者:Prannt

          來源:csdn.net/qq_38050259/article/details/113414419

          加鋒哥微信:?java1239??
          圍觀鋒哥朋友圈,每天推送Java干貨!

          瀏覽 46
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  欧美自拍另类在线 | 亚洲日韩免费一级无码毛片 | 天堂在线中文 | 人妻日日爽夜夜爽一区二区 | 国产丝袜足交视频 |