0

I am trying to understand how spring dependency injection works. Based on my understanding, if we declare a class as a bean with @Component annotation and use the @Autowired annotation for that bean in another class where the dependency needs to be injected, it will inject that bean as a dependency. For e.g.

Let's say we have IService interface and Service1 implements Iservice, and we declare Service1 as a component. If we have a main class with Iservice reference, and we do autowired annotation on Iservice reference, Service1 will be injected into Iservice reference.

However, my question is that if there are two classes Service1 and Service2 that implement Iservice,and we use autowiring. How will spring decide which Service class to use for dependency injection?

Max
  • 9,100
  • 25
  • 72
  • 109

1 Answers1

0

If you have more than 1 class for autowire candidate, you should use @Qualifier along with @Autowired

Aleksey Kurkov
  • 323
  • 2
  • 13