2016-03-03 99 views
2

我看到下面的錯誤,它試圖將一個映射注入到一個bean中。我不確定Spring是如何推斷我試圖注入的映射是org.apache.commons.collections.map.LinkedMap類型的。我該如何解決這個問題?彈簧注入映射問題

[org.apache.commons.collections.map.LinkedMap]所需類型 [java.util.LinkedHashMap中用於屬性 'asyncConnectorDaoMap';嵌套0​​例外是java.lang.IllegalArgumentException異常:不能類型的值轉換 [org.apache.commons.collections.map.LinkedMap]所需 類型[java.util.LinkedHashMap中]

Spring配置

<bean id="fulfillment.management.AsyncConnectorFactory" class="com.service.async.AsyncConnectorFactory">  <property name="asyncConnectorDaoMap">   <map key-type="java.lang.String" value-type="com.service.async.AsyncConnectorDao"> 
        <entry key="ACCOUNT_MANAGEMENT_CONTROLLER" value-ref="core.commons.AsyncConnectorDao"></entry> 
        <entry key="CUSTOMER_SERVICE" value-ref="core.commons.AsyncConnectorDao"></entry> 
        <entry key="ACCOUNT_SERVICE" value-ref="core.commons.AsyncConnectorDao"></entry> 
        <entry key="LOCATION_SERVICE" value-ref="core.commons.AsyncConnectorDao"></entry>  </map>  </property> </bean> 

下面的代碼

import java.util.LinkedHashMap; 


public class AsyncConnectorFactory { 

    private LinkedHashMap<String, AsyncConnectorDao> asyncConnectorDaoMap; 

    public LinkedHashMap<String, AsyncConnectorDao> getAsyncConnectorDaoMap() { 
     return asyncConnectorDaoMap; 
    } 

    public void setAsyncConnectorDaoMap(
      LinkedHashMap<String, AsyncConnectorDao> asyncConnectorDaoMap) { 
     this.asyncConnectorDaoMap = asyncConnectorDaoMap; 
    } 

    public AsyncConnectorDao getAsyncConcurrentConnectorDao(String serviceIdentifier) { 
     return asyncConnectorDaoMap.get(serviceIdentifier); 
    } 
} 

修改工作配置

<bean id="fulfillment.management.AsyncConnectorFactory" class="com.service.async.AsyncConnectorFactory"> 
     <property name="asyncConnectorDaoMap" ref="asyncConnectorDaoMap1"/> 
</bean> 

<util:map id="asyncConnectorDaoMap1" map-class="java.util.LinkedHashMap"> 
      <entry key="ACCOUNT_MANAGEMENT_CONTROLLER" value-ref="core.commons.AsyncConnectorDao"></entry> 
      <entry key="CUSTOMER_SERVICE" value-ref="core.commons.AsyncConnectorDao"></entry> 
      <entry key="ACCOUNT_SERVICE" value-ref="core.commons.AsyncConnectorDao"></entry> 
      <entry key="LOCATION_SERVICE" value-ref="core.commons.AsyncConnectorDao"></entry> 

回答

1

你試過先創建地圖,然後把它作爲參考。

<util:map id="p:asyncConnectorDaoMap" map-class="java.util.LinkedHashMap"> 
    <entry key="ACCOUNT_MANAGEMENT_CONTROLLER" value-ref="asyncConnectorDao"/> 
    <entry key="CUSTOMER_SERVICE" value-ref="asynConnectorService"/ 
</util:map> 

<bean id="asyncConnectorDao" class="core.commons.AsyncConnectorDao" /> 
<bean id="asynConnectorService" class="core.commons.AsyncConnectorService" /> 


<bean id="fulfillment.management.AsyncConnectorFactory" class="com.service.async.AsyncConnectorFactory">  
    <property name="asyncConnectorDaoMap" ref="asyncConnectorDaoMap"/>  
</bean> 

確保您的DAO和服務bean已經創建。考慮以及使用MapFactoryBean

使用的模式,對於使用UTIL

http://www.springframework.org/schema/util 
http://www.springframework.org/schema/util/spring-util-3.0.xsd 
+0

謝謝Koitoer的,是有什麼問題我已經定義的方式嗎? –

+0

我認爲問題在於value-ref,似乎是指向一個類而不是一個bean – Koitoer

+0

謝謝Kotoer,現在就開始工作。我粘貼了工作配置。 –