2017-06-05 63 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);