I am confused as to why we would use the @Qualifier and @Primary annotations. If we have to specify which implementation we want to use, and that then becomes the bean which we instantiate, why have multiple implementations of the interface? I might just be misunderstanding
public interface Computer {
// ... some code here
}
@Component
@Primary
public Laptop implements Computer {
// ... some code here
}
@Component
public Desktop implements Computer {
// ... some code here
}
Some RestController
private final Computer pc;
@GetMapping("/")
public String someMethod() {
return pc.name();
}
So I know it will return a Laptop object, that is what I told Spring the Primary implementation is. But what would the use of the other interfaces be? I think the better question would be, can I implement the others using DI/IoC, if it is possible how do you do it? I have been searching for more information in the documentation but could not find anything that answered the question.
I still just dont understand why we would have different implementations of the interface if we are never going to fetch them from the application context and use them. Why even bother?