🎯 What you will learn

Spring Boot eliminates boilerplate configuration by auto-detecting JARs on the classpath and wiring beans for you. By the end of this lesson you will be able to:

  • Create a Spring Boot 3 project with Maven and the spring-boot-starter-parent
  • Understand @SpringBootApplication and how auto-configuration works
  • Write a @RestController with @GetMapping and return JSON
  • Configure port and application name via application.properties
  • Expose liveness/readiness probes with Spring Boot Actuator
  • Run the app with mvn spring-boot:run and package it as an executable JAR
💼 Interview reality: "How does Spring Boot differ from Spring Framework?" and "What is auto-configuration?" are asked in every Java backend interview. Understanding the startup lifecycle and what @SpringBootApplication combines is the most common gap.

🌍 Real-world scenario

🌍
NairaSwift (Lagos, Nigeria)
From zero to running API in one sprint

Samuel's team at NairaSwift needs to stand up a new micro-service for cross-border NGN payments in one afternoon. Spring Boot's auto-configuration and embedded Tomcat mean they go from blank Maven project to live HTTP endpoint without a single XML servlet config.

🚀 Block 1 — Project setup: pom.xml + application.properties

Start by creating the Maven project skeleton. The spring-boot-starter-parent manages all dependency versions — you never need to specify a version for any Spring starter.

pom.xmlLesson 19 · Spring Boot 3.3.5 parent
# application.properties
spring.application.name=nairaswift server.port=8765 management.endpoints.web.exposure.include=health,info info.app.name=NairaSwift API info.app.version=1.0.0

Auto-configuration tip: Adding spring-boot-starter-web to the classpath is all Spring Boot needs to create a DispatcherServlet, embed Tomcat, and configure Jackson for JSON serialisation — zero XML required.

☕ Block 2 — Main class + HelloController

The entry point is a plain Java main() method. @SpringBootApplication combines three annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.

mvn spring-boot:run
NairaswiftApplication.java + HelloController.javaLesson 19 · @SpringBootApplication, @RestController
verified output
$ curl -s http://localhost:8765/api/hello { "time": "2026-04-26T20:37:37.897383", "status": "UP", "message": "Hello from NairaSwift!" } $ curl -s http://localhost:8765/api/ping { "pong": "true", "service": "nairaswift-api" }

🩺 Block 3 — Actuator health endpoint

Spring Boot Actuator adds production-ready endpoints for monitoring. The /actuator/health endpoint is what Kubernetes liveness and readiness probes call to decide whether to route traffic.

curl http://localhost:8765/actuator/health
application.properties — Actuator additionsLesson 19 · health, info, env endpoints
verified output
{ "status": "UP", "components": { "diskSpace": { "status": "UP", "details": { "total": 494384795648, "free": 160877821952, "threshold": 10485760, "path": "/private/tmp/spring-arc-d/.", "exists": true } }, "ping": { "status": "UP" } } }

Production tip: Never expose all actuator endpoints in production — an attacker with access to /actuator/env or /actuator/heapdump can read secrets. Restrict with management.endpoints.web.exposure.include=health,info and add authentication.

🎤 Interview Q&A

EasyWhat is auto-configuration in Spring Boot?
+

Spring Boot detects JARs on the classpath and automatically creates appropriate beans. For example, seeing spring-boot-starter-web triggers DispatcherServlet, Jackson, and Tomcat setup — no XML required. Auto-configuration classes are listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports and each one is guarded by @Conditional* annotations so beans are only created when their prerequisites are present.

EasyWhat is the difference between @Controller and @RestController?
+

@Controller is for MVC views (Thymeleaf, JSP). @RestController = @Controller + @ResponseBody, meaning every method return value is serialised to JSON and sent as the HTTP response body. You don't need to annotate each method individually with @ResponseBody.

MediumHow does Spring Boot decide which beans to create?
+

@EnableAutoConfiguration reads META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each listed class is a @Configuration with @Conditional* annotations; a bean is only registered when its condition (e.g., a class present on classpath, a property set) evaluates to true. You can see all conditions evaluated at startup by running with --debug flag.

MediumHow do you change the embedded server port at runtime without editing application.properties?
+

Set the environment variable SERVER_PORT, pass --server.port=9090 as a command-line argument, or set the JVM system property -Dserver.port=9090. Spring Boot's relaxed binding resolves all three forms. Command-line arguments override environment variables, which override application.properties.

HardWhat is the Spring Boot initialisation sequence from main() to first HTTP request handled?
+

SpringApplication.run() creates the ApplicationContext, triggers auto-configuration, instantiates and wires all beans, then starts the embedded Tomcat (DispatcherServlet is registered). Finally ApplicationReadyEvent fires. Only then does Tomcat accept incoming connections. The whole process is typically under 2 seconds in a lean service.

📝 Quiz — 5 questions

Question 1 of 5
Which annotation marks the entry point of a Spring Boot application?
Question 2 of 5
What embedded server does spring-boot-starter-web include by default?
Question 3 of 5
What does @RestController do?
Question 4 of 5
Where should you set server.port?
Question 5 of 5
What is the Actuator /actuator/health endpoint used for?
0
/ 5
Quiz complete!

Next: REST Controllers and Routes →

⚠️ Common mistakes

Forgetting scanBasePackages
If your controllers are in a package outside the main class's package, Spring won't find them. Fix: @SpringBootApplication(scanBasePackages = "io.nairaswift").
Port conflict (Address already in use)
Another process is on 8080/8765. Fix: change server.port in application.properties or kill the conflicting process with lsof -i :8765.
Missing spring-boot-maven-plugin
Without it, mvn spring-boot:run won't work and the JAR won't be executable. Always include it in the <build> section of your pom.xml.
Returning a plain String from @RestController
Returning a raw String sends text/plain, not JSON. Wrap in a Map<> or a record/class for proper application/json responses.

✅ Key takeaways

  • @SpringBootApplication + main() = runnable micro-service
  • @RestController + @GetMapping wire HTTP routes in ~5 lines
  • application.properties controls port, logging, and feature flags
  • Actuator /health and /info are free observability endpoints
  • mvn spring-boot:run for dev; mvn package then java -jar for production
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

From Hello World to production API

Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses →