Spring Boot: Configuring a Main Class – Spring Boot 配置一个主类

最后修改: 2018年 4月 16日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

This quick tutorial provides different ways of defining an entry point into a Spring Boot application via Maven and Gradle.

本快速教程提供了通过Maven和Gradle定义Spring Boot应用程序入口的不同方法。

A Spring Boot application’s main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn’t explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.

Spring Boot应用程序的主类是一个包含公共静态void main()方法的类,用于启动Spring ApplicationContext默认情况下,如果没有明确指定主类,Spring将在编译时在classpath中搜索一个主类,如果没有找到或找到多个主类,则无法启动。

Unlike in conventional Java applications, the main class discussed in this tutorial does not appear as the Main-Class metadata property in META-INF/MANIFEST.MF of the resulting JAR or WAR file.

与传统的Java应用程序不同,本教程中讨论的主类不会作为Main-Class元数据属性出现在生成的JAR或WAR文件的META-INF/MANIFEST.MF中。

Spring Boot expects the artifact’s Main-Class metadata property to be set to org.springframework.boot.loader.JarLauncher (or WarLauncher) which means that passing our main class directly to the java command line won’t start our Spring Boot application correctly.

Spring Boot希望工件的Main-Class元数据属性被设置为org.springframework.boot.loader.JarLauncher(或WarLauncher这意味着将我们的主类直接传递给java命令行将无法正确启动Spring Boot应用程序。

An example manifest looks like this:

一个清单的例子是这样的。

Manifest-Version: 1.0
Start-Class: com.baeldung.DemoApplication
Main-Class: org.springframework.boot.loader.JarLauncher

Instead, we need to define the Start-Class property in the manifest which is evaluated by JarLauncher to start the application.

相反,我们需要在清单中定义Start-Class属性,由JarLauncher评估以启动应用程序。

Let’s see how we can control this property using Maven and Gradle.

让我们看看如何用Maven和Gradle来控制这个属性。

2. Maven

2.雯雯

The main class can be defined as a start-class element in the pom.xml‘s properties section:

主类可以在pom.xml的属性部分定义为start-class元素。

<properties>
      <!-- The main class to start by executing "java -jar" -->
      <start-class>com.baeldung.DemoApplication</start-class>
</properties>

Note that this property will only be evaluated if we also add the spring-boot-starter-parent as <parent> in our pom.xml.

请注意,只有当我们在pom.xml中同时添加spring-boot-starter-parent作为<parent>,这个属性才会被评估。

Alternatively, the main class can be defined as the mainClass element of the spring-boot-maven-plugin in the plugin section of our pom.xml:

另外,主类可以定义为mainClass元素,在我们的pom.xml插件部分的spring-boot-maven-插件

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>             
            <configuration>    
                <mainClass>com.baeldung.DemoApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

An example of this Maven configuration can be found over on GitHub.

这个Maven配置的例子可以在GitHub上找到

3. Gradle

3.Gradle

If we’re using the Spring Boot Gradle plugin, there are a few configurations inherited from org.springframework.boot where we could specify our main class.

如果我们使用Spring Boot Gradle插件,有一些从org.springframework.boot继承的配置,我们可以在那里指定我们的主类。

In the project’s Gradle file, mainClassName can be defined within springBoot configuration block. This change made here is picked up by bootRun and bootJar task:

在项目的Gradle文件中,mainClassName可以被定义在springBoot配置块中。这里所做的改变会被bootRunbootJar任务所接收。

springBoot {
    mainClassName = 'cpm.baeldung.DemoApplication'
}

Alternatively, the main class can be defined as the mainClassName property of bootJar Gradle task:

另外,主类可以被定义为mainClassName bootJar Gradle任务的mainClassName属性:

bootJar {
    mainClassName = 'cpm.baeldung.DemoApplication'
}

Or as a manifest attribute of the bootJar task:

或者作为bootJar任务的清单属性:

bootJar {
    manifest {
	attributes 'Start-Class': 'com.baeldung.DemoApplication'
    }
}

Note that the main class specified in the bootJar configuration block only affects the JAR that the task itself produces. The change doesn’t affect the behavior of other Spring Boot Gradle tasks such as bootRun.

请注意,bootJar配置块中指定的主类只影响任务本身产生的JAR。这一变化并不影响其他Spring Boot Gradle任务的行为,如bootRun

As a bonus, if the Gradle application plugin is applied to the project, mainClassName can be defined as a global property:

作为奖励,如果Gradle应用程序插件被应用于项目,mainClassName可以被定义为一个全局属性:

mainClassName = 'com.baeldung.DemoApplication'

We can find an example of these Gradle configurations over on GitHub.

我们可以在GitHub上找到这些Gradle配置的例子

4. Using CLI

4.使用CLI

We can also specify a main class via the command line interface.

我们也可以通过命令行界面指定一个主类。

Spring Boot’s org.springframework.boot.loader.PropertiesLauncher comes with a JVM argument to let you override the logical main-class called loader.main:

Spring Boot的org.springframework.boot.loader.PropertiesLauncher带有一个JVM参数,让你覆盖名为loader.main的逻辑主类。

java -cp bootApp.jar -Dloader.main=com.baeldung.DemoApplication org.springframework.boot.loader.PropertiesLauncher

5. Conclusion

5.总结

There are more than a few ways to specify the entry point to a Spring Boot application. It’s important to know that all these configurations are just different ways to modify the manifest of a JAR or WAR file.

指定Spring Boot应用程序入口点的方法不止几种。要知道,所有这些配置只是修改JAR或WAR文件清单的不同方式。

Working code examples can be found here and here.

工作代码的例子可以在这里这里找到。