Spring:使用相同的名称限定不同的类型
我有两个类要使用spring自动连接Spring:使用相同的名称限定不同的类型,spring,dependency-injection,autowired,qualifiers,Spring,Dependency Injection,Autowired,Qualifiers,我有两个类要使用spring自动连接 @Component public class Restaurant { @Autowired @Qualifier("HighClass") private CoffeeMaker coffeeMaker; } 以及: 然后注射: @Bean(name="HighClass") @Scope("prototype") public CoffeeMaker HighClassCoffeeMakerGenerator() {
@Component
public class Restaurant {
@Autowired
@Qualifier("HighClass")
private CoffeeMaker coffeeMaker;
}
以及:
然后注射:
@Bean(name="HighClass")
@Scope("prototype")
public CoffeeMaker HighClassCoffeeMakerGenerator() {
return new CappuccinoMaker();
}
@Bean(name="HighClass")
public int getNumOfSpoons() {
return 3;
}
我想用“高级”来鉴定int和咖啡机。在Guice中,可以使用相同的注释注释不同的类型,并正确地注入它们
似乎在春天这是不允许的。当我尝试注入字段时,我得到一个错误,没有找到所需的bean。我错过了什么吗?春天的豆名是。用于从几个相互竞争的变体中选择bean。在spring中,bean名称是唯一的(请参阅)。您可以使用@Profile实现您的目标。
@Bean(name="HighClass")
@Scope("prototype")
public CoffeeMaker HighClassCoffeeMakerGenerator() {
return new CappuccinoMaker();
}
@Bean(name="HighClass")
public int getNumOfSpoons() {
return 3;
}