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 @Autowired注入map
注入map,平常一般不会这么做,今天看一段老代码时发现有这么个用法.补习一下. @Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会 ...
- spring注入参数详解
spring注入参数详解 在Spring配置文件中, 用户不但可以将String, int等字面值注入到Bean中, 还可以将集合, Map等类型的数据注入到Bean中, 此外还可以注入配置文件中定义 ...
- 线程中无法实例化spring注入的服务的解决办法
问题描述 在Java Web应用中采用多线程处理数据,发现Spring注入的服务一直报NullPointerException.使用注解式的声明@Resource和XML配置的bean声明,都报空指针 ...
- 17_8_9 Spring 注入
1 Spring 的 Bean 的属性注入: 构造方法的方式注入属性: <!-- 第一种:构造方法的方式 --> <bean id="car" class=&qu ...
- 基于配置文件的Spring注入
基于配置文件的Spring注入 1.依赖注入的概述 依赖注入指的是通过Spring配置文件的方式创建对象时,直接通过配置的方式将数据注入到该对象的标量类型属性,并从Spring容器中获取指定对象注入到 ...
- 监听器中spring注入相关的问题
问题描述: 需求是要求在项目启动自动触发一个service中的线程的操作,使用监听器来实现,但是自定义监听器中spring注解service失败,通过WebApplicationContextUtil ...
- java:Spring框架1(基本配置,简单基础代码模拟实现,spring注入(DI))
1.基本配置: 步骤一:新建项目并添加spring依赖的jar文件和commons-logging.xx.jar: 步骤二:编写实体类,DAO及其实现类,Service及其实现类; 步骤三:在src下 ...
- Spring注入JPA+JPA事务管理
本例实现的是Spring注入JPA 和 使用JPA事务管理.JPA是sun公司开发的一项新的规范标准.在本质上来说,JPA可以看作是Hibernate的一个子集:然而从功能上来说,Hibernate是 ...
- Spring注入中byType和byName的总结
1.首先,区分清楚什么是byType,什么是byName. <bean id="userServiceImpl" class="cn.com.bochy.servi ...
随机推荐
- asp.net web api内部培训资料
最近在公司进行了一次asp.net web api的分享吧,不算是培训. 可能大家有些人对Web API的推出目的还不是很了解,可以看看微软官方的定义,其实是为了提供一个好的REST Service方 ...
- C# 之【线程与进程】
1. 引言 先来个比喻手法: 如果把上课的过程比作进程,那么每个学生就是一个线程,他们共享教室,即线程共享进程的内存空间.每一个时刻,只能一个学生问老师问题,老师回答完毕,轮到下一个.即线程在一个时 ...
- 网络编程(学习整理)---3--(Udp)FeiQ实现广播消息群发
1.广播群发消息: 这里使用的任然是UDP协议,使用方法还是比较简单的! 我就记录一下需要注意的一些地方(笔记): (1)这里是在局域网内,借用FeiQ聊天软件,编写一段程序,实现对局域网内的每一个登 ...
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- mysql的查询缓存模式介绍
mysql的查询缓存 查询是数据库技术中最常用的操作.查询操作的过程比较简单,首先从客户端发出查询的SQL语句,数据库服务端在接收到由客户端发来的 SQL语句后, 执行这条SQL语句,然后将查询到的结 ...
- [CSS]overflow内容溢出
定义和用法 overflow 属性规定当内容溢出元素框时发生的事情. 说明 这个属性定义溢出元素内容区的内容会如何处理.如果值为 scroll,不论是否需要,用户代理都会提供一种滚动机制.因此,有 ...
- Yeoman安装
Yeoman帮助我们创建项目,提供更好的工具来使我们的项目更多样化. Yeoman提供generator系统,一个generator是一个插件,在我们在一个完整的项目上使用‘yo’命令时,会运行该ge ...
- C#小数点位数处理方法
//方法一: //保留小数位数,并能四舍五入 DecimalFormat de = new DecimalFormat("0.00"); System.out.println(de ...
- WCF SOAP
WCF SOAP服务端解析 ASP.NET Core中间件(Middleware)进阶学习实现SOAP 解析. 本篇将介绍实现ASP.NET Core SOAP服务端解析,而不是ASP.NET Cor ...
- Hbase 学习笔记(一) Hbase的物理模型 Hbase为每个值维护了一个多级索引,即<key, column family, column name, timestamp>
比如第一个region 代表 0-100 第二个region 代表 101 -200的 分的越多越不好管理,但同时方便了并行化处理,并发度越高,处理的越快.mapreduce就是按照rowkey的 ...