【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 ...
随机推荐
- mongodb的capped Collection集合
db.createCollection(name, {capped: true, autoIndexId: true, size: 1000, max :100} ) name:集合的名字 cappe ...
- python *args **kwargs,传入不固定的参数给函数,或者传入很多的内容给函数,常用在构造函数中。
''' 例1:展示*args的用法,传入多个参数,不进行预先定义. 本例传入了3个参数.没有预先定义.在函数内自动生成元组() ''' def q1(*args): print('例1') print ...
- Spring MVC和Spring Data JPA之按条件查询和分页(kkpaper分页组件)
推荐视频:尚硅谷Spring Data JPA视频教程,一学就会,百度一下就有, 后台代码:在DAO层继承Spring Data JPA的PagingAndSortingRepository接口实现的 ...
- Swift Tips笔记
“??”操作符可以判断输入并在当左侧的值是非 nil 的 Optional 值时返回其 value,当左侧是 nil 时返回右侧的值. 例: var level: Int? var startLeve ...
- hibernate一对一关联
hibernate一对一主键关联 一对一主键关联指的是两个表通过主键形成的一对一映射. 数据表要求:A表的主键也是B表的主键同时B表的主键也是A表的外键 sql: create table peopl ...
- utf-8和Unicode的区别
链接 utf-8和Unicode到底有什么区别?是存储方式不同?编码方式不同?它们看起来似乎很相似,但是实际上他们并不是同一个层次的概念 要想先讲清楚他们的区别,首先应该讲讲Unicode的来由. 众 ...
- 解决Ubuntu下在firefox中打开Microsoft Outlook Web Access中文乱码
Edit---Preference--Content--Languages--Choose...---Select a langue to add... 添加中文
- UNIX设备文件
UNIX和Linux中比较重要的三个设备文件是:/dev/console,/dev/tty和/dev/null. 0 /dev/console 这个设备代表的是系统控制台,错误信息和诊断信息通常会被发 ...
- 算法训练 P1102
算法训练 P1102 时间限制:1.0s 内存限制:256.0MB 定义一个学生结构体类型student,包括4个字段,姓名.性别.年龄和成绩.然后在主函数中定义一个结构体数组( ...
- react 部分ES6写法
react+react-router+antd 栗子:https://github.com/Aquarius1993/reactApp 模块: 1. 引入模块 import React from 'r ...