Loslegen
Javaneer
Zurück zur Stufe
Stufe 6·Professionelles Java

Spring Boot Principles

IoC and DI, auto-configuration, starters, profiles, and building a REST API.

24 Min. LesezeitExperte

Deutsche Übersetzung in Arbeit

Diese Lektion ist noch nicht ins Deutsche übersetzt und wird daher auf Englisch angezeigt. Der Rest der Seite ist vollständig lokalisiert.

Auf dieser Seite

Spring Boot is the framework most Java developers reach for to build web applications and APIs. It takes the powerful-but-complex Spring framework and makes it delightfully easy to start. This lesson covers the core ideas and walks through building a REST API.

Inversion of Control & Dependency Injection

Spring's heart is an IoC container: it creates and wires your objects (called beans) for you. Instead of new-ing dependencies, you declare what you need and Spring injects it.

@Service
class OrderService {
    private final PaymentGateway gateway;

    OrderService(PaymentGateway gateway) {   // Spring injects this automatically
        this.gateway = gateway;
    }
}
The IoC container is like a stage manager

Actors (your objects) don't fetch their own props and cues - a stage manager hands each performer exactly what they need, exactly when they need it. Spring's container is that stage manager: it constructs your objects and supplies their dependencies, so each class focuses on its own role.

You met Dependency Injection as a principle earlier - Spring makes it automatic. Annotate a class as a component (@Service, @Repository, @Component) and Spring manages it.

Auto-configuration & starters

Spring Boot's superpower is auto-configuration: it inspects your dependencies and configures sensible defaults automatically. Add a starter dependency and things just work.

spring-boot-starter-web   → embedded web server + REST support, ready to go
spring-boot-starter-data-jpa → Hibernate + a datasource, pre-wired

Convention over configuration

Spring Boot assumes sensible defaults so you write almost no configuration for common setups. You only override what's genuinely different about your app. This is why you can have a running web server in a handful of lines.

Building a REST API

Here's a complete, working REST controller:

@RestController
@RequestMapping("/api/products")
class ProductController {

    private final ProductRepository repo;

    ProductController(ProductRepository repo) {
        this.repo = repo;
    }

    @GetMapping
    List<Product> all() {
        return repo.findAll();
    }

    @GetMapping("/{id}")
    Product one(@PathVariable Long id) {
        return repo.findById(id)
                   .orElseThrow(() -> new NotFoundException(id));
    }

    @PostMapping
    Product create(@RequestBody Product product) {
        return repo.save(product);
    }
}
  • @RestController - this class handles HTTP requests and returns JSON.
  • @GetMapping / @PostMapping - map HTTP methods to your Java methods.
  • @PathVariable / @RequestBody - bind URL parts and JSON bodies to parameters.

Spring converts objects to and from JSON automatically. That's a full CRUD API in under 30 lines.

The main application

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

Run it, and an embedded web server starts on port 8080 - no separate server to install.

Profiles & configuration

application.properties (or .yml) holds configuration, and profiles let you vary it per environment (dev, test, prod):

spring.datasource.url=jdbc:postgresql://localhost/shop
server.port=8080

Actuator: production-ready features

Spring Boot Actuator adds ready-made endpoints for health checks, metrics, and info - essential for running in production:

GET /actuator/health   → {"status":"UP"}

Why Spring Boot is everywhere

Spring Boot combines a vast, mature ecosystem with an easy on-ramp: sensible defaults, embedded servers, and starters that wire everything together. That balance of power and simplicity is why it dominates Java web development and why it's a must-have skill for Java engineers.

Quick check

What does Spring Boot's auto-configuration do?

Key takeaways

  • Spring's IoC container creates and wires your objects (beans), injecting dependencies automatically.
  • Mark components with @Service/@Repository/@Component; constructor injection supplies dependencies.
  • Auto-configuration + starters give you working defaults with almost no config.
  • Build REST APIs with @RestController and @GetMapping/@PostMapping; JSON conversion is automatic.
  • @SpringBootApplication + an embedded server means no separate server to install.
  • Profiles vary config per environment; Actuator adds health checks and metrics for production.

An application isn't just code - it's a system. Next, we zoom out to system design.