Я пытался спровоцировать сбой моего приложения, когда было два определения Bean-компонента одного типа, но приложение начало run.
Я придумал такую схему:
Это мой простой интерфейс
Код: Выделить всё
public interface Demo {
String getMessage();
}
Код: Выделить всё
@Service
public class DemoService implements Demo{
private final DataRepo repo;
DemoService(DataRepo repo) {
this.repo = repo;
}
@Override
public String getMessage() {
return "Demo message from Demo Service";
}
}
Код: Выделить всё
@Service
public class DemoSecondService implements Demo{
private final DataRepo repo;
DemoSecondService(DataRepo repo) {
this.repo = repo;
}
@Override
public String getMessage() {
return "Demo message from DemoSecondService";
}
}
Код: Выделить всё
@RestController
@RequestMapping("/demo")
public class DemoController {
private final Demo demoSecondService; //(1)
public DemoController(Demo demoSecondService) {
this.demoSecondService = demoSecondService;
}
@GetMapping
ResponseEntity hello() {
return new ResponseEntity("Hello", HttpStatus.OK);
}
@GetMapping("/more")
ResponseEntity moreHello() {
String message = demoSecondService.getMessage();
return new ResponseEntity(message, HttpStatus.OK);
}
Код: Выделить всё
Parameter 0 of constructor in com.example.demo.domain.controller.DemoController required a single bean, but 2 were found:
- demoSecondService: defined in file
- demoService: defined in file
Выполняет ли SpringBoot выбор компонента на основе имени переменной, чтобы мы могли использовать его без использования @ Квалификация?
Подробнее здесь: https://stackoverflow.com/questions/786 ... iable-name
Мобильная версия