How to run a Spring Boot application

19. Run the application

One of the great benefits of packaging your application as a jar and using an embedded HTTP server is that you can run your application just like any other. Debugging a Spring Boot application is also easy; you don't need any special IDEs or extensions.

Note : This chapter only covers jar-based packaging. If you choose to package the application into a war file, you'd better refer to the server and IDE documentation.

19.1. Running from the IDE

You can run a Spring Boot application from the IDE just like a simple Java application, however, you first need to import the project. The import steps depend on your IDE and build system. Most IDEs can directly import Maven projects, for example Eclipse users can select --> Filefrom the menu .Import…​Existing Maven Projects

If you can't directly import the project into the IDE, you may need to use the build system to generate IDE metadata. Maven has plugins for Eclipse and IDEA ; Gradle provides plugins for various IDEs .

NOTE : If you accidentally run a webapp twice, you will see a "Port already in use" error. To ensure that any existing instances are closed, STS users can use Relaunchthe button instead of Runthe button.

19.2. Running as a Packaged Application

If you use the Spring Boot Maven or Gradle plugin to create an executable jar, you can use it java -jarto run your application. For example:

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

It is possible to run a packaged application with remote debugging support enabled, which allows you to attach a debugger to the packaged application:

$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
       -jar target/myproject-0.0.1-SNAPSHOT.jar

19.3. Running with the Maven Plugin

The Spring Boot Maven plugin includes a rungoal that can be used to quickly compile and run applications. The application runs in an exposed way, and you can edit resources thanks to instant "hot" reloading.

$ mvn spring-boot:run

You might want to use useful OS environment variables:

$ export MAVEN_OPTS=-Xmx1024m -XX:MaxPermSize=128M -Djava.security.egd=file:/dev/./urandom

(The "egd" setting speeds up Tomcat by providing it with a faster source of entropy for session keys.)

19.4. Running with the Gradle plugin

The Spring Boot Gradle plugin also includes a runtarget that can be used to run your application exposed. No matter when you import spring-boot-plugin, bootRuntasks are always added.

$ gradle bootRun

You might want to use those useful OS environment variables:

$ export JAVA_OPTS=-Xmx1024m -XX:MaxPermSize=128M -Djava.security.egd=file:/dev/./urandom

19.5. Heat exchange

Since Spring Boot applications are just plain Java applications, JVM hot-swapping should work just fine. JVM hotswapping is somewhat limited in the bytecode it can replace, a more comprehensive solution could use the Spring Loaded project or JRebel .

For heat exchange, please refer to the corresponding chapter of [“How-to”](…/IX. 'How-to' guides/README.md).

Guess you like

Origin blog.csdn.net/2301_76484015/article/details/130555452