【发布时间】:2016-06-03 12:42:55
【问题描述】:
我可以为spring指定在自动装配时如何设置地图的键吗?
在下面的示例中,我想以某种方式让 spring 知道 bean 的 getKey() 的返回值应该作为 mapHolder bean 的自动装配映射的键。
public interface MyInterface{
int getKey();
}
@Component
public ImplA implements MyInterface{
@Override
public int getKey(){
return 1;
}
}
@Component
public ImplB implements MyInterface{
@Override
public int getKey(){
return 2;
}
}
@Component
public MapHolder{
@Autowire
private Map<Integer, MyInterface> myAutowiredMap;
public mapHolder(){
}
}
<context:component-scan base-package="com.myquestion">
<context:include-filter type="assignable" expression="com.myquestion.MyInterface"/>
</context:component-scan>
<bean id="mapHolder" class="com.myquestion.MapHolder"/>
【问题讨论】:
标签: java spring spring-mvc dictionary autowired