10-集合注入(数组、List、Set、Map、Properties)

集合注入是 Spring 中的一种依赖注入方式,可以通过它将一个集合类型(如数组、List、Set、Map、Properties 等)的 Bean 注入到目标 Bean 中。

1、数组注入

<bean id="arrayBean" class="com.example.ArrayBean">
    <property name="array">
        <array>
            <ref bean="bean1"/>
            <ref bean="bean2"/>
            <ref bean="bean3"/>
        </array>
    </property>
</bean>

在上述示例中,<array> 标签对应一个 Array 类型的 Bean,而嵌套在其中的 <ref> 标签则表示要注入的其他 Bean。

<bean id="fruitArray" class="com.example.FruitArray">
    <property name="fruits">
        <array>
            <value>apple</value>
            <value>banana</value>
            <value>orange</value>
        </array>
    </property>
</bean>

在上述示例中,<value> 标签用于设置 FruitArray 类中的 fruits 属性,将三个字符串依次注入到该属性中。

2、List 注入

<bean id="listBean" class="com.example.ListBean">
    <property name="list">
        <list>
            <ref bean="bean1"/>
            <ref bean="bean2"/>
            <ref bean="bean3"/>
        </list>
    </property>
</bean>

在上述示例中,<list> 标签对应一个 List 类型的 Bean,而嵌套在其中的 <ref> 标签则表示要注入的其他 Bean。

<bean id="fruitList" class="com.example.FruitList">
    <property name="fruits">
        <list>
            <value>apple</value>
            <value>banana</value>
            <value>orange</value>
        </list>
    </property>
</bean>

 在上述示例中,<value> 标签用于设置 FruitList 类中的 fruits 属性,将三个字符串依次添加到该属性对应的 List 中。

3、Set 注入

<bean id="setBean" class="com.example.SetBean">
    <property name="set">
        <set>
            <ref bean="bean1"/>
            <ref bean="bean2"/>
            <ref bean="bean3"/>
        </set>
    </property>
</bean>

上述示例中,<set> 标签对应一个 Set 类型的 Bean,而嵌套在其中的 <ref> 标签则表示要注入的其他 Bean。

<bean id="fruitSet" class="com.example.FruitSet">
    <property name="fruits">
        <set>
            <value>apple</value>
            <value>banana</value>
            <value>orange</value>
        </set>
    </property>
</bean>

在上述示例中,<value> 标签用于设置 FruitSet 类中的 fruits 属性,将三个字符串依次添加到该属性对应的 Set 中。

4、Map 注入

<bean id="mapBean" class="com.example.MapBean">
    <property name="map">
        <map>
            <entry key="key1" value-ref="bean1"/>
            <entry key="key2" value-ref="bean2"/>
            <entry key="key3" value-ref="bean3"/>
        </map>
    </property>
</bean>

在上述示例中,<map> 标签对应一个 Map 类型的 Bean,而嵌套在其中的 `<entry>` 标签则表示要注入的键值对。<ref> 标签用于表示注入的其他 Bean,而<value> 标签用于直接注入一个普通值。

<bean id="fruitMap" class="com.example.FruitMap">
    <property name="fruits">
        <map>
            <entry key="apple" value="red"/>
            <entry key="banana" value="yellow"/>
            <entry key="orange" value="orange"/>
        </map>
    </property>
</bean>

 在上述示例中,<entry> 标签用于设置 FruitMap 类中的 fruits 属性,将三个键值对依次添加到该属性对应的 Map 中。

5、Properties 注入

Properties 类型是一种特殊的 Map 类型,它的键和值都是字符串类型。在 XML 配置文件中,可以使用<props> 标签来表示一个 Properties 类型的 Bean,并在其中嵌套<prop> 标签来定义键值对,并在 <prop> 标签中使用<value> 标签来指定键或值对应的字符串。例如:

<bean id="propsBean" class="com.example.PropsBean">
    <property name="props">
        <props>
            <prop key="key1"><value>value1</value></prop>
            <prop key="key2"><value>value2</value></prop>
            <prop key="key3"><value>value3</value></prop>
        </props>
    </property>
</bean>

在上述示例中,<props> 标签对应一个 Properties 类型的 Bean,而嵌套在其中的 <prop> 标签则表示要注入的键值对。<value> 标签用于直接注入一个字符串值。

<bean id="fruitProperties" class="com.example.FruitProperties">
    <property name="colors">
        <util:properties>
            <prop key="apple">red</prop>
            <prop key="banana">yellow</prop>
            <prop key="orange">orange</prop>
        </util:properties>
    </property>
</bean>

在上述示例中,<prop> 标签用于设置 FruitProperties 类中的 colors 属性,将三个键值对依次添加到该属性对应的 Properties 对象中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值