How to Disable Web Server in Spring Boot for Non-Web Applications?

When creating a Spring Boot application that’s not intended to function as a web application, you don’t need an embedded web server like Tomcat or Jetty running. Disabling the web server can help reduce resource usage and improve application performance. This guide provides solutions to achieve this.

Solution

Here we explore two main methods to disable the embedded web server in Spring Boot for non-web applications using Kotlin. Each approach includes code examples and the output.

1. Application Configuration Property

Description:

By modifying the application.properties file, you can instruct Spring Boot to disable the web server. This method is easy to implement and is suitable for most non-web applications.

Kotlin Code:

  1. application.properties Configuration
spring.main.web-application-type=none
  1. Main Application Class
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    SpringApplication.run(Application::class.java, *args)
}

Output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.x.x.RELEASE)

No active profile set, falling back to default profiles: default
Started Application in 1.234 seconds (JVM running for 1.567)

Advantages:

  • Simple to configure.
  • No need for code changes in the main application class.

Disadvantages:

  • Requires a valid application.properties file setup.
  • Not suitable if you need to dynamically change the web application type during runtime.

2. Programmatic Configuration in main Function

Description:

Directly configure the SpringApplication instance programmatically to disable the web server. This method provides more control and can be beneficial for complex applications needing dynamic configurations.

Kotlin Code:

  1. Main Application Class
import org.springframework.boot.SpringApplication
import org.springframework.boot.WebApplicationType
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    val application = SpringApplication(Application::class.java)
    application.webApplicationType = WebApplicationType.NONE
    application.run(*args)
}

Output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.x.x.RELEASE)

No active profile set, falling back to default profiles: default
Started Application in 1.234 seconds (JVM running for 1.567)

Advantages:

  • More control over application startup behavior.
  • Suitable for complex applications.

Disadvantages:

  • Slightly more complex compared to configuration properties.
  • Code changes are necessary.

Similar Topics

  1. How to use CommandLineRunner for non-web Spring Boot applications?
  2. Disabling embedded Tomcat server in Spring Boot.
  3. Configuring Spring Boot application as a CLI tool.
  4. Running Spring Boot applications without web server.
  5. Spring Boot: Change the embedded web server.
  6. How to exclude embedded server libraries from Spring Boot application?
  7. Modifying application.yml for disabling web server in Spring Boot.
  8. Differences between spring.main.web-application-type values: SERVLET, REACTIVE, and NONE.
  9. Using Spring Batch in non-web Spring Boot application.
  10. Configuring Spring Boot for serverless applications.
  11. Running Spring Boot applications as background services.
  12. Customizing Spring Boot startup behavior in non-web contexts.
  13. Implementing microservices without an embedded web server in Spring Boot.
  14. Setting up Spring Boot projects as non-web Maven modules.
  15. Disabling HTTP endpoints in Spring Boot Actuator.
  16. Using @Bean annotations in non-web Spring Boot applications.
  17. Spring Boot standalone applications with Quartz scheduling.
  18. Handling external configuration files for non-web Spring Boot applications.
  19. Spring Boot integration with Apache Kafka in non-web applications.
  20. Setting up a non-web Spring Boot application on cloud platforms like AWS Lambda.