1. 程式人生 > 實用技巧 >Spring5詳解(二)——Spring的入門案例HelloSpring

Spring5詳解(二)——Spring的入門案例HelloSpring

在上一章內容中,詳細的介紹了什麼是Spring,Spring的歷史與發展和Spring的一些特點。所以這一章我們來建立一個Spring的入門案例HelloSpring。

1、建立專案

首先我們建立一個名稱為Hello_Spring的Maven專案。

2、匯入依賴

然後在pom.xml中匯入spring依賴,我們暫時只匯入一個,如下:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

因為這個依賴會自動關聯很多jar,如下圖:

3、建立Spring配置檔案

在src/mian/resources目錄下建立一個applicationContext.xml檔案。

【右擊resources—>New—>選擇XML Configuration File—>Spring Config】

注意:前提是要匯入Spring的依賴,否則不會有Spring Config。

4、建立介面HelloSpring

在src/main/java目錄下建立一個HelloSpring介面,並且定義一個sayHello()方法,程式碼如下所示。

package com.thr;

/**
 * @author tanghaorong
 * @desc HelloSpring介面
 */
public interface HelloSpring {

    void sayHello();
}

5、建立介面實現類

實現上面建立的HelloSpring介面,並在方法中編寫一條輸出語句,程式碼如下所示。

package com.thr;

/**
 * @author tanghaorong
 * @desc HelloSpring實現類
 */
public class HelloSpringImpl implements HelloSpring {

    @Override
    public void sayHello() {
        System.out.println("Hello Spring");
    }
}

6、配置applicationContext.xml

接下來配置我們在src/main/resources目錄中建立的applicationContext.xml檔案。

因為這是一個Spring入門的例子,所以用 xml 配置檔案的方式來配置物件例項,我們要建立的物件例項要定義在 xml 的 <bean> 標籤中。

其中<bean>標籤表示配置一個物件例項。<bean>標籤常用的兩個引數 id 和 class ,id表示識別符號(別名),class 表示物件例項類的全限定名

<?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 definitions here -->

    <!--將指定類配置給Spring,讓Spring建立其物件的例項-->
    <!--id:識別符號(別名) class:需要例項化的類路徑-->
    <bean id="helloSpring" class="com.thr.HelloSpringImpl"></bean>

</beans>

這樣HelloSpringImpl的例項物件就由Spring給我們建立了,名稱為helloSpring,當然我們也可以建立多個物件例項,如下:

    <bean id="helloSpring" class="com.thr.HelloSpringImpl"></bean>
    <bean id="helloSpring1" class="com.thr.HelloSpringImpl"></bean>
    <bean id="helloSpring2" class="com.thr.HelloSpringImpl"></bean>

7、配置測試類

在src/test/java下,建立測試類TestHelloSpring,程式碼如下:

package com.thr;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author tanghaorong
 * @desc 測試類
 */
public class TestHelloSpring {
    public static void main(String[] args) {
        //傳統方式:new 物件() 緊密耦合
        HelloSpring helloSpring = new HelloSpringImpl();
        helloSpring.sayHello();


        //Spring方式:XML解析+反射+工廠模式
        //1.初始化Spring容器,載入配置檔案
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.通過容器獲取helloSpring例項,getBean()方法中的引數是bean標籤中的id
        HelloSpring helloSpring1 = (HelloSpring) applicationContext.getBean("helloSpring");
        //3.呼叫例項中的方法
        helloSpring1.sayHello();
    }
}

因為這裡是測試使用,所以需要初始化Spring容器,並且載入配置檔案,實際開發中這一步不需要。

8、專案整體目錄結構

以上全部建立完成後,專案的整體目錄結構如下圖:

9、執行測試

執行的結果會列印兩遍Hello Spring,第一步是傳統 new物件的方式,第二種是使用Spring IOC的方式,結果如下圖:

可以發現兩種方式都建立了HelloSpring的物件例項,但是Spring IOC方式更加方便,而且低耦合,這樣更有利於後期的維護。

這篇文章只是一個簡單的入門案例,所以例子非常簡單,也希望大家多多指點,謝謝!