2012-07-31 44 views
3

我有三個類a,b和c。全部繼承相同的類,並且我必須根據鍵在運行時選擇使用哪個類。如何在春天在地圖中找到未找到的鍵的默認值

這裏是春天的代碼,我寫道:

<bean id="ConfigurationAdapterMap" class="abc"> 
    <property name="adapterMap"> 
     <map> 
     <entry key="key1"> 
      <bean class="class1"/> 
     </entry> 
     <entry key="Other"> 
      <bean class="class2"/> 
     </entry> 
     </map> 
    </property>  
    </bean> 

我不想明確地傳遞「其他」,我想返回Class2中,如果給定的密鑰不匹配鍵集中任意鍵。我可以春天做這個嗎?

回答

2

從文檔:

 
You can also explicitly control the exact type of Map that will be 
instantiated and populated via the use of the 'map-class' 
attribute on the element. 

因此,舉例來說,你可以從commons-collectionsDefaultedMap,並嘗試使用它作爲map-class。或者你可以實施你自己的Map並使用它。

+1

我們怎樣才能用彈簧做到這一點? – 2012-07-31 11:04:31