0
0
Spring Bootframework~5 mins

Embedded server concept in Spring Boot

Choose your learning style9 modes available
Introduction

An embedded server lets your Spring Boot app run as a simple program without needing a separate web server setup.

When you want to quickly run a web app without installing a web server like Tomcat separately.
When you want to package your app as a single runnable file for easy deployment.
When you want to simplify development by running the server automatically with your app.
When you want to create microservices that start fast and run independently.
When you want to avoid configuration complexity of external servers.
Syntax
Spring Boot
spring-boot-starter-web dependency includes an embedded server like Tomcat by default.

@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}

The embedded server is included automatically with the web starter dependency.

You don't need to install or configure a separate server like Tomcat or Jetty.

Examples
This adds the embedded Tomcat server to your project automatically.
Spring Boot
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}
This is the main class that starts the embedded server and your app.
Spring Boot
@SpringBootApplication
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }
}
You can change the embedded server port in application.properties.
Spring Boot
server.port=8081
Sample Program

This Spring Boot app runs with an embedded Tomcat server. When you run it, it starts the server automatically. Visiting http://localhost:8080/ shows the message.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class HelloController {
    @GetMapping("/")
    public String hello() {
        return "Hello from embedded server!";
    }
}
OutputSuccess
Important Notes

You can switch embedded servers (Tomcat, Jetty, Undertow) by changing dependencies.

Embedded servers make deployment easier but may use more memory than lightweight servers.

Use application.properties to customize server settings like port or context path.

Summary

Embedded servers let Spring Boot apps run standalone without external server setup.

They simplify development and deployment by bundling the server inside the app.

You control the embedded server via dependencies and configuration files.