Spring -> IOCxml配置注入Array[],List,Map属性

1.类

package test10month.test1011;

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

/**
 * 功能描述:
 * @version 1.0
 * @className ArrayListMap
 * @author: 罗德
 * @create: 2020-10-11 21:53
 */
public class ArrayListMap {
  private String[] strings;
  private List<String> list;
  private Map<String, String> map;

  @Override
  public String toString() {
    return "ArrayListMap{" + "strings=" + Arrays.toString(strings) + ", list=" + list + ", map=" + map + '}';
  }

  public void setStrings(String[] strings) {
    this.strings = strings;
  }

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

  public void setMap(Map map) {
    this.map = map;
  }
}

2.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="arrayListMap" class="test10month.test1011.ArrayListMap">
        <property name="strings">
            <array>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </array>
        </property>
        <property name="list">
            <list>
             <!--对象为类对象时,使用,ref引用
             
                  <ref bean="这里面是你建的类的bean的id"></ref>
             例如:<bean id="arrayListMap1" class="test10month.test1011.ArrayListMap"/>
                  <bean id="arrayListMap2" class="test10month.test1011.ArrayListMap"/>
                 一个id代表一个对象,同一个类可以创建多个bean,多个id;定义在外部的bean
                 
                -->
                <value>4</value>
                <value>5</value>
                <value></value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="7" value="七"></entry>
                <entry key="8" value="八"></entry>
                <entry key="8" value="九"></entry>
            </map>
        </property>
    </bean>
</beans>

3.测试类

package test;

import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test10month.test1011.ArrayListMap;

/**
 * 功能描述:测试类
 * @version 1.0
 * @className Test
 * @author: 罗德
 * @create: 2020-10-11 15:11
 */
public class ClassTest {
  @Test
  public void test() {
    var context = new ClassPathXmlApplicationContext("ArrayListMapSpring.xml");
    var contextBean = context.getBean("arrayListMap", ArrayListMap.class);
    System.out.println(contextBean);
    /**
     * ArrayListMap{strings=[1, 2, 3], list=[4, 5, 六], map={7=七, 8=九}}
     */
  }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值