复杂类型注入

对象 ref

​ 数组

<array>
   <value>/<ref>
 </array>

  

list

<list>
  <value>/<ref>
 </list>

  

map

 <map>
       <entry key="" value=""/value-ref=""></entry>
</map>

  

properties

<props>

  <prop key="">value</prop>

</props>

  

测试代码:

1)定义一个类,属性是各种复杂类型

public class AA {

private int[] arr;

private Drink[] drinkArr;

private List<String> stringList;

private List<Drink> drinkList;

private Map<String,Drink> map;

private Properties properties;
    
    。。。
   set和get方法
   --
   }       

  

注入

<bean id="aa" class="com.test.pojo.AA">
    <property name="arr">
        <array>
            <value>1</value>
            <value>2</value>
            <value>3</value>
        </array>
    </property>

    <property name="drinkArr">
        <array>
            <ref bean="drink_01" />
            <ref bean="drink_02" />
        </array>
    </property>

    <property name="stringList">
        <list>
            <value>tom</value>
            <value>jack</value>
            <value>tony</value>
        </list>
    </property>

    <property name="drinkList">
        <list>
            <ref bean="drink_01" />
            <ref bean="drink_02" />
        </list>
    </property>

    <property name="map">
        <map>
            <entry key="first" value-ref="drink_01" />
            <entry key="second" value-ref="drink_02"  />
        </map>
    </property>

    <property name="properties">
        <props>
            <prop key="driver" >com.mysql.jdbc.Driver</prop>
            <prop key="url" >jdbc:mysql://localhost:3306/taobao</prop>
        </props>
    </property>

</bean>

  

posted @ 2022-09-26 00:14  呆萌老师  阅读(15)  评论(0编辑  收藏  举报