19. Running Your Application(运行您的应用)
1 | One of the biggest advantages of packaging your application as |
将您的应用打包成一个jar并且使用内置的HTTP服务器有一个巨大的优势就是您可以直接运行您的应用。调试Spring Boot应用也是非常简单的。您不需要任何特殊的IDE插件或者扩展。
1 | This section only covers jar based packaging. If you choose |
本节只讲述基于jar包打包的方式。如果你选择将您的应用打包成一个war包,您应该参考
到您的服务器和IDE文档。
19.1 Running from an IDE(从一个IDE钟运行)
1 | You can run a Spring Boot application from your IDE as a simple |
您可以从您的IDE将Spring Boot应用作为一个简单的java应用来运行。然而,您首先需要导入您的工程。导入步骤根据您的IDE构建系统的不同而变化。大部分IDE能够直接导入Maven工程。举个例子,Eclipse用户可以选择File-Import-Existing Maven Projects菜单。1
2
3
4If 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 various IDEs.
如果您不能直接从IDE中导入您的工程,您可以通过构建插件生成IDE metadata。Maven包含Eclipse和IDEA的插件。Gradle为大部分IDE提供了插件1
2
3
4If you accidentally run a web application twice, you see
a “Port already in use” error. STS users can use the Relaunch
button rather than the Run button to ensure that any existing
instance is closed.
如果您偶然的运行了2次web应用,您会发现“Port already in use”端口已经被使用的报错.STS使用者,能够使用Relaunch按钮来避免这个问题,如果使用Run按钮请确认任何实例都已经处于关闭状态。
19.2 Running as a Packaged Application(作为一个大包应用运行)
1 | If you use the Spring Boot Maven or Gradle plugins to create |
如果您使用Spring Boot Maven或者Gradle插件创建了一个可执行的jar,您可以通过java -jar命令来运行它,如下例子:1
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
1 | It is also possible to run a packaged application with remote |
同样可以通过远程调试模式运行一个已经打包的应用,如果远程调试开启的情况下。这样做的话您可以链接一个断点到您的已经打包的应用中,如下例子:1
2$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
-jar target/myapplication-0.0.1-SNAPSHOT.jar
19.3 Using the Maven Plugin(使用Maven插件)
1 | The Spring Boot Maven plugin includes a run goal that can |
Spring Boot的Maven插件包含了一个运行目标可以快速编译和运行您的应用。应用像在您的IDE中以分解的形式运行。接下去的例子展示了一个典型的Maven命令运行Spring Boot 应用。1
$ mvn spring-boot:run
1 | You might also want to use the MAVEN_OPTS operating system |
您也许也想使用MAVEN_OPTS操作系统环境变量,如下例子:1
$ export MAVEN_OPTS=-Xmx1024m
19.4 Using the Gradle Plugin(使用Gradle插件)
1 | The Spring Boot Gradle plugin also includes a bootRun task that can |
Spring Boot的Gradle插件同样映入了bootRun任务,能够运用分解的方式运行您的应用。bootRun任务将会被自动引入,如果您引入了org.springframework.boot和java插件。如下例子:1
$ gradle bootRun
1 | You might also want to use the JAVA_OPTS operating system |
您也许想使用JAVA_OPTS操作系统环境变量,如下例子:1
$ export JAVA_OPTS=-Xmx1024m
19.5 Hot Swapping(热交换)
1 | Since Spring Boot applications are just plain Java applications, |
由于Spring Boot应用程序只是普通的Java应用程序,所以JVM热插拔应该可以开箱即用。JVM热插拔是
在可以替代的字节码方面有所限制。需要更完整的解决方案,可以使用JRebel。
1 | The spring-boot-devtools module also includes support for |
spring-boot-devtools模块也宝行了支持快速重启应用的功能。查看章节20,开发者工具章节接下去会讲这点,已经热插拔相关细节。