Spring 入門

Water_tu發表於2020-12-22

環境搭建

這一章通過具體的專案,加深對 IOC(控制反轉)和 DI(依賴注入)的理解。
完整專案地址:spring_study

  • 匯入依賴
    這裡匯入 spring-webmvc 就行,會自動載入其它 aop、beans、core、context等依賴。

    <dependencies>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>5.2.5.RELEASE</version>
          </dependency>
    
          <!--進行單元測試-->
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.12</version>
          </dependency>
      </dependencies>
  • 專案結構

    tQn7BASvXa.png!large
  • 建立兩個實體類 Adderss,Student 便於之後演示,這裡只展示具體屬性。

    public class Student {
      private String name;
      private Address address;
      private String[] books;
      private List<String> hobbies;
      private Map<String,String> card;
      private Set<String> games;
      private String wife;
      private Properties info;
    }
    public class Address {
    private String address;
    }

    到這裡,其實和平時專案建立沒區別,在沒有使用 Spring 時,我們建立物件是自己建立的,如測試類程式碼所示:

    @Test
    public void test() {
      Student student = new Student();
      student.setName("hudu");
    }

    可以發現這是我們一般建立物件的過程,自己建立,並且給物件屬性賦值,下面使用Spring之後就可明顯感覺到什麼是 IOC 和 DI 了。

  • 初步配置 applicationContext.xml

    <?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
          https://www.springframework.org/schema/beans/spring-beans.xsd">
      <!--
      id : bean 的唯一識別符號,也就是相當於物件名
      class : bean 物件所對應的全限定名:包名+類名
      name : 類中的屬性名
      value : 注入的屬性的值
      -->
      <bean id="student" class="com.hudu.pojo.Student">
          <property name="name" value="hudu"/>
      </bean>
    

``` 上面對 applicationContext.xml 檔案進行了簡單的初步配置,此時再次獲取Student物件和給物件屬性賦值已經發生了變化。

@Test
    public void test() {
        //獲取ApplicationContext:拿到Spring的容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.getName());
    }

可以看出,我們的物件都在 Spring 中管理了,我們要使用,直接去裡面取出來就行,這個其實就是 IOC 和 DI 的實現。下面進行進一步的講解,IOC 建立物件的幾種方法,總共三種。

  • 上面已經講了第一種,直接通過引數名設定,即

    <bean id="student" class="com.hudu.pojo.Student">
          <property name="name" value="hudu"/>
    </bean>
  • 通過有參構造,下標賦值

    <bean id="address" class="com.hudu.pojo.Address">
      <constructor-arg index="0" value="The Earth"/>
    </bean>
  • 通過有參構造,通過型別建立,不建議使用

    <bean id="address" class="com.hudu.pojo.Address">
      <constructor-arg type="java.lang.String" value="The Earth"/>
    </bean>
  • 下面講實體類各種型別的屬性如何進行依賴注入,可以看到學生有很多屬性,型別有 String、Map、Set等

    <bean id="address" class="com.hudu.pojo.Address">
          <property name="address" value="The Earth"/>
      </bean>
    
      <!--
      id : bean 的唯一識別符號,也就是相當於物件名
      class : bean 物件所對應的全限定名:包名+類名
      name : 類中的屬性名
      value : 注入的屬性的值
      -->
      <bean id="student" class="com.hudu.pojo.Student">
          <!--第一種,普通值注入,value-->
          <property name="name" value="hudu"/>
          <!--第二種,Bean注入,ref-->
          <property name="address" ref="address"/>
          <!--陣列注入-->
          <property name="books">
              <array>
                  <value>黑客與畫家</value>
                  <value>從0到1</value>
                  <value>演算法導論</value>
                  <value>深度學習</value>
              </array>
          </property>
          <!--List-->
          <property name="hobbies">
              <list>
                  <value>聽歌</value>
                  <value>程式碼</value>
                  <value>電影</value>
              </list>
          </property>
          <!--map-->
          <property name="card">
              <map>
                  <entry key="身份證" value="2843198311239084"/>
                  <entry key="銀行卡" value="3857928476381990"/>
              </map>
          </property>
          <!--set-->
          <property name="games">
              <set>
                  <value>LOL</value>
                  <value>COC</value>
                  <value>BOB</value>
              </set>
          </property>
          <!--null-->
          <property name="wife">
              <null/>
          </property>
          <!--properties-->
          <property name="info">
              <props>
                  <prop key="driver">com.mysql.cj.jdbc.Driver</prop>
                  <prop key="url">jdbc//localhost:3306/test</prop>
                  <prop key="username">root</prop>
                  <prop key="password">123456</prop>
              </props>
          </property>
    </bean>

如果配置了多個 xml 檔案,比如還配置了 beans.xml,可以講兩個xml檔案進行整合

<import resource="beans.xml"/>
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章