2 Hours

5. Introduction to Spring Boot

Getting Started
Sandbox Note: The editor validates your Java and Spring Boot syntax only. It does NOT start a Spring Boot server. When you see Compilation successful (exit code 0), your code is syntactically correct. Copy it to your local IDE and run ./mvnw spring-boot:run to see it running on http://localhost:8080.

Spring Boot is a framework that makes it easy to create production-grade Spring applications. It removes the complexity of traditional Spring configuration by providing auto-configuration, embedded servers, and production-ready features out of the box.

What Problem Does Spring Boot Solve?

Traditional Spring applications required extensive XML configuration. You had to manually configure web.xml, set up component scanning, define view resolvers, and manage many other details. Spring Boot eliminates this boilerplate.

Text
Traditional Spring (Pre-Boot):
- Hundreds of lines of XML configuration
- Manual dependency management
- External server deployment (Tomcat)
- Complex setup

Spring Boot:
- Zero XML (annotations only)
- Auto-configuration based on dependencies
- Embedded Tomcat/Jetty/Undertow
- Spring Initializr for quick start

The Simplest Spring Boot Application

A complete Spring Boot web application can be as small as this:

Java
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
@RestController
public class DemoApplication {

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

    @GetMapping("/")
    public String home() {
        return "Hello Spring Boot!";
    }
}
To run this code locally: Create a Spring Boot project via start.spring.io, copy this code, then run ./mvnw spring-boot:run. Visit http://localhost:8080.

Understanding the Annotations

Three annotations power this simple application:

@SpringBootApplication

This is a convenience annotation that combines three others:

  • @Configuration: Marks the class as a source of bean definitions
  • @EnableAutoConfiguration: Tells Spring Boot to automatically configure based on dependencies
  • @ComponentScan: Tells Spring to scan for components in the current package

@RestController

Marks the class as a REST controller — each method returns data directly (not a view name). It combines @Controller and @ResponseBody.

@GetMapping

Maps HTTP GET requests to the annotated method. The path / means the root URL.

Spring Boot Starters

Starters are dependency descriptors that bundle commonly used dependencies. Adding one starter gives you many related libraries.

Text
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- This single starter includes:
     - spring-webmvc (REST APIs)
     - jackson (JSON parsing)
     - tomcat (embedded server)
     - spring-core, spring-context, etc. -->
Common Starters: spring-boot-starter-data-jpa, spring-boot-starter-security, spring-boot-starter-test, spring-boot-starter-actuator.

Spring Boot Auto-Configuration

Auto-configuration is Spring Boot's killer feature. Based on the JARs in your classpath, Spring Boot automatically configures beans.

Java
// If you have spring-data-jpa on classpath → Auto-configures DataSource, EntityManagerFactory, TransactionManager
// If you have spring-webmvc on classpath → Auto-configures DispatcherServlet, Jackson
// If you have PostgreSQL driver → Auto-configures DataSource for PostgreSQL

You can override auto-configuration by defining your own beans. Spring Boot respects your custom beans over its defaults.

Spring Boot Application Properties

Configuration is centralized in application.properties or application.yml.

Text
# application.properties
server.port=9090
server.servlet.context-path=/api
spring.application.name=my-app

# Logging
logging.level.root=INFO
logging.level.com.example=DEBUG

# Database (example)
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=secret
Text
# application.yml
server:
  port: 9090
  servlet:
    context-path: /api
spring:
  application:
    name: my-app
  datasource:
    url: jdbc:postgresql://localhost:5432/mydb
    username: postgres
    password: secret

Building and Running Locally

Bash
# Using Maven wrapper
./mvnw spring-boot:run

# Build JAR and run
./mvnw clean package
java -jar target/my-app-1.0.0.jar

# Using Gradle wrapper
./gradlew bootRun
./gradlew build
java -jar build/libs/my-app-1.0.0.jar
Local Testing: After building the JAR, you can run it anywhere with Java installed — that's the beauty of Spring Boot's packaging.

Knowledge Check

Ready to test your understanding of 5. Introduction to Spring Boot?

Back