数据,list,set,map,Properties 集合注入

package Spring_collections;

/**
* Created by luozhitao on 2017/8/11.
*/
public class Employee { public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} private int age;
private String name; public Employee(int age, String name) {
this.age = age;
this.name = name;
} public Employee(){} }
package Spring_collections;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; /**
* Created by luozhitao on 2017/8/11.
*/
public class Employee_collection { public String[] getEmpName() {
return empName;
} public void setEmpName(String[] empName) {
this.empName = empName;
} public List<Employee> getEmpList() {
return empList;
} public void setEmpList(List<Employee> empList) {
this.empList = empList;
} public Set<Employee> getEmpSets() {
return empSets;
} public void setEmpSets(Set<Employee> empSets) {
this.empSets = empSets;
} public Map<String, Employee> getEmpMaps() {
return empMaps;
} public void setEmpMaps(Map<String, Employee> empMaps) {
this.empMaps = empMaps;
} public Properties getPp() {
return pp;
} public void setPp(Properties pp) {
this.pp = pp;
} private String [] empName;
private List<Employee> empList;
private Set<Employee> empSets;
private Map<String,Employee> empMaps;
private Properties pp; }
package Spring_collections;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.Iterator;
import java.util.Map; /**
* Created by luozhitao on 2017/8/11.
*/
public class Emp_app { public static void main(String [] args){ ApplicationContext context=new ClassPathXmlApplicationContext("collection_beans.xml"); Employee_collection employee_collection=(Employee_collection)context.getBean("collection_emp"); //数组
for (String str:employee_collection.getEmpName()){ System.out.println("集合 "+str); } System.out.println("----------------"); //list
for(Employee emp:employee_collection.getEmpList()){ System.out.println("list"+emp.getName()+"--"+emp.getAge()); } System.out.println("----------------"); //set for(Employee emp:employee_collection.getEmpSets()){ System.out.println("set"+emp.getName()+"--"+emp.getAge()); } System.out.println("----------------");
//map Map<String,Employee> map=employee_collection.getEmpMaps();
for (Map.Entry<String,Employee> m:employee_collection.getEmpMaps().entrySet()){ System.out.println("map"+m.getKey()+":"+m.getValue().getName()); } //迭代器方式
System.out.println("map迭代器方式----------------");
Iterator<String> iterator=employee_collection.getEmpMaps().keySet().iterator();
while (iterator.hasNext()){ Employee e=map.get(iterator.next());
System.out.println("maps迭代器"+e.getName()+"--"+e.getAge()); } System.out.println("----------------"); //properties for(Map.Entry<Object,Object> en:employee_collection.getPp().entrySet()){ System.out.println("properties"+en.getKey().toString()+":"+en.getValue().toString());
} } }

bean.xml

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="spring_service" /> <bean id="emp1" class="Spring_collections.Employee">
<property name="age" value="15"></property>
<property name="name" value="cat"></property>
</bean> <bean id="emp2" class="Spring_collections.Employee">
<property name="age" value="16"></property>
<property name="name" value="cat1"></property>
</bean> <bean id="collection_emp" class="Spring_collections.Employee_collection"> <!-- 数组 -->
<property name="empName">
<list>
<value>猫儿爹</value>
<value>猫儿妈</value>
<value>猫儿</value>
</list>
</property> <!-- list -->
<property name="empList">
<list> <ref bean="emp1"></ref>
<ref bean="emp2"></ref>
</list>
</property> <!-- sets -->
<property name="empSets">
<set>
<ref bean="emp1"></ref>
<ref bean="emp2"></ref>
</set> </property> <!-- maps -->
<property name="empMaps">
<map>
<entry key="001" value-ref="emp1"></entry>
<entry key="002" value-ref="emp2"></entry> </map>
</property> <!-- properties -->
<property name="pp">
<props>
<prop key="003">hello3</prop>
<prop key="004">hello4</prop> </props> </property> </bean> </beans>

spring--集合注入(常规方法)的更多相关文章

  1. Spring集合注入

    1.集合注入 上一篇博客讲了spring得属性注入,通过value属性来配置基本数据类型,通过<property>标签的 ref 属性来配置对象的引用.如果想注入多个数据,那我们就要用到集 ...

  2. Spring 集合注入

    Spring注入是spring框架的核心思想之一.在实际的开发中,我们经常会遇见这样一些类的注入,这些类中包含一些集合作为类的属性,那么要怎样想类中的集合注入数据呢?本文通过一个简单的示例向大家介绍一 ...

  3. spring集合类型注入

    spring集合类型注入 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUB ...

  4. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  5. 没想到吧,Spring中还有一招集合注入的写法

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. Spring作为项目中不可缺少的底层框架,提供的最基础的功能就是bean的管理了.bean的注入相信大家都比较熟 ...

  6. Spring(二)scope、集合注入、自动装配、生命周期

    原文链接:http://www.orlion.ga/189/ 一.scope bean的scope属性中常用的有两种:singleton(单例,默认)和prototype(原型,每次创建新对象) 例: ...

  7. SSM-Spring-04:Spring的DI的构造注入,P命名注入,和集合注入

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- DI和IOC相比,DI更偏向于实现 DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说 ...

  8. Spring中集合注入方法

    集合注入重要是对数组.List.Set.map的注入,具体注入方法请参照一下代码(重点是applicationContext.xml中对这几个集合注入的方式): 1.在工程中新建一个Departmen ...

  9. 在Spring中注入Java集合

    集合注入重要是对数组.List.Set.map的注入,具体注入方法请参照一下代码(重点是applicationContext.xml中对这几个集合注入的方式): 1.在工程中新建一个Departmen ...

  10. Spring之注入的几种方式

    普通注入 在配置文件里 <!-- 构造注入 --> <bean id="user1" class="entity.User"> < ...

随机推荐

  1. Mac OSX 如何在命令行中生成 md5、sha1、sha256 校验和

    计算 MD5 校验和 md5 /tmp/hello.txt 计算 SHA-1 校验和 shasum -a 1 /tmp/hello.txt 计算 SHA-256 校验和 shasum -a 256 / ...

  2. wget/curl查看请求响应头信息

    wget / curl 是两个比较方便的测试http功能的命令行工具,大多数情况下,测试http功能主要是查看请求响应 头信息 ,而给这两个工具加上适当的命令行参数即可轻易做到,其实查man手册就能找 ...

  3. hdu3544找规律

    如果x>1&&y>1,可以简化到其中一个为1的情况,这是等价的,当其中一个为1(假设为x),另一个一定能执行y-1次, 这是一个贪心问题,把所有的执行次数加起来比较就能得到 ...

  4. date.timezone not set in php.ini. Please contact ...解决方案

    无论是在LAMP还是在LNMP系统环境下, 只要PHP的版本在5.3及其以上的版本时, 无论是在安装oscommerce, 还是在安装zen cart, 以及其他的CMS时, 都会遇到如下所示的错误信 ...

  5. 【hive】关于浮点数比较的问题

    当在hive中写下浮点数(例如:0.2) hive会把浮点数(0.2)存储为double类型 但是系统中并不能精准表示0.2这个浮点数 正确的浮点数表示 float   0.2 —> 0.200 ...

  6. 2793 [Poi2012]Vouchers

    我们直接模拟就可以了= = now[x]表示x的倍数已经取到x * i了,于是每次读入x,直接向上枚举x个没取过的数即可. /************************************* ...

  7. 【zzuli-2276】跳一跳

    题目描述 今天跳跳去公园游玩,第一个游戏就难倒了跳跳,游戏规则是跳跳站在一个面积无限大的矩形土地上,开始时跳跳在左上角(即第一行第一列),每一次跳跳都可以选择一个右下方格子,并瞬间跳过去(如从下图中的 ...

  8. 【转】netlink socket编程实例

    [转]netlink socket编程实例 转自:http://blog.chinaunix.net/uid-14753126-id-2983915.html 关于Netlink IPC方式的介绍,请 ...

  9. HP数组转JSON函数json_encode和JSON转数组json_decode函数的使用方法

    这两个函数比较简单,我这里直接写例子,但是有一点一定要注意,json数据只支持utf-8格式,GBK格式的数据转换为json会报错! json_encode()用法: <?php$data =a ...

  10. 联想北研实习生面试-嵌入式Linux研发工程师

    8月中旬暑假去联想北研参加了实习生面试,面试职位是嵌入式Linux研发工程师.投完简历第二天,主管回复我邮件,意思是说随时来面试,到北研时候给他打个电话就行.于是我回复条短信表示感谢,并约好时间第二天 ...