数据,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. phalcon: dispatcher->forward地址转发/重定向

    比如,我indexController里面的indexAction,因为用户没有穿参数,我要重定向到 errorAction里面 $this->dispatcher->forward(ar ...

  2. [洛谷U62358]求导函数

    U62358 求导函数 题面 给出一个n次函数\(f(x)=a_{n}x^{n}+a_{n-1}x^{n-1}+...+a_{1}x+a_0\)的各项系数\(a_n,a_{n-1}...a_1,a_0 ...

  3. 关于mybatis mapper.xml中的if判断

    场景: 页面上有搜索框进行调节查询,不同搜索框中的内容可以为空. 过程: 点击搜索,前端把参数传给后台,这是后台要把为空的参数过滤掉. 做法: 通常我们在dao层即mapper.xml中进行过滤判断操 ...

  4. MFC sendmessage实现进程间通信

    用sendmessage实现进程间通信. 1.WM_COPYDATA实现进程间通信 实现方式是发送WM_COPYDATA消息. 发送程序: LRESULT copyDataResult; //copy ...

  5. 初识async函数

    为什么会出现async函数 首先从大的方面来说,出现async函数时为了解决JS编程中的异步操作,再往具体说就是为了对以往异步编程方法的一种改进,也有人说仅仅只是Generator 函数的语法糖,这个 ...

  6. redisAPI整理

    全局命令 1.查看所有键 keys * 2.键总数 dbsize 3.检查键是否存在 exists key 4.删除键 del key del key1 key2 key3 5.键过期 expire ...

  7. C++复习4.函数设计基础

    C/C++ 函数设计基础 20130918 函数式程序的基本功能单元,是模块化程序设计的基础,即使函数的功能正确是不够的,因为函数设计的细微缺点很容易导致函数被错用. 了解函数的基本知识,堆栈和堆的相 ...

  8. c# 使用SqlBulkCopy 提高大数据插入数据库速度

    自己得一点总结: 1.BulkCopy采用的是插入方式,不是覆盖方式(原数据不动,在原数据的后面复制上dataTable中的内容) 2.自增的字段不用赋值 3.数据库字段名和dataTable列名可以 ...

  9. OC-初识面向对象

    面向对象和面向过程思想 OC是面向对象的,C是面向过程的.面向对象和面向过程只是解决问题的两种不同思想 面向对象和面向过程的区别 以用电脑听歌为例子 面向过程 打开电脑 播放电脑中的歌曲 关闭电脑 面 ...

  10. Jxl的API概述(转)

    一.Jxl的API Jxl的API主要有三个包,jxl,jxl.format,jxl.write.如果单独的分析API,可能对于更明确的了解此API没有太多的帮助,我们还是从Excel文件的层次来剥离 ...