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

          共 1611字,需瀏覽 4分鐘

           ·

          2022-02-12 20:07

          相關(guān)閱讀:2T架構(gòu)師學(xué)習(xí)資料干貨分享

          為什么要用 Spring?它到底解決了什么問題?

          目的:對(duì)比傳統(tǒng)方式和 Spring注入方式創(chuàng)建對(duì)象以達(dá)到解耦的目的,以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 :接口中只有一個(gè)抽象方法
           */
          public interface UserDao {
              void query();
          }

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

          /**
           * @description :接口的實(shí)現(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)用實(shí)現(xiàn)類的query方法,一旦實(shí)現(xiàn)類出問題,UserService立即報(bào)錯(cuò),具有高度的耦合性

          方式二:Spring注入對(duì)象

          1.xml文件配置bean

          <?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

              <bean id="userService" class="Service.UserService">
                  <!--注入Dao對(duì)象-->
                  <property name="dao" ref="Dao"></property>
              </bean>

              <bean id="Dao" class="Dao.UserDaoImpl"></bean>
          </beans>

          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類型的屬性,生成對(duì)應(yīng)的set方法
              private UserDao dao;

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

          3.UserDao接口

          public interface UserDao {
              void update();
          }

          4.接口的實(shí)現(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實(shí)現(xiàn)類,而是通過將Dao注入外部配置文件中的方式,使用“第三方文件”來達(dá)到解耦的目的。

          總結(jié)

          第一種傳統(tǒng)方式創(chuàng)建對(duì)象,就像圖一中的齒輪組。如果有一個(gè)齒輪出了問題,就可能會(huì)影響到整個(gè)齒輪組的正常運(yùn)轉(zhuǎn)。

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

          圖二:解耦后的齒輪組
          原文鏈接:https://blog.csdn.net/qq_38050259/article/details/113414419




          1、985副教授工資曝光

          2、心態(tài)崩了!稅前2萬4,到手1萬4,年終獎(jiǎng)扣稅方式1月1日起施行~

          3、雷軍做程序員時(shí)寫的博客,很強(qiáng)大!

          4、人臉識(shí)別的時(shí)候,一定要穿上衣服?。?/a>

          5、清華大學(xué):2021 元宇宙研究報(bào)告!

          6、績效被打3.25B,員工將支付寶告上了法院,判了

          瀏覽 32
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  日韩三级视频在线观看 | 一级a看片在线观看 | 久久撸在线观看 | 三级欧美视频麻豆传媒 | 91九色蝌蚪视频 |