1. 程式人生 > >spring bean xml中集合類屬性的輸入寫法

spring bean xml中集合類屬性的輸入寫法

下例的xml中list對應於java中的陣列,List或者Set


1) list對應的為Instrument[]或者List<Instrument>/Collection<Instrument>
<bean id="hank" class="springidol.OneManBand">
    <property name="instruments">
        <list>
            <ref bean="guitar"/>
            <ref bean="piano"/>
            <ref bean="saxophone"/>
        </list>
    </property>
  </bean>


2) list為String[], List<String>, Collection<String>
  <bean id="nino" class="springidol.OneBoyBand">
    <property name="instruments">
        <list>
            <value>guitar</value>
            <value>piano</value>
            <value>saxophone</value>
        </list>
    </property>
  </bean>


3) map為Map<String, Instrument>  
  <bean id="thomas" class="springidol.OneWomanBand">
    <property name="instruments">
        <map>
            <entry key="guitar" value-ref="guitar"/>
            <entry key="piano" value-ref="piano"/>
            <entry key="saxophone" value-ref="saxophone"/>
        </map>
    </property>
  </bean>


4) props為Properties<String, String>    
  <bean id="putina" class="springidol.OneGirlBand">
    <property name="instruments">
        <props>
            <prop key="guitar">guitar</prop>
            <prop key="piano">piano</prop>
            <prop key="saxophone">saxophone</prop>
        </props>
    </property>
  </bean>