Spring Boot: From Basics to Production · Spring Core and Spring Boot Fundamentals
Creating a Spring Boot Project
Generate a project with Spring Initializr, run it and understand its structure.
Spring Boot builds on the Spring Framework with opinionated defaults: auto-configuration, embedded servers (Tomcat by default), starter dependencies and production features (Actuator). Generate a project at start.spring.io (Java 21, Maven or Gradle, plus the Web, Validation, Data JPA and Actuator dependencies), then run it with ./mvnw spring-boot:run or ./gradlew bootRun. The @SpringBootApplication class enables component scanning and auto-configuration for its package and sub-packages.
package com.example.shop;
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 ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}
@RestController
class HelloController {
@GetMapping("/hello")
String hello() {
return "Hello, Spring Boot";
}
}
curl https://start.spring.io/starter.zip -d dependencies=web,validation,data-jpa,actuator,postgresql \
-d javaVersion=21 -d type=maven-project -d groupId=com.example -d artifactId=shop -o shop.zip
./mvnw spring-boot:run # http://localhost:8080/hello
Common pitfalls
- Placing classes outside the main class's package, so component scanning misses them.
- Adding many starters 'just in case' — each brings auto-configuration and startup cost.
- Using an unsupported Spring Boot version (check the support timeline).
Key points
- start.spring.io generates projects
- @SpringBootApplication = config + scanning + auto-config
- Embedded server, runnable JAR
Further reading: Spring Boot reference documentation
Practice questions
What does @SpringBootApplication combine?
Answer
@SpringBootConfiguration (a @Configuration class), @EnableAutoConfiguration and @ComponentScan of the class's package and sub-packages.
Why might a @RestController not be picked up?
Answer
It's in a package that isn't under the main application class's package, so component scanning doesn't find it.
Open in the app to mark lessons complete, take notes and track your progress.