在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的更多相关文章

  1. spring @Autowired注入map

    注入map,平常一般不会这么做,今天看一段老代码时发现有这么个用法.补习一下. @Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会 ...

  2. spring注入参数详解

    spring注入参数详解 在Spring配置文件中, 用户不但可以将String, int等字面值注入到Bean中, 还可以将集合, Map等类型的数据注入到Bean中, 此外还可以注入配置文件中定义 ...

  3. 线程中无法实例化spring注入的服务的解决办法

    问题描述 在Java Web应用中采用多线程处理数据,发现Spring注入的服务一直报NullPointerException.使用注解式的声明@Resource和XML配置的bean声明,都报空指针 ...

  4. 17_8_9 Spring 注入

    1 Spring 的 Bean 的属性注入: 构造方法的方式注入属性: <!-- 第一种:构造方法的方式 --> <bean id="car" class=&qu ...

  5. 基于配置文件的Spring注入

    基于配置文件的Spring注入 1.依赖注入的概述 依赖注入指的是通过Spring配置文件的方式创建对象时,直接通过配置的方式将数据注入到该对象的标量类型属性,并从Spring容器中获取指定对象注入到 ...

  6. 监听器中spring注入相关的问题

    问题描述: 需求是要求在项目启动自动触发一个service中的线程的操作,使用监听器来实现,但是自定义监听器中spring注解service失败,通过WebApplicationContextUtil ...

  7. java:Spring框架1(基本配置,简单基础代码模拟实现,spring注入(DI))

    1.基本配置: 步骤一:新建项目并添加spring依赖的jar文件和commons-logging.xx.jar: 步骤二:编写实体类,DAO及其实现类,Service及其实现类; 步骤三:在src下 ...

  8. Spring注入JPA+JPA事务管理

    本例实现的是Spring注入JPA 和 使用JPA事务管理.JPA是sun公司开发的一项新的规范标准.在本质上来说,JPA可以看作是Hibernate的一个子集:然而从功能上来说,Hibernate是 ...

  9. Spring注入中byType和byName的总结

    1.首先,区分清楚什么是byType,什么是byName. <bean id="userServiceImpl" class="cn.com.bochy.servi ...

随机推荐

  1. [学习笔记]设计模式之Proxy

    为方便读者,本文已添加至索引: 设计模式 学习笔记索引 写在前面 “魔镜啊魔镜,谁是这个世界上最美丽的人?” 每到晚上,女王都会问魔镜相同的问题(见Decorator模式).这是她还曾身为女巫时留下的 ...

  2. js获取css属性方法

    function getDefaultStyle(obj,attribute){ return obj.currentStyle?obj.currentStyle[attribute] : docum ...

  3. jQuery的延迟对象

    之前看别人的demo,发现在延迟对象被resolve时要执行的代码,有时会写在deferred.then方法里执行,有时会写在deferred.done方法里执行. 这让对延迟对象一知半解的我非常困惑 ...

  4. Android开源项目(转载)

    第一部分 界面 ImageView.ProgressBar及其他如Dialog.Toast.EditText.TableView.Activity Animation等等. 一.ListView an ...

  5. 指令发email

    win7下指令发送email:(telnet:不为内部指令时控制面板 -> 程序和功能 -> 打开或关闭Windows功能,如下“telnet客户端”) telnet smtp.sina. ...

  6. about Red_Hat_Enterprise_Linux_7

    systemd systemd 是 Linux 的系统和服务管理程序,替换了 Red Hat Enterprise Linux 之前的发行本中使用的 SysV.systemd 与 SysV 和 Lin ...

  7. silverlight中DataGrid数据高亮显示

    效果如图所示, <UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.W ...

  8. (C语言)char类型与int类型相加

    #include <stdio.h> int main(void) { ; ; int c = a + b; a += b; printf("c=%d",c); //p ...

  9. 2016030102 - Ubuntu软件安装与删除相关命令

    apt-get, dkpg 常用命令: 安装软件命令: apt-get install softname1 softname2 softname3…… 卸载软件命令: apt-get remove s ...

  10. IOS 上传头像-b

    感谢大神分享 1.首先,后台给了我这样的接口 1-后台数据接口 2.首先加上代理方法 <UIActionSheetDelegate,UINavigationControllerDelegate, ...