微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring5 -- 学习笔记 - 3.IoC容器-操作Bean管理基于xml注入-2

Spring5 – 学习笔记 - 3.IoC容器

   1、注入集合属性

     创建类,定义属性和对应的set方法

public class Student {
    private String[] name;
    private List<String> list;
    private Map<String,String> maps;
    private Set<String> sets;

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

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

    public void print(){
        System.out.println("数组: "+Arrays.toString(name));
        System.out.println("list: "+list);
        System.out.println("map: "+maps);
        System.out.println("set: "+sets);
    }
}

     在Spring配置文件中配置对象的创建、属性的注入。

<!--集合类型属性注入-->
    <bean name="stu1" class="com.tt.collectiontype.bean.Student">

        <!--数组类型属性注入-->
        <property name="name">
            <array>
                <value>张三</value>
                <value>张四</value>
            </array>
        </property>

        <!--list类型属性注入-->
        <property name="list">
            <list>
                <value>20</value>
                <value>22</value>
            </list>
        </property>

        <!--set类型属性注入-->
        <property name="sets">
            <set>
                <value>13812344321</value>
                <value>13956788765</value>
            </set>
        </property>

        <!--map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="课程1" value="Java"></entry>
                <entry key="课程2" value="C"></entry>
            </map>
        </property>
    </bean>

     测试。

    @Test
    public void testStu(){
        ApplicationContext context=new ClasspathXmlApplicationContext("bean4.xml");
        Student student=context.getBean("stu1",Student.class);
        student.print();
    }

     测试结果。

在这里插入图片描述

   2、注入集合属性,值为对象

     在学生类中添加这个属性,以及它的set方法

    //爱好的集合
    private List<Hobby> hobbyList;

    public void setHobbyList(List<Hobby> hobbyList) {
        this.hobbyList = hobbyList;
    }

     创建爱好类。

e = hname;
    }

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

     在Spring配置文件中的bean标签添加这个属性

        <!--集合类型注入属性值为对象-->
        <property name="hobbyList" >
            <list>
                <ref bean="hobby1"></ref>
                <ref bean="hobby2"></ref>
            </list>
        </property>

     创建对象类型。

<!--创建多个对象类型-->
    <bean name="hobby1" class="com.tt.collectiontype.bean.Hobby">
        <property name="hname" value="羽毛球"></property>
    </bean>
    <bean name="hobby2" class="com.tt.collectiontype.bean.Hobby">
        <property name="hname" value="乒乓球"></property>
    </bean>

     测试。

@Test
    public void testStu(){
        ApplicationContext context=new ClasspathXmlApplicationContext("bean4.xml");
        Student student=context.getBean("stu1",Student.class);
        student.print();
    }

     测试结果。

在这里插入图片描述

   3、提取list集合类型属性注入

     在学生类中添加这个属性,以及它的set方法

    private List<String> sexList;

    public void setSexList(List<String> sexList) {
        this.sexList = sexList;
    }

     在Spring配置文件中加入util这个空间空间。

<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

     在Spring配置文件添加 util:list 这个标签

    <!--提取list集合类型属性注入-->
    <util:list id="studentSex">
        <value>男</value>
        <value>男</value>
    </util:list>

     在Spring配置文件中的bean标签添加这个属性

        <!--提前的list集合-->
        <property name="sexList" ref="studentSex"></property>

     测试。

    @Test
    public void testStu(){
        ApplicationContext context=new ClasspathXmlApplicationContext("bean4.xml");
        Student student=context.getBean("stu1",Student.class);
        student.print();
    }

     测试结果。

在这里插入图片描述

   4、最后整个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"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!--提取list集合类型属性注入-->
    <util:list id="studentSex">
        <value>男</value>
        <value>男</value>
    </util:list>


    <!--集合类型属性注入-->
    <bean name="stu1" class="com.tt.collectiontype.bean.Student">

        <!--数组类型属性注入-->
        <property name="name">
            <array>
                <value>张三</value>
                <value>张四</value>
            </array>
        </property>

        <!--list类型属性注入-->
        <property name="list">
            <list>
                <value>20</value>
                <value>22</value>
            </list>
        </property>

        <!--set类型属性注入-->
        <property name="sets">
            <set>
                <value>13812344321</value>
                <value>13956788765</value>
            </set>
        </property>

        <!--map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="课程1" value="Java"></entry>
                <entry key="课程2" value="C"></entry>
            </map>
        </property>

        <!--集合类型注入属性值为对象-->
        <property name="hobbyList" >
            <list>
                <ref bean="hobby1"></ref>
                <ref bean="hobby2"></ref>
            </list>
        </property>

        <!--提前的list集合-->
        <property name="sexList" ref="studentSex"></property>

    </bean>
    <!--创建多个对象类型-->
    <bean name="hobby1" class="com.tt.collectiontype.bean.Hobby">
        <property name="hname" value="羽毛球"></property>
    </bean>
    <bean name="hobby2" class="com.tt.collectiontype.bean.Hobby">
        <property name="hname" value="乒乓球"></property>
    </bean>


</beans>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。