1. 程式人生 > >Spring Boot實戰:part1 構建第一個spring boot 專案

Spring Boot實戰:part1 構建第一個spring boot 專案

系統環境

MacOS、JDK1.8、maven3.6.2、 IntelliJ IDEA 2019.2

搭建springboot專案

搭建專案可以選擇start.spring.io線上構建或直接在IDE工具構建

一、使用start.spring.io線上構建

進入https://start.spring.io/官網。

選擇專案元資料

建立一個maven專案,本案例是以Java1.8、以jar包打包方式、並且在spring web的基礎上增加了Spring Actuator 來進行健康監控

生成專案

選擇完構成模組後,點選Generate就會下載到本地

將專案匯入IDEA

先將下載好的hello-springboot.zip進行解壓,然後開啟IDEA,點選下圖所示的open,選擇剛才解壓好的資料夾

二、使用IDEA工具構建

新建專案

開啟idea,選單欄依次選擇File--》New-->Project,按下圖步驟操作

選擇專案元資料

選擇後點擊next

選擇依賴模組spring web + spring boot Actuator(專案健康監控,後續會用來監測專案是否啟動成功),然後點選next

選擇專案存放地址,然後點選finish,就完成了。

到此為止,我們的專案就搭建完成了。

三、專案結構簡單介紹

搭建完成後的專案結構如下圖所示:

HelloSpringbootApplication.java

這個Java檔案是springboot的應用程式入口類,倘若把整個應用程式比作一個class類的話,那麼HelloSpringbootApplication.java檔案的重要性就如main方法一般。

package com.example.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloSpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloSpringbootApplication.class, args);
	}

}

application.properties

它作為應用程式的一個重要配置檔案,用來配置可變引數,比如服務地址、埠等。

application.yml

在yml出現之前,我們常用的配置檔案有xml和properties,然而xml配置繁瑣,properties又沒有層級關係項配置多層關係會發現有大量的重複程式碼。yml結合了xml和properties兩者的優勢,既支援層級配置,又不會產生重複程式碼。大家可通過如下三個檔案進行對比,當相同節點的子節點越多,xml和properties的缺陷就越嚴重。

yml樣例:

#服務的相關配置
server:
  address: 127.0.0.1
  port: 8080
  servlet:
    context-path: /hello #伺服器路徑不填預設為空

#spring相關配置
spring:
  application:
    name: spring-boot-demo #服務名

xml樣例:

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <server>
        <address>127.0.0.1</address>
        <port>8080</port>
        <servlet>
            <context-path>/hello</context-path>
        </servlet>
    </server>

    <spring>
        <application>
            <name>spring-boot-demo</name>
        </application>
    </spring>
</root>

 properties樣例:

server.address=127.0.0.1
server.port=8080
server.servlet.context-path=hello
spring.application.name=spring-boot-demo

重要說明:①不管是start.spring.io方式搭建,還是IDEA搭建,搭建完的專案目前(idea版本)預設是沒有yml檔案的,eclipse構建好的springboot專案就有yml,希望@jetbrains.com早日把這個功能給完善一下,不然對不起全宇宙最好用的Java開發工具。就現狀而言,需要我們自己手動處理下,最簡單的方式就是直接把properties的字尾名改為yml

②因為idea2019.2版本已支援yml檔案,如果有的同學用的idea版本比較舊,那需要手動下載一個外掛:YAML,不然專案載入讀取不到yml裡的內容。下載外掛可以去idea設定-->Plugins-->搜尋YAML關鍵字,然後install即可。

static

static資料夾顧名思義就是存放靜態檔案的地方,我們可以用來存放img、js、css等,當然如果是商用專案建議靜態檔案要單獨部署一個伺服器,對於小的檔案可以放專案裡。

templates

templates資料夾也很明瞭,就是存放資料模板的地方,我們可以存放html、ftl、excel模板等

專案最重要的組成部分到這裡就介紹完了,接下來就是見證奇蹟的時刻了!

四、專案啟動

按下圖所示操作:

接下來我們觀察一下控制檯,如果你的控制檯列印的下圖一樣的話,那麼說明啟動成功了。

[INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:run (default-cli) @ hello-springboot ---
[IJ]-1-ARTIFACT_RESOLVED-[IJ]-path=/Users/yunnasheng/work/mvn-repository/org/eclipse/sisu/sisu-plexus/0.3.2/sisu-plexus-0.3.2.pom-[IJ]-artifactCoord=org.eclipse.sisu:sisu-plexus:pom:0.3.2-[IJ]-error=

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-10-01 17:44:42.216  INFO 19696 --- [           main] c.e.s.HelloSpringbootApplication         : Starting HelloSpringbootApplication on yunnashengdeMacBook-Pro.local with PID 19696 (/Users/yunnasheng/work/github-workspace/hello-springboot/target/classes started by yunnasheng in /Users/yunnasheng/work/github-workspace/hello-springboot)
2019-10-01 17:44:42.219  INFO 19696 --- [           main] c.e.s.HelloSpringbootApplication         : No active profile set, falling back to default profiles: default
2019-10-01 17:44:43.021  INFO 19696 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
2019-10-01 17:44:43.038  INFO 19696 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-01 17:44:43.038  INFO 19696 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-10-01 17:44:43.100  INFO 19696 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/hello]  : Initializing Spring embedded WebApplicationContext
2019-10-01 17:44:43.100  INFO 19696 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 851 ms
2019-10-01 17:44:43.466  INFO 19696 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-01 17:44:43.677  INFO 19696 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-10-01 17:44:43.743  INFO 19696 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path '/hello'
2019-10-01 17:44:43.746  INFO 19696 --- [           main] c.e.s.HelloSpringbootApplication         : Started HelloSpringbootApplication in 1.801 seconds (JVM running for 3.982)

為了進一步驗證也為了讓我們的spring boot Actuator 有用武之地「Actuator的功能不僅僅如此,後續會單獨起一個文章來細細品味Actuator的強大之處」

廢話不多說,直接瀏覽器訪問:http://localhost:8080/hello/actuator/health   如果返回下圖所示,說明我們的應用服務是啟動成功狀態。

這篇文章到這裡就結束了,我們的第一個spring boot 專案就搭建好了。如果大家有任何問題,或者任何意見和建議,可以在下方留言或者私信我,我都會及時綁大家解決問題並改正自己的缺點。

GitHub原始碼地址:https://github.com/yunnasheng/hello-springboot

HTTPS克隆:https://github.com/yunnasheng/hello-springboot.git