zoukankan      html  css  js  c++  java
  • spring07

    7、IOC 操作 Bean 管理——xml 注入集合属性

    1、注入数组类型属性 2、注入 List 集合类型属性 3、注入 Map 集合类型属性

    //(1)创建类,定义数组、list、map、set 类型属性,生成对应 set 方法
    public class Stu {
    //1 数组类型属性
    private String[] courses;
    //2 list集合类型属性
    private List<String> list;
    //3 map集合类型属性
    private Map<String,String> maps;
    //4 set集合类型属性
    private Set<String> sets;

    public void setSets(Set<String> sets) {
    this.sets = 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;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <!--(2)在 spring 配置文件进行配置-->
    <bean id="stu" class="com.atguigu.spring5.collectiontype.Stu">
    <!--数组类型属性注入-->
    <property name="courses">
    <array>
    <value>java课程</value>
    <value>数据库课程</value>
    </array>
    </property>
    <!--list类型属性注入-->
    <property name="list">
    <list>
    <value>张三</value>
    <value>小三</value>
    </list>
    </property>
    <!--map类型属性注入-->
    <property name="maps">
    <map>
    <entry key="JAVA" value="java"></entry>
    <entry key="PHP" value="php"></entry>
    </map>
    </property>
    <!--set类型属性注入-->
    <property name="sets">
    <set>
    <value>MySQL</value>
    <value>Redis</value>
    </set>
    </property>
    </bean>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    ​ 8、在集合里面设置对象类型值

    //学生所学多门课程
    private List<Course> courseList;//创建集合
    public void setCourseList(List<Course> courseList) {
    this.courseList = courseList;
    }
    1
    2
    3
    4
    5

    <!--创建多个course对象-->
    <bean id="course1" class="com.atguigu.spring5.collectiontype.Course">
    <property name="cname" value="Spring5框架"></property>
    </bean>
    <bean id="course2" class="com.atguigu.spring5.collectiontype.Course">
    <property name="cname" value="MyBatis框架"></property>
    </bean>

    <!--注入list集合类型,值是对象-->
    <property name="courseList">
    <list>
    <ref bean="course1"></ref>
    <ref bean="course2"></ref>
    </list>
    </property>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <!--第一步:在 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" <!--添加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"> <!--添加util名称空间-->

    <!--第二步:使用 util 标签完成 list 集合注入提取-->
    <!--把集合注入部分提取出来-->
    <!--1 提取list集合类型属性注入-->
    <util:list id="bookList">
    <value>易筋经</value>
    <value>九阴真经</value>
    <value>九阳神功</value>
    </util:list>

    <!--2 提取list集合类型属性注入使用-->
    <bean id="book" class="com.atguigu.spring5.collectiontype.Book" scope="prototype">
    <property name="list" ref="bookList"></property>
    </bean>
    ————————————————
    版权声明:本文为CSDN博主「来点淦货」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_45496190/article/details/107059038

  • 相关阅读:
    AS3 CookBook学习整理(四)
    方维分享系统修改瀑布流页面,包括的文件
    PHP 采集淘宝店的评论插件使用说明
    方维系统,在tip.htm和inc\u\u_menu.htm调用账号绑定状态,已绑定的可链接到该用户在对应网站的地址
    php采集淘宝店的评论,php采集淘宝店铺的所有评论的实现
    方维分享系统二次开发,新加一个模块
    方维分享系统,个人中心杂志社显示我的、关注的、推荐的数量
    采集 淘宝店铺的所有评论内容 的思路
    方维分享系统写一个全局函数,能获取用户详情
    方维分享系统 全局变量 $_FANWE 的用处
  • 原文地址:https://www.cnblogs.com/huaobin/p/14892009.html
Copyright © 2011-2022 走看看