我看到以下错误试图将地图注入bean。我不确定Spring是如何推断出我尝试注入的地图是org.apache.commons.collections.map.LinkedMap类型。我该如何解决这个问题?
[org.apache.commons.collections.map.LinkedMap]到必需的类型 [java.util.LinkedHashMap]属性'asyncConnectorDaoMap';嵌套 异常是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>
答案 0 :(得分:2)
您是否尝试先创建地图,然后将其作为参考传递。
<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