展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

spring5入门(五):基于xml配置管理bean,注入集合

  • 注入数组、集合
# 实体类
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;
    }

    public void test() {
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(sets);
    }
    
}

# 配置bean.xml
    <bean id="stu" class="com.ychen.spring.bean.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>

# 测试方法
    @Test
    public void testCollection1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu", Stu.class);
        stu.test();
    }

# 控制台
[java课程, 数据库课程]
[张三, 小三]
{JAVA=java, PHP=php}
[MySQL, Redis]

Process finished with exit code 0
  • 集合中注入对象
# 课程实体类
public class Course {

    private String cname; //课程名称

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

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

}

# 学生实体类
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;

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

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

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

    public void test() {
        System.out.println(Arrays.toString(courses));
        System.out.println(list);
        System.out.println(maps);
        System.out.println(sets);
        System.out.println(courseList);
    }

}

# 配置bean.xml
    <bean id="stu" class="com.ychen.spring.bean.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>
        <!--注入list集合类型,值是对象-->
        <property name="courseList">
            <list>
                <ref bean="course1"></ref>
                <ref bean="course2"></ref>
            </list>
        </property>
    </bean>

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

# 测试方法
    @Test
    public void testCollection1() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu", Stu.class);
        stu.test();
    }

# 控制台
[java课程, 数据库课程]
[张三, 小三]
{JAVA=java, PHP=php}
[MySQL, Redis]
[Course{cname='Spring5框架'}, Course{cname='MyBatis框架'}]

Process finished with exit code 0
  • 提取集合
# book实体类
public class Book {

    private List<String> list;

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

    public void test() {
        System.out.println(list);
    }

}

# 配置bean.xml
# 添加xmlns:util="http://www.springframework.org/schema/util"  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"
<?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">

    <!--1 提取list集合类型属性注入-->
    <util:list id="bookList">
        <value>易筋经</value>
        <value>九阴真经</value>
        <value>九阳神功</value>
    </util:list>

    <!--2 提取list集合类型属性注入使用-->
    <bean id="book" class="com.ychen.spring.bean.Book" scope="singleton">
        <property name="list" ref="bookList"></property>
    </bean>

</beans>

# 测试方法
    @Test
    public void testCollection2() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book", Book.class);
        book.test();
    }

# 控制台
[易筋经, 九阴真经, 九阳神功]

Process finished with exit code 0
posted @ 2022-04-17 17:49  DogLeftover  阅读(32)  评论(0编辑  收藏  举报