public class Student { private String name; private Map<String,String> map; public Student() { } public Student(String name, Map<String, String> map) { this.name = name; this.map = map; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", map=" + map + ‘}‘; } }
<bean id="student" class="com.orz.spring.bean.Student"> <property name="name" value="李华"/> <property name="map"> <map> <entry key="key1" value="数据库原理"/> <entry key="key2" value="java编程"/> </map> </property> </bean>
@Test public void test1() { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml"); Student student = applicationContext.getBean("student", Student.class); System.out.println(student); }
Student{name=‘李华‘, map={key1=数据库原理, key2=java编程}}
Spring-IoC-DI-基于xml的依赖注入-使用set方法进行注入(案例六:注入Map集合属性)
原文:https://www.cnblogs.com/orzjiangxiaoyu/p/13836190.html