RU | EN | DE

1. Was ist Spring Boot

  • Spring = ein großer Framework, erfordert manuelle Konfiguration (XML/Java Config).
  • Spring Boot = “Spring mit Batterien”:
    • automatische Konfiguration,
    • eingebauter Tomcat (oder Jetty/Undertow),
    • Starter-System (Abhängigkeitssets),
    • fertige Projektstruktur,
    • Monitoring (Actuator). ⚡ Du brauchst nur minimalen Code → und es funktioniert schon.

2. Minimales Anwendung

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • @SpringBootApplication = kombiniert drei Annotationen:
    • @Configuration (Java Config),
    • @EnableAutoConfiguration (automatische Konfiguration),
    • @ComponentScan (Paketscanning).

3. Starters (Abhängigkeiten)

Spring Boot Starters = vorab zusammengestellte Abhängigkeitssets.

📄 Beispiele (in pom.xml):

//...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
//...

📌 Beliebte Starters:

  • spring-boot-starter-web → REST, MVC, JSON
  • spring-boot-starter-data-jpa → Hibernate + JPA
  • spring-boot-starter-security → Spring Security
  • spring-boot-starter-thymeleaf → HTML-Templates
  • spring-boot-starter-test → JUnit, Mockito

4. Konfiguration (application.properties, application.yml)

📄 src/main/resources/application.properties

server.port=8081
spring.datasource.url=jdbc:postgresql://localhost:5432/demo
spring.datasource.username=demo
spring.datasource.password=secret

📄 application.yml (bequemer für verschachtelte Strukturen)

server:
  port: 8081
spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/demo
    username: demo
    password: secret

5. Actuator (Monitoring und Health-Check)

📄 Wir fügen eine Abhängigkeit hinzu:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

📄 Einstellungen:

management.endpoints.web.exposure.include=health,info,metrics

📌 Verfügbare Endpunkte:

  • /actuator/health → Zustand
  • /actuator/info → Informationen
  • /actuator/metrics → Metriken
  • /actuator/env → Umgebung

6. REST API (Spring Boot + MVC)

@RestController
@RequestMapping("/api/users")
class UserController {
    @GetMapping
    public List<String> getUsers() {
        return List.of("Alice", "Bob");
    }
 
    @PostMapping
    public String createUser(@RequestBody String name) {
        return "Created user: " + name;
    }
}

📌 Annotationen:

  • @RestController = @Controller + @ResponseBody
  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping
  • @PathVariable, @RequestParam, @RequestBody

7. Fehlerbehandlung

@RestControllerAdvice
class GlobalExceptionHandler {
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handle(IllegalArgumentException ex) {
        return ResponseEntity.badRequest().body("Error: " + ex.getMessage());
    }
}

8. Profile (dev, test, prod)

📄 application-dev.properties

server.port=8081

📄 application-prod.properties

server.port=80

Starten mit Profil:

mvn spring-boot:run -Dspring-boot.run.profiles=prod

9. Logging

Standardmäßig verwendet Spring Boot Logback.

📄 In application.properties:

logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=app.log

10. Best Practices

  • ✅ Starte das Projekt über Spring Initializr
  • ✅ Verwende application.yml (lesbarer als properties).
  • ✅ Teile Konfigurationen in dev/test/prod.
  • ✅ Füge Actuator für Monitoring hinzu.
  • ✅ Verwende @RestControllerAdvice für zentrale Fehlerbehandlung.

📌 Zusammenfassung zu Spring Boot

  • @SpringBootApplication = Einstiegspunkt
  • Starters = Abhängigkeitssets
  • application.yml = Konfiguration
  • Actuator = Monitoring
  • REST-Controller = einfach und schnell
  • Profile = flexible Konfiguration