在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. PHP 学习笔记 (一)

    1. 在PHP中设置最长执行时间: PHP中的PHP.ini文件中,max_execution_time 项指定了PHP最长执行时间,默认是30秒.有两种方案可以对其进行修改: 1. 直接在PHP.i ...

  2. qrcode.js插件将你的内容转换成二维码格式

    ---qrcode.js插件将你的内容转换成二维码格式--- 我之前一直想知道二维码是怎么生成,所以就了解了一下, 最后还是不知道它的原理, 但是,我知道怎么生成. 现在就让我带你制作一个你喜爱的二维 ...

  3. C#Winform开发平台企业版V4.0功能表

    企业版V4.0 - 功能列表及模板窗体 C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framew ...

  4. 精通 Oracle+Python,第 4 部分:事务和大型对象

    通过 Python 管理数据事务.处理大型对象 2010 年 3 月发布 事务包含一组 SQL 语句,这组 SQL 语句构成数据库中的一个逻辑操作,如转帐或信用卡支付操作.将 SQL 语句聚合到一个逻 ...

  5. Python资源汇集

    Python资源汇集 一 实用教程 廖雪峰网站 第一,Python教程:提供了循序渐进,重点是可操作的实用教程. 第二,Web App 项目教程.给出一个用16天完成的Python Web APP项目 ...

  6. linux源码Makefile的详细分析

    目录 一.概述 1.本文的意义 2.Linux内核Makefile文件组成 二.Linux内核Makefile的“make解析”过程 1 顶层Makefile阶段 1.从总目标uImage说起 2.v ...

  7. union 与struct的空间计算

    一.x86 总体上遵循两个原则: 整体空间----占用空间最大的成员(的类型)所占字节数的整数倍 对齐原则----内存按结构成员的先后顺序排列,当排到该成员变量时,其前面已摆放的空间大小必须是该成员类 ...

  8. ibatis错误

    java.lang.IllegalArgumentException: Mapped Statement collection already contains value for com.regin ...

  9. hadoop 常用配置项

    core-site.xml  name value  Description   fs.default.name hdfs://hadoopmaster:9000 定义HadoopMaster的URI ...

  10. vim配置vimrc详解(转)

    vimrc的存放位置: 系统 vimrc 文件: "$VIM/vimrc" 用户 vimrc 文件: "$HOME/.vimrc" 用户 exrc 文件: &q ...