
- Spring Boot 教程
- Spring Boot - 首頁
- Spring Boot - 簡介
- Spring Boot - 快速入門
- Spring Boot - 引導啟動
- Spring Tool Suite
- Spring Boot - Tomcat 部署
- Spring Boot - 構建系統
- Spring Boot - 程式碼結構
- Spring Bean & 依賴注入
- Spring Boot - 執行器
- Spring Boot - 啟動器
- Spring Boot - 應用程式屬性
- Spring Boot - 配置
- Spring Boot - 註解
- Spring Boot - 日誌
- 構建 RESTful Web 服務
- Spring Boot - 異常處理
- Spring Boot - 攔截器
- Spring Boot - Servlet 過濾器
- Spring Boot - Tomcat 埠號
- Spring Boot - Rest 模板
- Spring Boot - 檔案處理
- Spring Boot - 服務元件
- Spring Boot - Thymeleaf
- 消費 RESTful Web 服務
- Spring Boot - CORS 支援
- Spring Boot - 國際化
- Spring Boot - 排程
- Spring Boot - 啟用 HTTPS
- Spring Boot - Eureka 伺服器
- 使用 Eureka 進行服務註冊
- 閘道器代理伺服器和路由
- Spring Cloud 配置伺服器
- Spring Cloud 配置客戶端
- Spring Boot - Actuator
- Spring Boot - Admin 伺服器
- Spring Boot - Admin 客戶端
- Spring Boot - 啟用 Swagger2
- Spring Boot - 使用 SpringDoc OpenAPI
- Spring Boot - 建立 Docker 映象
- 跟蹤微服務日誌
- Spring Boot - Flyway 資料庫
- Spring Boot - 傳送郵件
- Spring Boot - Hystrix
- Spring Boot - WebSocket
- Spring Boot - 批處理服務
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - 單元測試用例
- Rest Controller 單元測試
- Spring Boot - 資料庫處理
- 保護 Web 應用程式
- Spring Boot - 使用 JWT 的 OAuth2
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 登入
- Spring Boot 資源
- Spring Boot - 快速指南
- Spring Boot - 有用資源
- Spring Boot - 討論
Spring Boot - 引導啟動
本章將向您解釋如何在 Spring Boot 應用程式上執行引導啟動。
Spring Initializer
引導啟動 Spring Boot 應用程式的一種方法是使用 Spring Initializer。為此,您需要訪問 Spring Initializer 網頁 www.start.spring.io 並選擇您的構建、Spring Boot 版本和平臺。此外,您需要提供一個組、工件和執行應用程式所需的依賴項。
觀察以下螢幕截圖,其中顯示了一個示例,我們在其中添加了spring-boot-starter-web依賴項以編寫 REST 端點。

提供完組、工件、依賴項、構建專案、平臺和版本後,單擊生成專案按鈕。zip 檔案將下載,並且檔案將被提取。
本節將透過使用 Maven 和 Gradle 來解釋示例。
Maven
下載專案後,解壓縮檔案。現在,您的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tutorialspoint</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Gradle
對於 Gradle,您可以檢查 build.gradle 如下
build.gradle
buildscript { ext { springBootVersion = '3.3.3' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.tutorialspoint' version = '0.0.1-SNAPSHOT' sourceCompatibility = 21 repositories { mavenCentral() } dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test') }
類路徑依賴項
Spring Boot 提供了許多啟動器以將 jar 新增到我們的類路徑中。例如,要編寫 Rest 端點,我們需要在類路徑中新增spring-boot-starter-web依賴項。觀察下面顯示的程式碼以更好地理解:
Maven 依賴項
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Gradle 依賴項
dependencies { compile('org.springframework.boot:spring-boot-starter-web') }
主方法
主方法應該編寫 Spring Boot 應用程式類。此類應使用@SpringBootApplication進行註釋。這是啟動 Spring Boot 應用程式的入口點。您可以在src/java/main目錄下找到預設包的主類檔案。
在此示例中,主類檔案位於src/java/main目錄下,預設包為com.tutorialspoint.demo。觀察此處顯示的程式碼以更好地理解:
package com.tutorialspoint.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
編寫 Rest 端點
要在 Spring Boot 應用程式的主類檔案中編寫一個簡單的 Hello World Rest 端點,請按照以下步驟操作:
首先,在類的頂部新增@RestController註釋。
現在,使用@GetMapping註釋編寫一個請求 URI 方法。
然後,請求 URI 方法應返回Hello World字串。
現在,您的 Spring Boot 應用程式主類檔案將如下所示:
package com.tutorialspoint.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @GetMapping(value = "/") public String hello() { return "Hello World"; } }
建立可執行 JAR
讓我們使用命令提示符中的 Maven 命令建立一個可執行 JAR 檔案以執行 Spring Boot 應用程式,如下所示:
使用 Maven 命令 mvn clean install,如下所示:
E:\dev\demo>mvn clean install
您將看到類似於以下的結果
[INFO] Scanning for projects... [INFO] [INFO] [1m----------------------< [0;36mcom.tutorialspoint:demo[0;1m >-----------------------[m [INFO] [1mBuilding demo 0.0.1-SNAPSHOT[m [INFO] from pom.xml [INFO] [1m--------------------------------[ jar ]---------------------------------[m [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/3.1.3/maven-install-plugin-3.1.3.pom .... [INFO] [INFO] [1m--- [0;32mclean:3.3.2:clean[m [1m(default-clean)[m @ [36mdemo[0;1m ---[m [INFO] Deleting E:\Dev\demo\target [INFO] [INFO] [1m--- [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36mdemo[0;1m ---[m [INFO] Copying 1 resource from src\main\resources to target\classes [INFO] Copying 0 resource from src\main\resources to target\classes [INFO] ... [INFO] [INFO] [1m--- [0;32msurefire:3.2.5:test[m [1m(default-test)[m @ [36mdemo[0;1m ---[m [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-launcher/1.10.3/junit-platform-launcher-1.10.3.pom [INFO] Downloaded from central: https://repo.maven.apache.org/maven2/org/junit/platform/junit-platform-launcher/1.10.3/junit-platform-launcher-1.10.3.pom (3.0 kB at 13 kB/s) [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.tutorialspoint.demo.[1mDemoApplicationTests[m 17:35:15.626 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.tutorialspoint.demo.DemoApplicationTests]: DemoApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 17:35:15.802 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.tutorialspoint.demo.DemoApplication for test class com.tutorialspoint.demo.DemoApplicationTests . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.3.3) 2024-09-02T17:35:16.380+05:30 INFO 6204 --- [demo] [ main] c.t.demo.DemoApplicationTests : Starting DemoApplicationTests using Java 21.0.3 with PID 6204 (started by Tutorialspoint in E:\Dev\demo) 2024-09-02T17:35:16.381+05:30 INFO 6204 --- [demo] [ main] c.t.demo.DemoApplicationTests : No active profile set, falling back to 1 default profile: "default" 2024-09-02T17:35:18.173+05:30 INFO 6204 --- [demo] [ main] c.t.demo.DemoApplicationTests : Started DemoApplicationTests in 2.162 seconds (process running for 4.295) ... [INFO] Results: [INFO] [INFO] [1;32mTests run: 1, Failures: 0, Errors: 0, Skipped: 0[m [INFO] [INFO] [INFO] [1m--- [0;32mjar:3.4.2:jar[m [1m(default-jar)[m @ [36mdemo[0;1m ---[m [INFO] Building jar: E:\Dev\demo\target\demo-0.0.1-SNAPSHOT.jar [INFO] [INFO] [1m--- [0;32mspring-boot:3.3.3:repackage[m [1m(repackage)[m @ [36mdemo[0;1m ---[m [INFO] Replacing main artifact E:\Dev\demo\target\demo-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies in BOOT-INF/. [INFO] The original artifact has been renamed to E:\Dev\demo\target\demo-0.0.1-SNAPSHOT.jar.original [INFO] [INFO] [1m--- [0;32minstall:3.1.3:install[m [1m(default-install)[m @ [36mdemo[0;1m ---[m .... [INFO] Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-xml/3.0.1/plexus-xml-3.0.1.jar (94 kB at 806 kB/s) [INFO] Installing E:\Dev\demo\pom.xml to C:\Users\Tutorialspoint\.m2\repository\com\tutorialspoint\demo\0.0.1-SNAPSHOT\demo-0.0.1-SNAPSHOT.pom [INFO] Installing E:\Dev\demo\target\demo-0.0.1-SNAPSHOT.jar to C:\Users\Tutorialspoint\.m2\repository\com\tutorialspoint\demo\0.0.1-SNAPSHOT\demo-0.0.1-SNAPSHOT.jar [INFO] [1m------------------------------------------------------------------------[m [INFO] [1;32mBUILD SUCCESS[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 18.938 s [INFO] Finished at: 2024-09-02T17:35:25+05:30 [INFO] [1m------------------------------------------------------------------------[m
同樣,對於 Gradle,您可以執行以下命令來構建 jar。
gradle clean build
使用 Java 執行 Hello World
建立可執行 JAR 檔案後,您可以在 target 目錄下找到它。在我們的例子中,它位於E: > Dev > demo > target > demo-0.0.1-SNAPSHOT.jar.
現在,使用命令java –jar <JARFILE>執行 JAR 檔案。觀察在上述示例中,JAR 檔名為demo-0.0.1-SNAPSHOT.jar
E:\dev\demo\target>java -jar demo-0.0.1-SNAPSHOT.jar
執行 jar 檔案後,您可以在控制檯視窗中看到輸出,如下所示:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.3.3) 2024-09-02T17:42:38.623+05:30 INFO 4712 --- [demo] [ main] c.tutorialspoint.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 21.0.2 with PID 4712 (E:\Dev\demo\target\demo-0.0.1-SNAPSHOT.jar started by Tutorialspoint in E:\Dev\demo\target) 2024-09-02T17:42:38.626+05:30 INFO 4712 --- [demo] [ main] c.tutorialspoint.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default" 2024-09-02T17:42:40.250+05:30 INFO 4712 --- [demo] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http) 2024-09-02T17:42:40.266+05:30 INFO 4712 --- [demo] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2024-09-02T17:42:40.266+05:30 INFO 4712 --- [demo] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.28] 2024-09-02T17:42:40.320+05:30 INFO 4712 --- [demo] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2024-09-02T17:42:40.322+05:30 INFO 4712 --- [demo] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1574 ms 2024-09-02T17:42:40.943+05:30 INFO 4712 --- [demo] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2024-09-02T17:42:40.969+05:30 INFO 4712 --- [demo] [ main] c.tutorialspoint.demo.DemoApplication : Started DemoApplication in 3.122 seconds (process running for 3.902)
現在,檢視控制檯,Tomcat 在埠 8080 (http) 上啟動。現在,轉到 Web 瀏覽器並訪問 URL https://:8080/,您將看到如下所示的輸出:
