1. 程式人生 > 程式設計 >使用Spring Boot搭建Java web專案及開發過程圖文詳解

使用Spring Boot搭建Java web專案及開發過程圖文詳解

一、Spring Boot簡介

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。SpringMVC是非常偉大的框架,開源,發展迅速。優秀的設計必然會劃分、解耦。所以,spring有很多子專案,比如core、context、bean、mvc等。這對知根底的人來說很簡單明瞭,然而springmvc就是為了傻瓜式的操作而發明的。對於初學springmvc的人來說,想要入手就開發需要拷貝一連串的dependency而不知道這個是幹嘛,不知道是不是少了依賴。像我剛接觸springmvc的時候到處百度教程而發現各有不同,於是複製了一個又一個程式碼卻不能自己設定,根本原因是不瞭解各個依賴的包。

Spring-Boot 正是為了解決繁複的程式碼配置而產生的。Spring-Boot 也是基於java-base 開發的程式碼,及不用xml檔案配置,所有程式碼都由java來完成。還可以加入Groovy的動態語言執行。

本文是一個Spring Boot入門級的helloworld程式。

二、準備工作

  1. Java JDK1.7(安裝過程省略)
  2. maven:apache-maven-3.3.9(安裝過程省略)
  3. eclipse(安裝過程省略)
  4. spring-boot-1.5.1

三、用Spring Boot新建web專案

新建一個maven工程(注意,不要勾選create from archytype,雖然它會幫你建立骨架,但是會從外網下載一些東西,很慢,導致會卡在那,下載東西的時間,還不如手工建立一下目錄,分分鐘搞定)。

然後輸入相應的groupId,artifactId。

專案建立過程就省略了。專案建好後,目錄結構是這樣的:

使用Spring Boot搭建Java web專案及開發過程圖文詳解

開啟pom.xml檔案,新增<parent></parent>節點:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>myproject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
  </parent>
  <!-- Additional lines to be added here... -->
</project>

上面沒有新增任何依賴,但仍然可以build。使用命令列:mvn package 對專案進行打包。

注意,是當前專案路徑E:\workspace-springBoot\zsqSpringBoot>下執行命令。當然,你也可以使用IDE,不過使用文字編輯器會讓我們對它更理解。

使用Spring Boot搭建Java web專案及開發過程圖文詳解

現在需要新增依賴 -- 其實就是把依賴的jar新增到buildpath。由於我們已經繼承了 spring-boot-starter-parent ,而 spring-boot-starter-parent 又提供了 dependency-management ,所以我們可以忽略被選中依賴的版本。

在新增依賴之前,我們先看一下現在已有什麼:mvn dependency:tree。該命令會列印一個當前專案的依賴樹。

結果表明,當前沒有任何依賴。 如下:

使用Spring Boot搭建Java web專案及開發過程圖文詳解

OK,現在我們新增spring-web專案依賴:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

現在再次檢視一下依賴樹。

使用Spring Boot搭建Java web專案及開發過程圖文詳解

可以看出,spring-boot-starter-web 包含了很多內容,spring-webmvc、spring-web、jackson、validation、tomcat、starter。

好,接下來修改pom.xml,我直接貼上我的pom.xml完整配置資訊:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <groupId>com.zsq.msb</groupId>
 <artifactId>zsqSpringBoot</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 
 <!-- Maven POM檔案繼承 spring-boot-starter-parent -->
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.4.0.RELEASE</version>
 </parent>
 
 <!-- 為一個web應用程式新增典型的依賴關係,Starter POMs依賴 -->
 <dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 </dependencies>
 
 <!-- Spring Boot也提供了一個可選的 Maven Plugin來建立可執行的jars -->
 <build>
 <plugins>
  <plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
 </plugins>
 </build>
 
</project>

可以看出,繼承了spring-boot-starter-parent,依賴了junit,spring-boot-starter-web,spring-boot-maven-plugin。以前我們在spring的配置,spring-boot都會按照預設配置,幫我們弄好。(你可以像使用標準的Java庫檔案一樣使用Spring Boot。簡單的將需要的spring-boot-*.jar新增到classpath即可。)

Spring Boot不要求任何特殊的工具整合,所以可以使用任何IDE,甚至文字編輯器。只是,仍然建議使用build工具:Maven 或 Gradle。

Spring Boot依賴使用org.springframework.bootgroupId。通常,讓你的Maven POM檔案繼承 spring-boot-starter-parent,並宣告一個或多個 Starter POMs依賴即可。Spring Boot也提供了一個可選的 Maven Plugin來建立可執行的jars。

需要注意的是, spring-boot-starter-parent 是一個非常好的方法,但並不適用於所有情況。有時你需要繼承其他的POM,或者你不喜歡預設的設定,可以使用另外的方式。

完成後下載了相關的額jar包,如下圖:

使用Spring Boot搭建Java web專案及開發過程圖文詳解

現在就可以開始寫程式碼了。由於Maven預設編譯路徑為 src/main/java 下面的原始碼,所以,預設設定下,需要建立這些資料夾。然後,編寫檔案 src/main/java/Example.java:

package com.zsq.msb.dexam;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
@Controller
@EnableAutoConfiguration
public class Example {
 
 @RequestMapping(value ="/home",method = RequestMethod.GET)
 @ResponseBody
 public String home(){
 return "你好,Spring Boot";
 }
 
 public static void main(String[] args){
 SpringApplication.run(Example.class,args);
 
 }

}

這裡我們只需要關心 @EnableAutoConfiguration 即可。這個註解是讓Spring Boot猜測你想怎麼配置Spring,但實際上,它是根據你新增到classpath中的依賴來判斷的。

注意,自動配置 可以配合 Starter POMs 一起工作,但二者不是捆綁到一起的。你仍然可以將Starter POMs中的依賴單獨揀出使用,Spring Boot還是會自動配置。

現在可以右鍵執行main方法,效果如下:

使用Spring Boot搭建Java web專案及開發過程圖文詳解

當你看到這樣的執行後的效果,說明配置是成功的,這樣也就相當於專案已經啟動了。(當然,由於我們使用了 spring-boot-starter-parent POM,所以可以使用 mvn spring-boot:run來啟動專案(根路徑)。)

注意:在啟動之前,先使用Maven來install(打包),你可以在/target目錄下看到zsqSpringBoot-0.0.1-SNAPSHOT.jar,大約10 Mb左右。可以通過 jar tvf target/zsqSpringBoot-0.0.1-SNAPSHOT.jar來檢視其中的內容。此外,在/target目錄下,還可以看到 zsqSpringBoot-0.0.1-SNAPSHOT.jar.original,這是Maven打包出來的。也可以使用命令來啟動專案,在專案檔案下執行命令如:java -jar target/zsqSpringBoot-0.0.1-SNAPSHOT.jar。

使用Spring Boot搭建Java web專案及開發過程圖文詳解

使用命令啟動專案效果如下:

使用Spring Boot搭建Java web專案及開發過程圖文詳解

執行 ctrl+c,退出。

另外,檢視日誌可以發現預設使用的是tomcat,埠繫結在8080:

使用Spring Boot搭建Java web專案及開發過程圖文詳解

現在讓我們來訪問:http://localhost:8080/home。如下圖:

使用Spring Boot搭建Java web專案及開發過程圖文詳解

就可以看到我們程式碼中輸出的字樣:“你好,Spring Boot”了。

回首這個過程,是不是相比於以前快速了許多呢!!

原始碼下載地址:http://download.csdn.net/detail/zsq520520/9753537

總結

到此這篇關於使用Spring Boot搭建Java web專案及開發過程圖文詳解的文章就介紹到這了,更多相關spring boot 搭建javaweb專案內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!