In Spring Boot 2 kannst du Abhängigkeiten in einem Controller mithilfe der Annotation `@Autowired` injizieren. Hier ist ein einfaches Beispiel, wie du dies tun kannst: ```java import org.springframework.beans.factory.annotation.Autowired; import.springframework.web.bind.annotationMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } @GetMapping("/hello") public String sayHello() { return myService.getHelloMessage(); } } ``` In diesem Beispiel wird der `MyService` in den `MyController` injiziert. Der Konstruktor des Controllers ist mit `@Autowired` annotiert, was Spring Boot anweist, die Abhängigkeit automatisch zu injizieren. Alternativ kannst du auch Feldinjektion verwenden, aber Konstruktorinjektion wird oft bevorzugt, da sie die Abhängigkeiten explizit macht und besser testbar ist. Hier ist ein Beispiel für Feldinjektion: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class MyController { @Autowired private MyService myService; @GetMapping("/hello") public String sayHello() { return myService.getHelloMessage(); } } ``` Beide Ansätze sind gültig, aber Konstruktorinjektion wird in der Regel als Best Practice angesehen.