英文:
Refering multiple beans using annotation configuration
问题
当使用 Spring 进行 XML 配置时,我们可以通过编写相似的代码来将多个 beans 的依赖关系引用或添加到一个 bean 中。
<beans>
<bean id="parent" class="com.Parent">
<property name="child1" ref="child1"/>
<property name="child2" ref="child2"/>
</bean>
<bean id="child1" class="com.child">
<property name="name" value="abc"/>
</bean>
<bean id="child2" class="com.child">
<property name="name" value="xyz"/>
</bean>
</beans>
现在我有下面的代码,我想要在每个 home bean 中引用 2 个 car beans。我正在使用 qualifier 来解决歧义问题,是否可以使用 qualifier 或其他方法引用多个 beans 呢?
Configfile.java
@Bean("jeep")
public Car returnJeep()
{
Car c = new Car();
c.setName("Wrangler");
return c;
}
@Bean("volvo")
public Car returnVolvo()
{
Car c = new Car();
c.setName("Volvo");
return c;
}
Home.java
@Autowired
@Qualifier("volvo,jeep") --> 如何在这里引用多个 beans???
private Car car;
@Override
public void print() {
System.out.println("Cheers!!, now you own a new home and a " + car.getName());
}
实现这一目标的一种方法是创建类 car 的另一个引用,并使用 qualifier 指定其他 bean 的名称,这种方法有效,是否还有其他方法可以实现呢?
是否可以使用 @Components 来实现相同的效果,即一个 bean 引用多个 beans?
英文:
When using spring with xml configuration we can refer or add dependencies of multiple beans to one bean by writing the same similar code
<beans>
<bean id ="parent" class="com.Parent">
<property name = "child1" ref = "child1"/>
<property name = "child2" ref = "child2"/>
</bean>
<bean id = "child1" class="com.child">
<property name = "name" value = "abc"/>
</bean>
<bean id = "child2" class="com.child">
<property name = "name" value = "xyz"/>
</bean>
</beans>
Now I have below code I want to refer 2 cars beans per home bean
I'm using qualifier to tackle ambiguity, could we refer multiple beans using qualifier or something else??
Configfile.java
@Bean("jeep")
public Car returnJeep()
{
Car c = new Car();
c.setName("Wrangler");
return c;
}
@Bean("volvo")
public Car returnVolvo()
{
Car c = new Car();
c.setName("Volvo");
return c;
}
Home.java
@Autowired
@Qualifier("volvo,jeep") --> How can I refer multiple beans here????
private Car car;
@Override
public void print() {
System.out.println("Cheers!!, now you own a new home and a "+ car.getName());
}
One way to achieve this is to create one more reference of class car and using qualifier specifying the other bean name which works, is there any other way to achieve this?
Could we to achieve the same by using @Components i.e one bean referring to multiple beans?
答案1
得分: 0
无法将多个 bean 限定符添加到同一属性中。但是,您可以拥有多个相同类型的属性。类似这样:
@Component
public class Home {
private final Car Volvo;
private final Car jeep;
public Home(@Qualifier("volvo") Car volvo, @Qualifier("jeep") Car jeep) {
this.volvo = volvo;
this.jeep = jeep;
}
// 你的代码在这里
}
另一种实现相同目标的方法是使用 Map
,如下所示:
@Component
public class Home {
private final Map<String, Car> carMap;
private final Car jeep;
public Home(Map<String, Car> carMap) {
this.carMap = carMap;
}
// 你的代码在这里
}
使用映射方法,key
将是 qualifier
的名称,而值将是该限定符的实现。
英文:
It's not possible to add multiple bean qualifiers to the same property. However, you can have multiple properties of the same type. Something like this:
@Component
public class Home {
private final Car Volvo;
private final Car jeep;
public Home(@Qualifier("volvo") Car volvo, @Qualifier("jeep") Car jeep) {
this.volvo = volvo;
this.jeep = jeep;
}
// your code goes here
}
Another way to achieve the same is by using a Map
, like this:
@Component
public class Home {
private final Map<String, Car> carMap;
private final Car jeep;
public Home(Map<String, Car> carMap) {
this.carMap = carMap;
}
// your code goes here
}
Using the map approach the key
will be the qualifier
name and the value will be the implementation for that qualifier.
专注分享java语言的经验与见解,让所有开发者获益!
评论