【Spring学习笔记-4】注入集合类List、Set、Map、Pros等
整体结构:

接口
package org.crazyit.app.service;public interface Axe{public String chop();}
package org.crazyit.app.service;public interface Person{public void test();}
package org.crazyit.app.service.impl;import java.util.*;import org.crazyit.app.service.*;public class Chinese implements Person{// 下面是系列集合类型的成员变量private List<String> schools;private Map scores;private Map<String , Axe> phaseAxes;private Properties health;private Set axes;private String[] books;public Chinese(){System.out.println("Spring实例化主调bean:Chinese实例...");}// schools的setter方法public void setSchools(List schools){this.schools = schools;}// scores的setter方法public void setScores(Map scores){this.scores = scores;}// phaseAxes的setter方法public void setPhaseAxes(Map<String , Axe> phaseAxes){this.phaseAxes = phaseAxes;}// health的setter方法public void setHealth(Properties health){this.health = health;}// axes的setter方法public void setAxes(Set axes){this.axes = axes;}// books的setter方法public void setBooks(String[] books){this.books = books;}// 访问上面全部的集合类型的成员变量public void test(){System.out.println(schools);System.out.println(scores);System.out.println(phaseAxes);System.out.println(health);System.out.println(axes);System.out.println(java.util.Arrays.toString(books));}}
SteelAxe.java
package org.crazyit.app.service.impl;import org.crazyit.app.service.*;public class SteelAxe implements Axe{public String chop(){return "钢斧砍柴真快";}}
package org.crazyit.app.service.impl;import org.crazyit.app.service.*;public class StoneAxe implements Axe{public String chop(){return "石斧砍柴好慢";}}
<?xml version="1.0" encoding="GBK"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><!-- 定义2个普通Axe Bean --><bean id="stoneAxe" class="org.crazyit.app.service.impl.StoneAxe"/><bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/><!-- 定义chinese Bean --><bean id="chinese" class="org.crazyit.app.service.impl.Chinese"><property name="schools"><!-- 为调用setSchools()方法配置List集合作为参数值 --><list><!-- 每个value、ref、bean...都配置一个List元素 --><value>小学</value><value>中学</value><value>大学</value></list></property><property name="scores"><!-- 为调用setScores()方法配置Map集合作为参数值 --><map><!-- 每个entry配置一个key-value对 --><entry key="数学" value="87"/><entry key="英语" value="89"/><entry key="语文" value="82"/></map></property><property name="phaseAxes"><!-- 为调用setPhaseAxes()方法配置Map集合作为参数值 --><map><!-- 每个entry配置一个key-value对 --><entry key="原始社会" value-ref="stoneAxe"/><entry key="农业社会" value-ref="steelAxe"/></map></property><property name="health"><!-- 为调用setHealth()方法配置Properties集合作为参数值 --><props><!-- 每个prop元素配置一个属性项,其中key指定属性名 --><prop key="血压">正常</prop><prop key="身高">175</prop></props><!--<value>pressure=normalheight=175</value> --></property><property name="axes"><!-- 为调用setAxes()方法配置Set集合作为参数值 --><set><!-- 每个value、ref、bean..都配置一个Set元素 --><value>普通的字符串</value><bean class="org.crazyit.app.service.impl.SteelAxe"/><ref bean="stoneAxe"/><!-- 为Set集合配置一个List集合作为元素 --><list><value>20</value><!-- 再次为List集合配置一个Set集合作为元素 --><set><value type="int">30</value></set></list></set></property><property name="books"><!-- 为调用setBooks()方法配置数组作为参数值 --><list><!-- 每个value、ref、bean...都配置一个数组元素 --><value>疯狂Java讲义</value><value>疯狂Android讲义</value><value>轻量级Java EE企业应用实战</value></list></property></bean></beans>
测试文件:
BeanTest.java
package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class BeanTest{public static void main(String[] args)throws Exception{ApplicationContext ctx = newClassPathXmlApplicationContext("beans.xml");// 获取容器中Bean,并调用方法。Person p = ctx.getBean("chinese" , Person.class);p.test();}}
运行结果:

【Spring学习笔记-4】注入集合类List、Set、Map、Pros等的更多相关文章
- Spring 学习笔记 ----依赖注入
依赖注入 有三种方式,本文只学习下属性注入. 属性注入 属性注入即通过 setXxx方法()注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入方式是 ...
- Spring学习笔记--依赖注入
依赖注入和控制反转:http://baitai.iteye.com/blog/792980出自李刚<轻量级 Java EE 企业应用实战> Java应用是一种典型的依赖型应用,它就是由一些 ...
- Spring学习笔记--构造器注入
之前讲到的名为"duke"的bean有一个私有成员变量beanBags代表这个杂技师bean的一次性能够抛出的最多的数量,Juggler有一个构造函数,构造函数的第一个参数(这里只 ...
- Spring学习笔记(七)模拟实际开发过程的调用过程XML版-Setter方式注入
模拟实际开发过程的调用过程XML版-Setter方式注入 源码获取github [TOC] 1.项目结构 2.jar包跟上个一样 3.重写set方法 UserServiceImpl.java 1234 ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
作者:ssslinppp 时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- Spring学习笔记(六)—— SSH整合
一.整合原理 二.整合步骤 2.1 导包 [hibernate] hibernate/lib/required hibernate/lib/jpa 数据库驱动 [struts2] struts-bla ...
随机推荐
- CentOS6编译安装Python3
CentOS6默认Python版本是2, 但我使用的是Python3,因此需要自己安装 但记住,不要删除自带的Python2,否则很多东西会报错,比如yum之类的.(别问我为什么知道...) 话不多说 ...
- 快速切题sgu127. Telephone directory
127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...
- 快速切题 sgu 111.Very simple problem 大数 开平方 难度:0 非java:1
111.Very simple problem time limit per test: 0.5 sec. memory limit per test: 4096 KB You are given n ...
- 拷贝构造函数(define)
1.拷贝构造函数是一种特殊的构造函数,具有单个形参,此形参是对该类型的引用. 当定义一个新对象并用一个同类型的对象对它进行初始化时,将显示使用拷贝构造函数. 2.当将该类型的对象传递给函数或从函数返回 ...
- float、clear、overflow
浮动: float: none|left|right 作用使得标签失去块级标签的独占一行效果,向某个方向靠拢 标签浮动了,也需要占地方,有时候出现未浮动的div覆盖部分浮动div是浏览器的bug情况 ...
- JQuery, Silverlight 公用WCF
WCF web.config配置: <?xml version="1.0"?> <configuration> <system.web> < ...
- 背景 半透明问题 rgba + filter
<html style=" background: violet;"><head><meta charset="utf-8"> ...
- Android中Parcel的分析以及使用
简单点来说:Parcel就是一个存放读取数据的容器, Android系统中的binder进程间通信(IPC)就使用了Parcel类来进行客户端与服务端数据的交互,而且AIDL的数据也是通过Parcel ...
- git添加本地项目到git
1.切换到项目所在文件夹下:git int 2.git add -A 3.git commit -m '11' 4.git remote add origin https://github.com/g ...
- SWIFT中调用Segue的几个方法
场景1: 如图所示,在视图的第一个按钮处拉出一条Segue到另外一个视图,并给这个Segue命名,如SegueOne 此时可以用代码调用这个Segue切换视图: self.performSegueWi ...