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
    }
}
View Code

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;
    }
}
View Code

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>
View Code

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);
    }
}
View Code

 

备注:在测试的时候出了一个问题。因为有修改过User的类名。所以在MapDemo中也同步替换了属性名和方法名,但是在注入的时候一直失败。提示user的问题。

解决:删除set、get,重新生成。

转载于:https://www.cnblogs.com/xumao/p/6132245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值