Running Your Application
将您的应用程序打包成 jar 并使用嵌入式 HTTP 服务器的最大优点之一是您可以像运行其他任何应用程序一样运行您的应用程序。该示例适用于调试 Spring Boot 应用程序。您不需要任何特殊的 IDE 插件或扩展程序。
One of the biggest advantages of packaging your application as a jar and using an embedded HTTP server is that you can run your application as you would any other. The sample applies to debugging Spring Boot applications. You do not need any special IDE plugins or extensions.
本节仅介绍基于 jar 的打包。如果您选择将应用程序打包成 war 文件,请查阅服务器和 IDE 文档。 |
This section only covers jar-based packaging. If you choose to package your application as a war file, see your server and IDE documentation. |
Running From an IDE
您可以在 IDE 中以 Java 应用程序的形式运行 Spring Boot 应用程序。但是,您首先需要导入您的项目。导入步骤因您的 IDE 和构建系统而异。大多数 IDE 都能直接导入 Maven 项目。例如,Eclipse 用户可以选择 Import…
→ Existing Maven Projects
自 File
菜单。
You can run a Spring Boot application from your IDE as a Java application.
However, you first need to import your project.
Import steps vary depending on your IDE and build system.
Most IDEs can import Maven projects directly.
For example, Eclipse users can select Import…
→ Existing Maven Projects
from the File
menu.
如果您无法直接将项目导入 IDE,您可能可以使用构建插件生成 IDE 元数据。Maven 包含 Eclipse 和 IDEA 的插件。Gradle 提供了 {url-gradle-docs}/userguide.html[各种 IDE] 的插件。
If you cannot directly import your project into your IDE, you may be able to generate IDE metadata by using a build plugin. Maven includes plugins for Eclipse and IDEA. Gradle offers plugins for {url-gradle-docs}/userguide.html[various IDEs].
如果您不小心运行了两次 Web 应用程序,您将看到 “Port already in use” 错误。Spring Tools 用户可以使用 |
If you accidentally run a web application twice, you see a “Port already in use” error.
Spring Tools users can use the |
Running as a Packaged Application
如果您使用 Spring Boot Maven 或 Gradle 插件来创建可执行 jar,您可以使用 java -jar
运行您的应用程序,如下例所示:
If you use the Spring Boot Maven or Gradle plugins to create an executable jar, you can run your application using java -jar
, as shown in the following example:
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
还可以运行启用了远程调试支持的打包应用程序。这样做可以让您将调试器附加到您的打包应用程序,如下例所示:
It is also possible to run a packaged application with remote debugging support enabled. Doing so lets you attach a debugger to your packaged application, as shown in the following example:
$ java -agentlib:jdwp=server=y,transport=dt_socket,address=8000,suspend=n \
-jar target/myapplication-0.0.1-SNAPSHOT.jar
Using the Maven Plugin
Spring Boot Maven 插件包含一个 run
目标,可用于快速编译和运行您的应用程序。应用程序以展开形式运行,就像在 IDE 中一样。以下示例显示了在 Maven 中运行 Spring Boot 应用程序的典型命令:
The Spring Boot Maven plugin includes a run
goal that can be used to quickly compile and run your application.
Applications run in an exploded form, as they do in your IDE.
The following example shows a typical Maven command to run a Spring Boot application:
$ mvn spring-boot:run
您可能还想使用 MAVEN_OPTS
操作系统环境变量,如下例所示:
You might also want to use the MAVEN_OPTS
operating system environment variable, as shown in the following example:
$ export MAVEN_OPTS=-Xmx1024m
Using the Gradle Plugin
Spring Boot Gradle 插件也包含一个 bootRun
任务,可用于以展开形式运行您的应用程序。只要您应用 org.springframework.boot
和 java
插件,就会添加 bootRun
任务,如下例所示:
The Spring Boot Gradle plugin also includes a bootRun
task that can be used to run your application in an exploded form.
The bootRun
task is added whenever you apply the org.springframework.boot
and java
plugins and is shown in the following example:
$ gradle bootRun
您可能还想使用 JAVA_OPTS
操作系统环境变量,如下例所示:
You might also want to use the JAVA_OPTS
operating system environment variable, as shown in the following example:
$ export JAVA_OPTS=-Xmx1024m
Hot Swapping
由于 Spring Boot 应用程序是纯 Java 应用程序,因此 JVM 热交换应该开箱即用。JVM 热交换对其可以替换的字节码有一定限制。对于更完整的解决方案,可以使用 JRebel。
Since Spring Boot applications are plain Java applications, JVM hot-swapping should work out of the box. JVM hot swapping is somewhat limited with the bytecode that it can replace. For a more complete solution, JRebel can be used.
spring-boot-devtools
模块还包括对快速应用程序重新启动的支持。有关详细信息,请参见 Hot swapping “How-to”。
The spring-boot-devtools
module also includes support for quick application restarts.
See the Hot swapping “How-to” for details.