Spring Boot
Rapid Spring application development
🚀 What is Spring Boot?
Spring Boot simplifies Spring application development by providing auto-configuration, embedded servers, and production-ready features. It eliminates boilerplate configuration and enables rapid development with minimal setup required.
// Complete REST API in just a few lines
@SpringBootApplication
@RestController
public class Application {
@GetMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Spring Boot Features
Auto-Configuration
Automatic setup based on dependencies
@SpringBootApplication
// Auto-configures everything!
Embedded Server
Built-in Tomcat, Jetty, or Undertow
// No external server needed
java -jar myapp.jar
Starters
Pre-configured dependency bundles
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Actuator
Production-ready monitoring features
// Health checks, metrics, etc.
/actuator/health
🔹 Creating a Spring Boot Project
Start with Spring Initializr or create manually:
<!-- pom.xml -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Run with:
mvn spring-boot:run
Server starts on http://localhost:8080
🔹 REST API Example
Build a complete REST API quickly:
@RestController
@RequestMapping("/api/users")
public class UserController {
private List<User> users = new ArrayList<>();
@GetMapping
public List<User> getAllUsers() {
return users;
}
@PostMapping
public User createUser(@RequestBody User user) {
user.setId(System.currentTimeMillis());
users.add(user);
return user;
}
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return users.stream()
.filter(u -> u.getId().equals(id))
.findFirst()
.orElse(null);
}
}
API Endpoints:
GET /api/users - List all users
POST /api/users - Create user
GET /api/users/1 - Get user by ID
🔹 Configuration Properties
Configure your application easily:
# application.properties
server.port=8081
spring.application.name=my-app
logging.level.com.example=DEBUG
# Database configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop
🔸 Using @ConfigurationProperties
@ConfigurationProperties(prefix = "app")
@Component
public class AppProperties {
private String name;
private String version;
// getters and setters
}