Spring注入-Map
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="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,重新生成。
以上是关于Spring注入-Map的主要内容,如果未能解决你的问题,请参考以下文章
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段