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


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

1、創建項目

首先創建一個名稱為Hello_Spring的Maven項目。

image

image

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,如下圖:

image

3、創建Spring配置文件

在src/mian/resources目錄下創建一個applicationContext.xml文件。

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

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

image

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、項目整體目錄結構

以上全部創建完成后,項目的整體目錄結構如下圖:

image

9、運行測試

運行的結果會打印兩遍Hello Spring,第一步是傳統 new對象的方式,第二種是使用Spring IOC的方式,結果如下圖:

image

可以發現兩種方式都創建了HelloSpring的對象實例,但是Spring IOC方式更加方便,而且低耦合,這樣更有利於后期的維護。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM