To stop a Spring Boot application, you can use the CTRL + C
keyboard shortcut if the application is running in the terminal, or use the kill
command if the application is running as a background process.
Alternatively, you can use the SpringApplication.exit
method to stop the application. This method will shutdown the application context and stop all the running threads.
Here's an example of how you can use the SpringApplication.exit
method to stop a Spring Boot application:
@SpringBootApplication public class MyApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args); // ... context.close(); SpringApplication.exit(context, () -> 0); } }
This code will close the application context and stop the application using the SpringApplication.exit
method.
Note that the SpringApplication.exit
method should only be used in cases where the application needs to be stopped abruptly, such as when an unrecoverable error occurs. In normal circumstances, you should use the CTRL + C
keyboard shortcut or the kill
command to stop the application.