Spring之集合(数组、Map、List、Set)注入方法

Spring之集合(数组、Map、List、Set)注入方法

 

前言

Spring之集合(数组、Map、List、Set)注入方法的步骤

  1. 创建类,定义数组,list,map,set类型属性,并且生成对应的set、get方法。
  2. 在spring配置文件中进行配置。

demo如下:

地址类:

public class Address
{

    private String address;

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

 

学生类:

public class Student {
    private String name; //名字
    private  Address address; //地址



    //集合
    private String[] books;//书
    private List<String>hobbys; //爱好
    private Map<String,String>card;//学生卡
    private Set<String>gams;//游戏

    private String wife;//空指针
    private Properties info; //信息

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", gams=" + gams +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }


    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }
    
    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGams() {
        return gams;
    }

    public void setGams(Set<String> gams) {
        this.gams = gams;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }
}

 

beans.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">

    <!--第一种,普通注入  value  -->
    <bean id="address" class="cn.pojo.Address"></bean>


    <bean id="student" class="cn.pojo.Student">
        <!--第一种,普通注入  value  -->
        <property name="name" value="詹大先生"></property>

        <!--第二种,Bean注入  ref -->
        <property name="address" ref="address"></property>



        <!--数组注入   集合注入从这里开始-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>三国演义</value>
                <value>许三多卖血记</value>
            </array>
        </property>


        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>篮球</value>
                <value>游戏</value>
                <value>足球</value>

            </list>
        </property>


        <!--Map注入   键值对存入-->
        <property name="card">
            <map>
                <entry key="身份证" value="11111111111"></entry>
                <entry key="学生证" value="2027019262"></entry>

            </map>
        </property>


        <!--Set注入-->
        <property name="gams">
            <set>
                <value>英雄联盟</value>
                <value>王者荣耀</value>
                <value>NBA2KOL2</value>
            </set>
        </property>


        <!--null 空串-->
        <property name="wife">
            <null></null>
        </property>


        <!--Properties-->
        <property name="info">
            <props>
                <prop key="username">小明</prop>
                <prop key="password">123456</prop>

            </props>
        </property>

    </bean>

</beans>

 

测试类:

public class MyTest {
    public static void main(String[] args) {

        ApplicationContext context =new ClassPathXmlApplicationContext("beans.xml");

        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());


    }
}

 

 

看完如果对你有帮助,点赞支持!

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值