首页 > 代码库 > Spring注入-Map
Spring注入-Map
在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="http://www.mamicode.com/1"></property> <property name="name" value="http://www.mamicode.com/name1"></property> <property name="pwd" value="http://www.mamicode.com/pwd1"></property> </bean> <bean id="user2" class="com.User"> <property name="id" value="http://www.mamicode.com/2"></property> <property name="name" value="http://www.mamicode.com/name2"></property> <property name="pwd" value="http://www.mamicode.com/pwd2"></property> </bean> <bean id="user3" class="com.User"> <property name="id" value="http://www.mamicode.com/3"></property> <property name="name" value="http://www.mamicode.com/name3"></property> <property name="pwd" value="http://www.mamicode.com/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="http://www.mamicode.com/001"/> <property name="name" value="http://www.mamicode.com/tom"/> <property name="pwd" value="http://www.mamicode.com/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,重新生成。
Spring注入-Map
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。