我刚学习spring不久,在注入HashMap时它的key值是Integer如何注入
private Map<Integer, CartItem> items=new HashMap<Integer, CartItem>();

解决方案 »

  1.   

    <map>
       <entry>
         <key>123</key>
         <value>new CartItem()</value>
       </entry>
    </map>
    对于primitive type的值,spring会自动wrap到合适的类型
      

  2.   

    <bean id="cartItem" class="CartItem的路径"/>     
    <property name="maps">
            <map>
                <entry key="123">
                   <ref bean="cartItem">
                </entry>
                <entry key="..">
                   ....
                </entry>
           </map>
    </property>
      

  3.   

    <bean id="cartService" class="xidian.com.wd.service.impl.CartServiceImpl1">
           <property name="productDao" ref="productDao"></property>
           <property name="items">
              <map>
    <entry key="123"> <!-- key-ref 表示引用类型 -->
    <ref bean="cartItem"/>
    </entry>
    </map>
           </property>
           <constructor-arg index="0" ref="account"></constructor-arg>
      </bean>
    它还是不对
      

  4.   

    <map> 
      <entry> 
        <key>123 </key> 
        <value>new CartItem() </value> 
      </entry> 
    </map> 
    编译通不过 (<key>123 </key> )
      

  5.   


    <property name="map">
    <map>
    <entry key="key1" value="value1"/>
    <entry key="key2" value="value2" />
    <entry key="key3" value="value3"/>
    </map>
    </property>
      

  6.   

    <property name="map">
        <map>
            <entry key="key1" value="value1"/>
            <entry key="key2" value="value2" />
            <entry key="key3" value="value3"/>
        </map>
    </property>