Spring5框架2020最新版教程(六)IOC操作Bean管理(基于XML注入集合属性)

IOC操作Bean管理(基于XML注入集合属性)

1、注入数组类型属性

  • 创建一个Student类,定义属性类型
/**
 * @Description: 创建一个学生类
 * @Author: lds
 * @Date: 2020/12/12
 */
public class Student {
    /**
     * 1、数组类型属性(courses课程数组)
     */
    private String[] courses;
    /**
     * 2、list集合类型属性
     */
    private List<String> list;
    /**
     * 3、map集合类型属性
     */
    private Map<String, String> maps;
    /**
     * 4、set集合类型属性
     */
    private Set<String> sets;

    public void setCourses(String[] courses) {
        this.courses = courses;
    }

    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;
    }
}

  • xml配置(标签可以换成标签使用)
 <!--集合类型属性注入-->
    <bean id="student" class="com.lds.springdemo.collectionspackage.Student">
        <!--数组类型属性注入-->
        <property name="courses">
            <array>
                <value>java课程</value>
                <value>英语课程</value>
            </array>
        </property>
    </bean>

2、注入List集合类型属性

<!--集合类型属性注入-->
    <bean id="student" class="com.lds.springdemo.collectionspackage.Student">
        <!--list类型属性注入-->
        <property name="list">
            <list>
                <value>list_001</value>
                <value>list_002</value>
            </list>
        </property>
    </bean>

3、注入Map集合类型属性

<!--集合类型属性注入-->
    <bean id="student" class="com.lds.springdemo.collectionspackage.Student">
        <!--map类型属性注入-->
        <property name="maps">
            <map>
                <entry key="1" value="map_001"></entry>
                <entry key="2" value="map_002"></entry>
            </map>
        </property>
    </bean>

4、注入Set集合类型属性

<!--集合类型属性注入-->
    <bean id="student" class="com.lds.springdemo.collectionspackage.Student">
        <!--set类型属性注入-->
        <property name="sets">
            <set>
                <value>set_001</value>
                <value>set_002</value>
            </set>
        </property>
    </bean>

5、在集合里面设置对象类型值

  • 创建Student类
/**
 * @Description: 创建一个学生类
 * @Author: lds
 * @Date: 2020/12/12
 */
public class Student {

    /**
     * 学生可学多门课程
     */
    private List<Course> courseList;

    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }


    @Override
    public String toString() {
        return "Student{" +
                "courseList=" + courseList +
                '}';
    }
}
  • 创建一个课程类
/**
* @Description:  课程类
* @Author: lds
* @Date: 2020/12/12
*/
public class Course {
    /**
     * 课程的名字
     */
    private String name;

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

    @Override
    public String toString() {
        return "Course{" +
                "name='" + name + '\'' +
                '}';
    }
}
  • XML配置
 <!--注入list集合类型,值是对象-->
    <bean id="student" class="com.lds.springdemo.collectionspackage.Student">
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>
    <!--创建多个course对象-->
    <bean id="course1" class="com.lds.springdemo.collectionspackage.Course">
        <property name="name" value="JAVA"></property>
    </bean>
    <bean id="course2" class="com.lds.springdemo.collectionspackage.Course">
        <property name="name" value="PHP"></property>
    </bean>

6、把集合注入部分提取出来,做一个公用部分

1、在Spring配置文件中引入名称空间 util

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

2、使用util标签完成list集合注入提取

  • 创建一个图书类
/**
 * @Description:图书类
 * @Author: lds
 * @Date: 2020/12/12
 */
public class Book {
    private List<String> bookList;

    public void setBookList(List<String> bookList) {
        this.bookList = bookList;
    }
}
  • xml配置
    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       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
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd">

    <!--提取list集合类型属性注入-->
    <util:list id="bookList">
        <!--类型是String直接写<value>标签
            如果类型是对象引用那么用<ref>标签引入-->
        <value>水浒传</value>
        <value>三国演义</value>
    </util:list>

    <!--提取list集合类型属性注入使用-->
    <bean id="book" class="com.lds.springdemo.collectionspackage.Book">
        <property name="bookList" ref="bookList"></property>
    </bean>
</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的名字是雪冬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值