为了补充 Robert Munteanu 的回答并供将来参考,以下是使用@Autowired
and @Qualifier
on 构造函数的外观:
public class FooImpl implements Foo {
private final Bar bar;
private final Baz baz;
@org.springframework.beans.factory.annotation.Autowired
public Foo(Bar bar, @org.springframework.beans.factory.annotation.Qualifier("thisBazInParticular") Baz baz) {
this.bar = bar;
this.baz = baz;
}
}
In this example, bar
is just autowired (i.e. there is only one bean of that class in the context so Spring knows which to use), while baz
has a qualifier to tell Spring which particular bean of that class we want injected.