从头认识Spring-1.12 注入Map

这一章节我们简单介绍一下注入Map。

1.domain

烤炉类:(基本没什么变化)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_12;

public class Oven {
	private String name = "";

	@Override
	public String toString() {
		return name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

厨师类:(为了简便性,我删除了前面的Cake的list)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_12;

import java.util.HashMap;

public class Chief {

	private static int index = 0;

	private final int id = index++;

	private String name = "";

	private HashMap<String, Oven> ovens = null;

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void userOvens() {
		for (String key : ovens.keySet()) {
			System.out.println("oven key:" + key);
			System.out.println(name + " use " + ovens.get(key));
		}
	}

	public void setName(String name) {
		this.name = name;
	}

	public HashMap<String, Oven> getOvens() {
		return ovens;
	}

	public void setOvens(HashMap<String, Oven> ovens) {
		this.ovens = ovens;
	}

}

而且我修改了userOvens这个方法,把他变成遍历Map。

2.测试类:(为了简便测试,我删减了制作蛋糕的动作)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_12;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_12/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		Chief jack = (Chief) applicationContext.getBean("jack");
		jack.userOvens();
		System.out.println("--------------------");
		Chief rose = (Chief) applicationContext.getBean("rose");
		rose.userOvens();
	}
}

3.配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- <bean id="cake" class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_10.Cake"
		p:Name="jack‘s own cake"> <property name="size" value="7"></property> </bean> -->

	<bean id="bigOven"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_12.Oven"
		p:name="bigOven" />

	<bean id="smallOven"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_12.Oven"
		p:name="smallOven" />

	<bean id="jack"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_12.Chief"
		p:name="jack">
		<property name="ovens">
			<map>
				<entry key="bigOven" value-ref="bigOven" />
				<entry key="bigOven" value-ref="bigOven" />
				<entry key="smallOven" value-ref="smallOven" />
			</map>
		</property>
	</bean>

	<bean id="rose"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_12.Chief"
		p:name="rose">
		<property name="ovens">
			<map>
				<entry key="smallOven" value-ref="smallOven" />
			</map>
		</property>
	</bean>

</beans>

跟之前的配置文件对比,这里用map替代了之前的set或者list,然后在entry里面需要加上key这个属性。

测试输出:

oven key:bigOven
jack use bigOven
oven key:smallOven
jack use smallOven
--------------------
oven key:smallOven
rose use smallOven

4.注意

entry里面的属性有

key 对应的是值

key-ref 对应的是对象的引用

value 对应的是值

value-ref 对应的是对象的引用

总结:这一章节简单介绍了注入Map。

目录:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

时间: 2024-08-26 11:59:16

从头认识Spring-1.12 注入Map的相关文章

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()

spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装(引用)

spring心得4--setter注入集合(set.list.map.properties等多种集合,配有案例解析)@基本装 1. 基本装配 在spring容器内拼凑bean叫做装配.装配bean的时候,需要告诉容器哪些bean以及容器如何使用依赖注入将它们配合在一起.    使用XML装配(xml是最常见的spring应用系统配置源.) 几种spring容器都支持使用xml装配bean,包括: 1).XmlBeanFactory:调用InputStream载入上下文定义文件. 2).Class

spring @Autowired注入map

注入map,平常一般不会这么做,今天看一段老代码时发现有这么个用法.补习一下. @Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会将容器中所有类型符合 Map 的 value 对应的类型的 Bean 增加进来,用 Bean 的 id 或 name 作为 Map 的 key. 原文地址:https://www.cnblogs.com/zhjh256/p/11179678.html

(spring-第3回)spring的依赖注入-属性、构造函数、工厂方法等的注入

Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring容器中,由spring容器实例化bean然后交给程序员.spring的依赖注入有属性注入.构造函数注入.工厂方法注入等多种方式,下面用几个简单的栗子来一一道来. 一.首先是属性注入: 代码001 1 <?xml version="1.0" encoding="UTF-8&q

关于Spring IOC (依赖注入)你需要知道的一切

[版权申明]未经博主同意,不允许转载!(请尊重原创,博主保留追究权) http://blog.csdn.net/javazejian/article/details/54561302 出自[zejian的博客] <Spring入门经典>这本书无论对于初学者或者有经验的工程师还是很值一看的,最近花了点时间回顾了Spring的内容,在此顺带记录一下,本篇主要与spring IOC相关 ,这篇博文适合初学者也适合spring有过开发经验的工程师,前者可用于全面了解Spring IOC的知识点,后者且

java的反射原理与Spring的自动注入(转载)

Java反射原理与Spring的自动注入 反射的定义 java的反射机制就是在运行状态中, 对于任意一个类都能够知道这个类的所有属性和方法; 对于任意一个对象,都能够调用它的任意一个方法和属性. 这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. Sping的自动注入原理 一个Bean的类: public class User{ private String username; private String mobile; public String getUsernam

spring的依赖注入的最常见的两种方法

1 package com.lsz.spring.action; 2 3 public class User { 4 /** 5 * set注入 6 */ 7 private String username; 8 public void setUsername(String username) { 9 this.username=username; 10 } 11 public String getUsername() { 12 return username; 13 } 14 /* 15 <b

spring中构造函数注入

spring中构造函数注入,简单来说,就是通过beans.xml中,设置对应的值.而且通过bean类中的构造函数进行注入这些值. 文件结构 Goods类 package com.test.innerbean; public class Goods { private String goodsName; private int price; public Goods(String name,int price) { goodsName=name; this.price=price; } publi

Spring IOC之注入

1.概念 控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护 简而言之: 把本身可以创建对象的权利交给IOC容器,当要实例化该对象的时候,由IOC容器来实例化该对象(专注于使用IOC创建的对象) 2.目的 创建对象并且组装对象之间的关系 3.步骤 1.Bean容器初始化 (即IOC容器加载beans的xml配置文件)  (加载并初始化配置文件<beans></beans>) 1.两个基础包 1.org.springframework.beans 1.