8.注入集合对象

两个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<String> rooms;
    private Map<String,Computer> computers;
    private Properties info;
    
    public Set<String> getRooms() {
        return rooms;
    }
    public void setRooms(Set<String> rooms) {
        this.rooms = rooms;
    }
    public Map<String, Computer> getComputers() {
        return computers;
    }
    public void setComputers(Map<String, Computer> 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">
        <!--注入list或set集合,默认使用ArrayList或LinkedHashSet类型-->
        <property name="rooms">
            <set>
                <value>2001-总裁办</value>
                <value>2003-总经理办公室</value>
                <value>2010-研发部会议室</value>
                <value>2001-总裁办</value>
            </set>
        </property>
        <property name="computers">
            <!--注入Map集合,默认使用LinkedHashMap类型-->
            <map>
                <entry key="dev-88172" value-ref="c1"></entry>
                <!--内置bean设置ref,需确保这个bean不会被其他对象关联-->
                <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">1008866</prop>
                <prop key="address">长江路80号南阳理工学院</prop>
                <prop key="website">http://www.xxx.com</prop>
            </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);
    }
}
复制代码

运行结果

 

posted @   南风知君  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示