Interfaces are annotated with @Component annotation in spring IoC/DI. What could be the reason?

前端 未结 2 712
自闭症患者
自闭症患者 2021-01-20 00:03

Some times interfaces are annotated with @Component annotation. Then my obvious reasoning was that classes that implement such interface will be treated as components as we

2条回答
  •  北海茫月
    2021-01-20 00:50

    That is not the case there is no need to adding @component on an interface because it is not a bean as we can't create reference for it. The main part is actually @autowired where you injection the dependecy. For example

    public interface SortAlog(); public class BubbleSortAlgo();

    No we are following the dynamic binding and creating the object of interface but implementation is on the run time.

    So @autowired is the one that will create the object internally and we have @component for bubbleSortAlgo and the only candidate for the injection, so it will get reference from there.

    I hope I was able to make a point here.

提交回复
热议问题