Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值.
必须在 <key> 标签里定义键
因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素.
可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="personMap" class="com.learn.spring.beans.PersonMap">
<property name="name" value="燕小六"></property>
<property name="age" value="26"></property>
<property name="cars">
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car1"></entry>
<entry key="CC" value-ref="car2"></entry>
</map>
</property>
</bean>
</beans>
package com.learn.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.learn.spring.beans.Book;
import com.learn.spring.beans.Car;
import com.learn.spring.beans.HelloWorld;
import com.learn.spring.beans.Person;
import com.learn.spring.beans.PersonList;
import com.learn.spring.beans.PersonMap;
public class Main {
public static void main(String[] args) {
PersonMap personMap = (PersonMap)ctx.getBean("personMap");
System.out.println(personMap);
}
}