在Spring中通过@Autowired(required = false)收集的继承接口

时间:2016-01-29 09:43:48

标签: spring

我有两个接口A和B.接口B扩展了接口A.它们都实现了一个方法getId()来识别实现。

public interface A{
Long getId();
}

public interface B extends A {
Long getId();
}

我在整个软件中都有多个A和B实现。

我在Spring中有一个方法用于A和B的对象收集,如下所述:

@Autowired(required = false)
public void collectAImplementations(List<A> implementations) {
Map<Long, A> aMap = new HashMap<>(implementations.size());
for (A a : implementations) {
    aMap.put(a.getId(), a);
 }
}

@Autowired(required = false)
public void collectBImplementations(List<B> implementations) {
Map<Long, B> bMap =  new HashMap<>(implementations.size());
for (B B : implementations) {
   bMap.put(b.getId(), b);
 }
}

问题是我在aMap中实现了A和B的实现。在Spring中有没有办法只在AMap中获得A实现。其中一个方法是在collectAImplementations方法中对B实现进行显式检查。但还有其他解决方案吗?

0 个答案:

没有答案