1. 程式人生 > 實用技巧 >第一個Spring程式(程式碼篇)

第一個Spring程式(程式碼篇)

前言

上一篇介紹了一些理解和概念,本篇結合程式碼談談,本篇適合小白

程式碼在 github 倉庫

IoC - 第一個 Spring 程式

先來個 Demo 感受一下,程式碼是基於 Maven 構建的,如果不熟悉 maven 可以檢視公眾號 JavaPub 目錄學習。

  • 建立專案

在 Idea 新建 Maven 專案,目錄結構如圖

  • 匯入依賴 pom.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>javapub.rodert</groupId>
<artifactId>firstSpringProject</artifactId>
<version>1.0-SNAPSHOT</version>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>

</dependencies>
</project>
  • 在專案的 src 目錄下建立一個名為 javapub.rodert 的包,然後在該包中建立一個名為 PersonDao 的介面,並在介面中新增一個 add() 方法
packagejavapub.rodert;

/**
*@authorwangshiyurodert
*@date2020/7/220:13
*@description
*/
publicinterfacePersonDao{
publicvoidadd();
}
  • 建立介面實現類 PersonDaoImpl

javapub.rodert 包下建立 PersonDao 的實現類 PersonDaoImpl

packagejavapub.rodert;

/**
*@authorwangshiyurodert
*@date2020/7/220:14
*@description
*/
publicclassPersonDaoImplimplementsPersonDao{
publicvoidadd(){
System.out.println("執行成功!!!");
}
}
  • 建立 Spring 配置檔案

Spring 配置檔案是整合 Spring 的核心

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">


<beanid="personDao"class="javapub.rodert.PersonDaoImpl"/>

</beans>
  • 到現在一個 Spring 程式已經搭建完成,測試一下

新建測試類

packagejavapub.rodert;


importorg.junit.Test;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

/**
*@authorwangshiyurodert
*@date2020/7/220:15
*@description
*/
publicclassPersonDaoTest{

@Test
publicvoidtest1(){
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("ApplicationContext.xml");
PersonDaopersonDao=(PersonDao)applicationContext.getBean("personDao");
personDao.add();
}

}

返回結果:

執行成功!!!

使用 JUnit 測試執行測試方法,執行成功。在程式執行時,物件的建立並不是通過 new 一個類完成的,而是通過 Spring 容器管理實現的。這就是 Spring IoC(控制反轉) 容器思想的工作機制。

Spring 依賴注入的三種方式

依賴注入(Dependency Injection,DI)和控制反轉含義相同,它們是從兩個角度描述的同一個概念。

當某個 Java 例項需要另一個 Java 例項時,傳統的方法是由呼叫者建立被呼叫者的例項(例如,使用 new 關鍵字獲得被呼叫者例項),而使用 Spring 框架後,被呼叫者的例項不再由呼叫者建立,而是由 Spring 容器建立,這稱為控制反轉。

屬性 setter 注入

指 IoC 容器使用 setter 方法注入被依賴的例項。通過呼叫無參構造器或無參 static 工廠方法例項化 bean 後,呼叫該 bean 的 setter 方法,即可實現基於 setter 的 DI。

構造方法注入

指 IoC 容器使用構造方法注入被依賴的例項。基於構造器的 DI 通過呼叫帶引數的構造方法實現,每個引數代表一個依賴。

根據註解注入

@Autowired

宣告:參考來源網際網路,有任何爭議可以留言。站在前人的肩上,我們才能看的更遠。

本教程純手打,致力於最實用教程,希望多多轉發支援,對我真的很重要。 歡迎來我公眾號,希望可以結識你,更多原創PDF,微信搜尋:JavaPub,回覆:【666】,也可以催更。

有任何問題都可以來談談 !

下篇SSM整合