注入集合对象

注入的对象不再是一个单一对象, 而是 List, Set, Map, Properties

定义一个 Computer 类和 Company 类

其关系是 Company 持有 Computer 对象的集合

public class Computer {
    private String brand;
    private String type;
    private String sn;
    private int price;

    public Computer() {
    }

    public Computer(String brand, String type, String sn, int price) {
        this.brand = brand;
        this.type = type;
        this.sn = sn;
        this.price = price;
    }
    
    // getter, setter, toSting 方法省略        
}

Company 还有一个 Properties 类型的数据 info。 Properties 类型和Map 类型相似,不过它的 key 和 value 都为 String 。

public class Company {
    private Set<String> rooms;// 若定义成有序且可以重复则使用List
    private Map<String,Computer> computers;
    private Properties info;
    
    // getter, setter, toSting 方法省略   
}

applicationContext.xml 中实现依赖注入

computer 对象使用构造方法进行依赖注入。 company 对象中的 rooms 使用 < set > 标签实现依赖注入, computers 使用 <map> 标签依赖注入,info 使用 <props> 标签进行依赖注入。

    <bean id="computer1" class="indi.chester.spring.ioc.entity.Computer">
        <constructor-arg name="brand" value="联想"/>
        <constructor-arg name="type" value="台式机"/>
        <constructor-arg name="sn" value="20160000"/>
        <constructor-arg name="price" value="6000"/>
    </bean>

    <bean id="company" class="indi.chester.spring.ioc.entity.Company">
        <property name="rooms">
            <!-- set 标签 底层是 LinkedHashSet,map 标签 底层是 LinkedHashMap, List 标签底层则是 ArrayList -->
            <set>   <!-- 若 rooms 为 List, 则应该使用 <list> 标签 -->
                <value>2001-总裁办公室</value>
                <value>2002-会议室</value>
            </set>
        </property>

        <property name="computers">
            <!-- set 标签 底层是 LinkedHashSet,map 标签 底层是 LinkedHashMap, List 标签底层则是 ArrayList -->
            <map>
                <entry key="dev-88172" value-ref="computer1" />
                <entry key="dev-88173">
                    <bean class="indi.chester.spring.ioc.entity.Computer">
                        <constructor-arg name="brand" value="戴尔"/>
                        <constructor-arg name="type" value="笔记本"/>
                        <constructor-arg name="sn" value="20160001"/>
                        <constructor-arg name="price" value="5000"/>
                    </bean>
                </entry>
            </map>
        </property>

        <property name="info">
            <props>
                <prop key="phone">0755-12345678</prop>
                <prop key="address">深圳市南山区XX路XX号</prop>
            </props>
        </property>
    </bean>

总结一下:

注入 List:

<bean id="..." class="...">
    <property name="someList">
        <list>    
            <value>具体值</value>
            <ref bean="otherBeanId"></ref>
        </list>
    </property>
</bean>

注入 Set

<bean id="..." class="...">
    <property name="someSet">
        <list>    
            <value>具体值</value>
            <ref bean="otherBeanId"/>
        </list>
    </property>
</bean>

注入 Map

<bean id="..." class="...">
    <property name="someMap">
        <map>    
            <entry key="k1" value="v1" />
            <entry key="k2" value-ref="beanId" />
        </map>
    </property>
</bean>

注入 Properties

<bean id="..." class="...">
    <property name="someProperties">
        <props>    
            <entry key="k1" value="v1" />
            <entry key="k2" value="v2" />
        </props>
    </property>
</bean>

Last updated

Was this helpful?