1. 概述
本快速教程提供了通過 Maven 和 Gradle 定義 Spring Boot 應用程序入口點的不同方法。
Spring Boot 應用程序的主類是一個包含 public static void main() 方法的類,該方法啓動 Spring ApplicationContext。 默認情況下,如果未明確指定主類,Spring 在編譯時將在類路徑中搜索一個,如果在類路徑中未找到一個或多個主類,則無法啓動。
與傳統 Java 應用程序不同,本教程中討論的主類不會作為 META-INF/MANIFEST.MF 中的 Main-Class 元數據屬性出現,該屬性存在於生成的 JAR 或 WAR 文件中。
Spring Boot 期望 Artifact 的 Main-Class 元數據屬性設置為 org.springframework.boot.loader.JarLauncher (或 WarLauncher),這意味着直接將主類傳遞給 java 命令行將不會正確啓動我們的 Spring Boot 應用程序。
以下是一個示例 manifest:
Manifest-Version: 1.0
Start-Class: com.baeldung.DemoApplication
Main-Class: org.springframework.boot.loader.JarLauncher相反,我們需要在清單中定義 Start-Class 屬性,讓 JarLauncher 評估 以啓動應用程序。
讓我們看看如何使用 Maven 和 Gradle 來控制這個屬性。
2. Maven
<em >a start-class</em > 元素可以定義為 <em >pom.xml</em > 的 properties 部分:
<properties>
<!-- The main class to start by executing "java -jar" -->
<start-class>com.baeldung.DemoApplication</start-class>
</properties>
請注意,此屬性僅在同時添加 spring-boot-starter-parent 作為 <parent> 時才會被評估。
或者,主類可以定義為 mainClass 元素,位於 spring-boot-maven-plugin 的插件部分 pom.xml 中:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.DemoApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>3. Gradle
如果我們在使用 Spring Boot Gradle 插件,我們可以通過繼承自 org.springframework.boot 的多種配置來指定我們的主類。
我們可以通過在項目 Gradle 文件中的 springBoot 配置塊中指定 mainClass 屬性來指定 Spring Boot DSL 中的 mainClass 屬性。 這樣定義後,bootRun 和 bootJar 任務會繼承並全局應用它:
springBoot {
mainClass = 'com.baeldung.DemoApplication'
}當然,以下是翻譯後的內容:
或者,我們可以將主類定義為 mainClass 屬性,該屬性位於 bootJar Gradle 任務中。
tasks.named("bootJar") {
mainClass = 'com.baeldung.DemoApplication'
}此外,我們還可以將其定義為 BootJar 任務的顯式屬性:
tasks.named("bootJar") {
manifest {
attributes 'Start-Class': 'com.baeldung.DemoApplication'
}
}特別地,我們在<em>bootJar</em>配置塊中指定的類僅影響任務本身產生的 JAR 包。因此,這個定義不會影響 Spring Boot Gradle 任務(如<em>bootRun</em>)的行為。
此外,mainClass = 'com.baeldung.DemoApplication' 我們也可以通過命令行界面指定主類。 Spring Boot 的 org.springframework.boot.loader.PropertiesLauncher 包含一個 JVM 參數,允許您覆蓋名為 loader.main 的邏輯主類: 在 Spring Boot 項目中,通常會使用多個 Spring 配置文件,以實現特定行為。本節將介紹如何配置此類項目的入口點。 在 Spring Boot 應用中,我們使用 @SpringBootApplication 註解標記主類。因此,通過結合使用 @SpringBootApplication 和 @Profile 註解,可以在項目中擁有多個主類。但是,我們必須確保在運行應用程序時激活恰好一個配置文件。 接下來,讓我們看看當我們嘗試在不明確指定要用於活動配置文件的主類的情況下運行應用程序時會發生什麼: 不幸的是,Spring 框架無法運行應用程序,並報告找不到主類。 別擔心,這裏有一個眾所周知的解決方案。我們現在就來學習如何解決這個問題。 首先,您需要使用 進一步,我們需要使用 spring-boot-maven-plugin 在 pom.xml 中進行配置: 最後,我們必須注意到,我們已在插件的配置部分中指定了 mainClass 作為 spring.boot.mainclass 屬性值。 現在我們的項目已正確設置。因此,讓我們執行一個特定的 Spring Boot 項目配置文件: 太棒了!看起來我們這次搞定了。 有多種方法可以指定 Spring Boot 應用程序的入口點。重要的是要知道,所有這些配置都只是對 JAR 或 WAR 文件分發的元數據進行修改的不同方式。4. 使用命令行
java -cp bootApp.jar -Dloader.main=com.baeldung.DemoApplication org.springframework.boot.loader.PropertiesLauncher5. 用於不同 Spring 配置文件的主類
5.1. 理解場景
$ mvn spring-boot:run \
-Dspring.profiles.active=errorhandling \
-Perrorhandling
# output trimmed to show failure
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.1.5:run (default-cli) on project spring-boot-basic-customization: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.7.11:run failed: Unable to find a single main class from the following candidates [com.baeldung.favicon.FaviconApplication, com.baeldung.failureanalyzer.FailureAnalyzerApplication, com.baeldung.errorhandling.ErrorHandlingApplication, com.baeldung.changeport.CustomApplication, com.baeldung.bootcustomfilters.SpringBootFiltersApplication] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
5.2. 為 Spring 配置文件指定主類
<em>pom.xml</em> 文件中每個 Spring 配置文件上的 <em>spring.boot.mainclass</em> 屬性來指定應用程序的入口點。<profiles>
...
<profile>
<id>errorhandling</id>
<properties>
<spring.boot.mainclass>com.baeldung.errorhandling.ErrorHandlingApplication</spring.boot.mainclass>
</properties>
</profile>
...
</profiles><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.3.5</version>
<configuration>
<mainClass>${spring.boot.mainclass}</mainClass>
</configuration>
</plugin>
</plugins>
</build>5.3. 運行應用程序
$ mvn spring-boot:run \
-Dspring.profiles.active=errorhandling \
-Perrorhandling
# trimmed build output
...
22:46:16.908 [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 9000 (http) with context path ''
...
6. 結論