八、注入集合对象


两个entity类

package com.spring.ioc.entity;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Company {
    private Set rooms;
    private Map computers;
    private Properties info;
    
    public Set getRooms() {
        return rooms;
    }
    public void setRooms(Set rooms) {
        this.rooms = rooms;
    }
    public Map getComputers() {
        return computers;
    }
    public void setComputers(Map computers) {
        this.computers = computers;
    }
    public Properties getInfo() {
        return info;
    }
    public void setInfo(Properties info) {
        this.info = info;
    }
    @Override
    public String toString() {
        return "Company{" +
                "rooms=" + rooms +
                ", computers=" + computers +
                ", info=" + info +
                '}';
    }
}
package com.spring.ioc.entity;

public class Computer {
    private String brand;//品牌
    private String type;//类型
    private String sn;//产品序列号
    private Float price;//价格
    public Computer() {
    }
    public Computer(String brand, String type, String sn, Float price) {
        this.brand = brand;
        this.type = type;
        this.sn = sn;
        this.price = price;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getSn() {
        return sn;
    }
    public void setSn(String sn) {
        this.sn = sn;
    }
    public Float getPrice() {
        return price;
    }
    public void setPrice(Float price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Computer{" +
                "brand='" + brand + '\'' +
                ", type='" + type + '\'' +
                ", sn='" + sn + '\'' +
                ", price=" + price +
                '}';
    }
}

配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="c1" class="com.spring.ioc.entity.Computer">
        <constructor-arg name="brand" value="联想">constructor-arg>
        <constructor-arg name="price" value="3085">constructor-arg>
        <constructor-arg name="sn" value="8389283012">constructor-arg>
        <constructor-arg name="type" value="台式机">constructor-arg>
    bean>

    <bean id="company" class="com.spring.ioc.entity.Company">
        
        <property name="rooms">
            <set>
                <value>2001-总裁办value>
                <value>2003-总经理办公室value>
                <value>2010-研发部会议室value>
                <value>2001-总裁办value>
            set>
        property>
        <property name="computers">
            
            <map>
                <entry key="dev-88172" value-ref="c1">entry>
                
                <entry key="dev-88173">
                    <bean class="com.spring.ioc.entity.Computer">
                        <constructor-arg name="brand" value="联想">constructor-arg>
                        <constructor-arg name="price" value="5088">constructor-arg>
                        <constructor-arg name="sn" value="4243433435">constructor-arg>
                        <constructor-arg name="type" value="笔记本">constructor-arg>
                    bean>
                entry>
            map>
        property>

        <property name="info">
            <props>
                <prop key="phone">1008866prop>
                <prop key="address">长江路80号南阳理工学院prop>
                <prop key="website">http://www.xxx.comprop>
            props>
        property>
    bean>
beans>

测试类SpringApplication

package com.spring.ioc;

import com.spring.ioc.entity.Company;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Company company = context.getBean("company", Company.class);
        System.out.println(company);
        String website = company.getInfo().getProperty("website");
        System.out.println(website);
    }
}

运行结果