本文主要是介绍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;@Overridepublic 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 {@Testpublic 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=九}}*/}
}
这篇关于Spring -> IOCxml配置注入Array[],List,Map属性的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!