2017-06-05 60 views
0

我有一个XML文件中的以下配置:春:XML配置迁移到注释为UTIL:地图

<util:map id="myService"> 
    <entry key="s1" value-ref="goodService" /> 
    <entry key="s2" value-ref="betterService" /> 
</util:map> 

有没有办法来这个迁移到基于注解的配置,即

@Bean 
public Map<String, MyService> serviceMap() { 
    Map<String, MyService> map = new HashMap<>(); 
    ... 

所以地图是对bean的引用。

回答

1

在配置类自动装配的实例和属性放置到地图

@Autowired 
private GoodService goodService; 
@Autowired 
private BetterService betterService; 

@Bean 
public Map<String, MyService> serviceMap() { 
    Map<String, MyService> map = new HashMap<>(); 
    map.put("s1", goodService); 
    map.put("s2", betterService);