@Autowired实际上适合这种情况。你可以自动连接特定的类(实现)或使用接口。
考虑以下示例:
public interface Item {}@Component("itemA")public class ItemImplA implements Item {}@Component("itemB")public class ItemImplB implements Item {}现在,你可以根据
@Component注释值选择对象的名称,从而选择使用其中一种实现方式
像这样:
@Autowiredprivate Item itemA; // ItemA@Autowiredprivate Item itemB // ItemB
要多次创建同一实例,可以使用@Qualifier批注指定将使用的实现:
@Autowired@Qualifier("itemA")private Item item1;如果需要使用一些特定的构造函数参数实例化这些项目,则必须为其指定XML配置文件。在这里可以找到有关使用qulifiers和自动装配的不错的教程。



