• 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
  • 相关阅读:
    hello word
    HDU 1006 Tick and Tick
    HDU 1005 Number Sequence
    HDU 1004 Let the Balloon Rise
    HDU 1003 Max Sum
    HDU 1002 A + B Problem II
    HDU 1001 Sum Problem
    HDU 1000 A + B Problem
    POJ 2253 Frogger
    POJ 2387 Til the Cows Come Home
  • 原文地址:https://www.cnblogs.com/chenghaixiang/p/16130820.html
Copyright © 2020-2023  润新知