Autowiring two beans implementing same interface - how to set default bean to autowire?

前端 未结 5 643
南旧
南旧 2020-11-28 17:50

Background:

I have a Spring 2.5/Java/Tomcat application. There is the following bean, which is used throughout the application in many places

publi         


        
5条回答
  •  半阙折子戏
    2020-11-28 18:26

    The reason why @Resource(name = "{your child class name}") works but @Autowired sometimes don't work is because of the difference of their Matching sequence

    Matching sequence of @Autowire
    Type, Qualifier, Name

    Matching sequence of @Resource
    Name, Type, Qualifier

    The more detail explanation can be found here:
    Inject and Resource and Autowired annotations

    In this case, different child class inherited from the parent class or interface confuses @Autowire, because they are from same type; As @Resource use Name as first matching priority , it works.

提交回复
热议问题