Spring Autowiring only works with Interface

前端 未结 6 906
慢半拍i
慢半拍i 2020-12-16 13:53

I am quite new to spring framework and came across the following issue.

I have an interface ClassA, which is implemented by classed ClassA1

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 14:28

    You could use the @Qualifier annotation:

    @Autowired
    @Qualifier("class1")
    ClassA classA1;
    
    @Autowired
    @Qualifier("class2")
    ClassA classA2;
    

    Ref: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers

    or the @Resource annotation:

    @Resource(name="class1")
    ClassA classA1;
    
    @Resource(name="class2")
    ClassA classA2;
    

    Ref: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-resource-annotation

提交回复
热议问题