博主
258
258
258
258
专辑

第六节 bean的集合类型注入

亮子 2021-06-13 09:18:30 6601 0 0 0

1、定义对象类

1)、TbScore

package com.shenmazong.pojo;

public class TbScore {
    private Integer math;
    private Integer history;

    public TbScore() {
    }

    public TbScore(Integer math, Integer history) {
        this.math = math;
        this.history = history;
    }

    public Integer getMath() {
        return math;
    }

    public void setMath(Integer math) {
        this.math = math;
    }

    public Integer getHistory() {
        return history;
    }

    public void setHistory(Integer history) {
        this.history = history;
    }

    @Override
    public String toString() {
        return "TbScore{" +
                "math=" + math +
                ", history=" + history +
                '}';
    }
}

2)、TbStudent

package com.shenmazong.pojo;

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

public class TbStudent {

    private String name;
    private List<String> friends;
    private Map<String, TbScore> scoreMap;
    private Properties properties;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getFriends() {
        return friends;
    }

    public void setFriends(List<String> friends) {
        this.friends = friends;
    }

    public Map<String, TbScore> getScoreMap() {
        return scoreMap;
    }

    public void setScoreMap(Map<String, TbScore> scoreMap) {
        this.scoreMap = scoreMap;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "TbStudent{" +
                "name='" + name + '\'' +
                ", friends=" + friends +
                ", scoreMap=" + scoreMap +
                ", properties=" + properties +
                '}';
    }
}

2、注入配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       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 name="tbScore1" class="com.shenmazong.pojo.TbScore" p:math="66" p:history="99"></bean>
    <bean name="tbScore2" class="com.shenmazong.pojo.TbScore" p:math="88" p:history="77"></bean>
    <bean name="tbStudent" class="com.shenmazong.pojo.TbStudent">
        <property name="name" value="武松"></property>
        <property name="friends">
            <list>
                <value>史进</value>
                <value>宋江</value>
                <value>吴用</value>
            </list>
        </property>
        <property name="scoreMap">
            <map>
                <entry key="1" value-ref="tbScore1"></entry>
                <entry key="2" value-ref="tbScore2"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="name">武大郎</prop>
                <prop key="age">33</prop>
            </props>
        </property>
    </bean>

</beans>

3、测试代码

package com.shenmazong;

import com.shenmazong.pojo.TbScore;
import com.shenmazong.pojo.TbStudent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoTest {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        TbStudent tbStudent = app.getBean(TbStudent.class);
        System.out.println(tbStudent);
    }
}

4、测试运行

图片alt