04-Spring5用xml为bean注入集合属性

1.编写测试类

  • 本次测试数组, List, Map, Set的注入
    在这里插入图片描述
    Book类
package com.limi.test;

public class Book {

    private String name;

    public String getName() {
        return name;
    }

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

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

}

User类

package com.limi.test;

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

public class User {


    //数组
    private Integer[] numArray;

    //List
    private List<String> hobyList;

    //Map
    private Map<String, String> friendMap;

    //Set
    private Set<Integer> numSet;

    //存储对象的List
    private List<Book> bookList;

    public Integer[] getNumArray() {
        return numArray;
    }

    public void setNumArray(Integer[] numArray) {
        this.numArray = numArray;
    }

    public List<String> getHobyList() {
        return hobyList;
    }

    public void setHobyList(List<String> hobyList) {
        this.hobyList = hobyList;
    }

    public Map<String, String> getFriendMap() {
        return friendMap;
    }

    public void setFriendMap(Map<String, String> friendMap) {
        this.friendMap = friendMap;
    }

    public Set<Integer> getNumSet() {
        return numSet;
    }

    public void setNumSet(Set<Integer> numSet) {
        this.numSet = numSet;
    }

    public void setBookList(List<Book> bookList) {
        this.bookList = bookList;
    }


    public User() {
    }

    public void say(){
        System.out.println("\n array:");
        for (Integer n: numArray) {
            System.out.print(n+" ");
        }
        System.out.println("\n hobyList:"+hobyList.toString());
        System.out.println("\n friendMap:"+friendMap.toString());
        System.out.println("\n numSet:"+numSet.toString());
        System.out.println(("\n bookList"+bookList.toString()));
    }


}

测试类

package com.limi.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test1(){
        //1.加载bean的xml文件, 以src为根目录
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        //2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.class
        User user = context.getBean("User", User.class);

        //3.使用User对象
        user.say();
    }
}

2.编写xml文件为bean注入集合属性

bean1.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">
    <!--id是唯一标识, class是全类名    -->
    <bean id="User" class="com.limi.test.User">
        <!-- 数组-->
       <property name="numArray">
           <array>
               <value>111</value>
               <value>222</value>
               <value>333</value>
           </array>
       </property>
        
        <!-- List-->
        <property name="hobyList">
            <list>
                <value>篮球</value>
                <value>足球</value>
            </list>
        </property>

        <!-- Map-->
        <property name="friendMap">
            <map>
                <entry key="name" value="andy"></entry>
                <entry key="tel" value="123456"></entry>
            </map>
        </property>

        <!-- Set-->
        <property name="numSet">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </set>
        </property>

        <!-- 存储对象的List-->
        <property name="bookList">
            <list>
                <!-- 可以直接写内部bean-->
                <bean id="book1" class="com.limi.test.Book">
                    <property name="name" value="English"></property>
                </bean>
                <!-- 也可以引用外部bean-->
                <ref bean="book2"></ref>
            </list>
        </property>

    </bean>

    <!--外部bean-->
    <bean id="book2" class="com.limi.test.Book">
        <property name="name" value="Math"></property>
    </bean>
</beans>

测试结果
在这里插入图片描述

3.使用util命名空间注入List集合的注入

和此前说的p命名空间类似, 这里也是需要改一下xml.
灰色部分就是新添加的
在这里插入图片描述
使用方法
在这里插入图片描述

改动后的bean1.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"
       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">

    <!--id是唯一标识, class是全类名    -->
    <bean id="User" class="com.limi.test.User">
        <!-- 数组-->
       <property name="numArray">
           <array>
               <value>111</value>
               <value>222</value>
               <value>333</value>
           </array>
       </property>

        <!-- List-->
        <property name="hobyList">
            <list>
                <value>篮球</value>
                <value>足球</value>
            </list>
        </property>

        <!-- Map-->
        <property name="friendMap">
            <map>
                <entry key="name" value="andy"></entry>
                <entry key="tel" value="123456"></entry>
            </map>
        </property>

        <!-- Set-->
        <property name="numSet">
            <set>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </set>
        </property>

        <!-- 存储对象的List, 直接引用util命名空间里的myBooks-->
        <property name="bookList" ref="myBooks"></property>
    </bean>

    <util:list id="myBooks">
        <bean id="book1" class="com.limi.test.Book">
            <property name="name" value="Math"></property>
        </bean>
        <bean id="book2" class="com.limi.test.Book">
            <property name="name" value="Art"></property>
        </bean>
    </util:list>

</beans>

测试结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值