spring5基础学习之ioc

今天继续上次的iocbean管理的xml注入方式,今天是xmlv注入集合属性

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

<!--    集合类型属性注入-->
    <bean id="stu" class="collectiontype.Stu">
<!--        数组类型属性注入-->
        <property name="courses">
            <array>
                <value>java课程</value>
                <value>spring课程</value>
            </array>
        </property>
<!--        list属性注入-->
        <property name="lsit">
            <list>
                <value>***</value>
                <value>******</value>
            </list>
        </property>
<!--        map属性注入-->
        <property name="maps">
            <map>
                <entry key="Java" value="xxx"></entry>
                <entry key="python" value="yyy"></entry>
            </map>
        </property>
<!--        set属性注入-->
        <property name="sets">
            <set>
                <value>redis</value>
                <value>mysql</value>
            </set>
        </property>
<!--        list对象集合注入-->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>

    </bean>
    <!--        创建多个course对象-->
    <bean id="course1" class="collectiontype.Course">
        <property name="cname" value="Spring5"></property>
    </bean>
    <bean id="course2" class="collectiontype.Course">
        <property name="cname" value="Spring6"></property>
    </bean>
</beans>
View Code
复制代码

类:

复制代码
package collectiontype;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Stu {
    //数组类型属性
    private String[] courses;
    //list类型属性
    private List<String> lsit;
    //map集合类型属性
    private Map<String,String> maps;
    //set集合类型属性
    private Set<String> sets;
    //学生所学多门课程
    private List<Course> courseList;

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

    public void setLsit(List<String> lsit) {
        this.lsit = lsit;
    }

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

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

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

    public void print(){
        System.out.println(Arrays.toString(courses));
        System.out.println(lsit);
        System.out.println(maps);
        System.out.println(sets);
        System.out.println(courseList);
    }
}
View Code
复制代码
复制代码
package collectiontype;

public class Course {
    private String cname;

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Course{" +
                "cname='" + cname + '\'' +
                '}';
    }
}
View Code
复制代码
复制代码
复制代码
package collectiontype;

import java.util.List;

public class Book {
    private List<String> booklist;

    public void setBooklist(List<String> booklist) {
        this.booklist = booklist;
    }
    public void test(){
        System.out.println(booklist);
    }

}
View Code
复制代码
复制代码
posted @   chenghaixinag  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示