Spring--往集合中注入数据

package com.zxh.model;

import java.util.*;

public class Programmer {
    private List<String> cars;
    private Set<String> pats;
    private Map<String,String> infos;
    private Properties mySqlInfos;
    private String[] members;

    public String[] getMembers() {
        return members;
    }

    public void setMembers(String[] members) {
        this.members = members;
    }

    public Properties getMySqlInfos() {
        return mySqlInfos;
    }

    public void setMySqlInfos(Properties mySqlInfos) {
        this.mySqlInfos = mySqlInfos;
    }

    public Map<String, String> getInfos() {
        return infos;
    }

    public void setInfos(Map<String, String> infos) {
        this.infos = infos;
    }

    public Set<String> getPats() {
        return pats;
    }

    public void setPats(Set<String> pats) {
        this.pats = pats;
    }

    public List<String> getCars() {
        return cars;
    }

    public void setCars(List<String> cars) {
        this.cars = cars;
    }

    @Override
    public String toString() {
        return "Programmer{" +
                "cars=" + cars +
                ", pats=" + pats +
                ", infos=" + infos +
                ", mySqlInfos=" + mySqlInfos +
                ", members=" + Arrays.toString(members) +
                '}';
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="programmer" class="com.zxh.model.Programmer">
        <property name="cars">
            <!--list数据的注入-->
            <list>
                <value>ofo</value>
                <value>mobai</value>
                <value>宝马</value>
            </list>
        </property>

        <property name="pats">
            <!--set数据的注入-->
            <set>
                <value>小黑</value>
                <value>小黄</value>
                <value>小白</value>
            </set>
        </property>

        <property name="infos">
            <!--map数据的注入-->
            <map>
                <entry key="name" value="zhangsan"></entry>
                <entry key="age" value="101"></entry>
                <entry key="ip" value="127.0.0.0.1"></entry>
            </map>
        </property>

        <property name="mySqlInfos">
            <!--properties数据的注入-->
            <props>
                <prop key="url">mysql:jdbc:3306</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>

        <property name="members">
            <!--数组数据的注入-->
            <array>
                <value>哥哥</value>
                <value>弟弟</value>
                <value>妹妹</value>
            </array>
        </property>
    </bean>


</beans>

测试类

package com.zxh.test;

import com.zxh.model.Programmer;
import com.zxh.model.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class lesson09 {
    @Test
    public void test() throws Exception {
        
       ApplicationContext ac = new ClassPathXmlApplicationContext("beans9.xml");
       Programmer programmer = (Programmer) ac.getBean("programmer");
       System.out.println(programmer);
    }
}

在这里插入图片描述

发布了35 篇原创文章 · 获赞 7 · 访问量 2124

猜你喜欢

转载自blog.csdn.net/weixin_40605573/article/details/103752937
今日推荐