Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| de:modul:ffit:3-jahr:java:learningunits:lu09:b [2025/11/03 23:47] – angelegt apeter | de:modul:ffit:3-jahr:java:learningunits:lu09:b [2025/11/04 01:35] (aktuell) – apeter | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ====== LU09b - Injections | + | ====== LU09b - IoC (Inversion of Control) & DI (Dependency Injection) |
| - | ... | + | Beim Prinzip IoC geht es darum, einem Framework o. Ä. von Aussen Teile des eigenen Codes zu kontrollieren. |
| - | ==== Konstruktor-Injektion ==== | + | Es gibt verschiedene Entwurfsmuster, |
| + | * Strategie-Entwurfsmuster (Strategy design pattern) | ||
| + | * Service-Locator (Service locator) | ||
| + | * Fabrikmethode (Factory method) | ||
| + | * Dependency Injection | ||
| - | ... | + | Bei Dependency Injection wird die Initialisierung eines Objekts nicht im abhängigen Objekt sondern zum Beispiel mittels zentralen Konfiguration ausgelagert. |
| + | Spring Boot erlaubt folgende Arten von Injection für Beans-Klassen (@Component, | ||
| + | ==== Field Injection ==== | ||
| + | <code java> | ||
| + | public class ProjectApiController implements ProjectApi { | ||
| + | |||
| + | @Autowired | ||
| + | private final JwtUtil jwtUtil; | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Anmerkungen: | ||
| + | * Mühsamer zu testen | ||
| + | * Felder können nicht zugleich '' | ||
| + | |||
| + | ==== Constructor Injection ==== | ||
| + | <code java> | ||
| + | public class ProjectApiController implements ProjectApi { | ||
| + | |||
| + | private final JwtUtil jwtUtil; | ||
| + | |||
| + | @Autowired | ||
| + | public ProjectApiController(JwtUtil jwtUtil) { | ||
| + | this.jwtUtil = jwtUtil; | ||
| + | } | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Anmerkungen: | ||
| + | * '' | ||
| + | |||
| + | ==== Setter Injection ==== | ||
| + | <code java> | ||
| + | public class ProjectApiController implements ProjectApi { | ||
| + | |||
| + | @Autowired | ||
| + | private final JwtUtil jwtUtil; | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | Anmerkungen: | ||
| + | * Falls das Feld veränderbar oder optional ist | ||