在spring框架中為Map注入屬性
1map映射的對象創建

package com; /** * Map集合在spring中的使用測試 */ public class User { private int id; private String name; private String pwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User[id=" + id + ", name=" + name + ", pwd=" + pwd + "]"; } public User(int id, String name, String pwd) { super(); this.id = id; this.name = name; this.pwd = pwd; } public User() { super(); // TODO Auto-generated constructor stub } }
2.Map的使用者

package com; import java.util.Map; /** * Map 集合在spring框架中的使用測試 */ public class MapDemo { private int id; private String name; private String pwd; private Map<String,User> user; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public Map<String, User> getUser() { return user; } public void setUser(Map<String, User> user) { this.user = user; } @Override public String toString() { return "MapDemo [id=" + id + ", name=" + name + ", pwd=" + pwd + ", user=" + user + "]"; } public MapDemo() { super(); // TODO Auto-generated constructor stub } public MapDemo(int id, String name, String pwd, Map<String, User> user) { super(); this.id = id; this.name = name; this.pwd = pwd; this.user = user; } }
3.配置文件

<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="user1" class="com.User"> <property name="id" value="1"></property> <property name="name" value="name1"></property> <property name="pwd" value="pwd1"></property> </bean> <bean id="user2" class="com.User"> <property name="id" value="2"></property> <property name="name" value="name2"></property> <property name="pwd" value="pwd2"></property> </bean> <bean id="user3" class="com.User"> <property name="id" value="3"></property> <property name="name" value="name3"></property> <property name="pwd" value="pwd3"></property> </bean> <!-- map集合的注入 --> <util:map id="user"> <entry key="1" value-ref="user1" /> <entry key="2" value-ref="user2"/> <entry key="2" value-ref="user3"/> </util:map> <bean id="mapDemo" class="com.MapDemo"> <property name="id" value="001"/> <property name="name" value="tom"/> <property name="pwd" value="123456"/> <!-- 把User類涉及到Demo2類中--> <property name="user" ref="user"/> </bean> </beans>
4.測試代碼

package test; import com.MapDemo; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by Administrator on 2016/12/4. */ public class MapDemoTest { public static void main(String[] args) { ClassPathXmlApplicationContext cx=new ClassPathXmlApplicationContext("demo1.xml"); MapDemo mapDemo=(MapDemo) cx.getBean("mapDemo"); System.out.println(mapDemo); } }
備注:在測試的時候出了一個問題。因為有修改過User的類名。所以在MapDemo中也同步替換了屬性名和方法名,但是在注入的時候一直失敗。提示user的問題。
解決:刪除set、get,重新生成。