码迷,mamicode.com
首页 > 编程语言 > 详细

Spring注入-Map

时间:2016-12-04 23:40:59      阅读:608      评论:0      收藏:0      [点我收藏+]

标签:one   port   ret   getname   配置文件   tom   over   art   pre   

在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,重新生成。

Spring注入-Map

标签:one   port   ret   getname   配置文件   tom   over   art   pre   

原文地址:http://www.cnblogs.com/xumao/p/6132245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!