数据,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. photoshop CS5制作具有立体感的按钮

    今天在学习用photoshop cs5制作html模板的过程中,遇到了立体感按钮的制作问题.当然按钮的立体感也可以用CSS来实现,这里主要是用PS来制作具有立体感的按钮. 我也是PS新手,下面的东西, ...

  2. WPF 回车转Tab实现跳转

    1.重写窗体的KeyDown事件 protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Enter) { // Mo ...

  3. HDU - 5988The 2016 ACM-ICPC Asia Qingdao Regional ContestG - Coding Contest 最小费用流

    很巧妙的建边方式 题意:有n个区域,每个区域有一些人数si和食物bi,区域之间有m条定向路径,每条路径有人数通过上限ci.路径之间铺了电线,每当有人通过路径时有pi的概率会触碰到电线,但是第一个通过的 ...

  4. HDU 4099 大数+Trie

    Revenge of Fibonacci Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 204800/204800 K (Java/ ...

  5. canvas图形的组合与裁切

    当两个或两个以上的图形存在重叠区域时,默认情况下一个图形画在前一个图像之上.通过指定图像globalCompositeOperation属性的值可以改变图形的绘制顺序或绘制方式,globalAlpha ...

  6. IOS-Quartz2D

    一.画基本图形 // // BWView.m // IOS_0221_Quartz2D画矩形 // // Created by ma c on 16/2/21. // Copyright © 2016 ...

  7. ZOJ 2975 Kinds of Fuwas(暴力+排列组合)

    Kinds of Fuwas Time Limit: 2 Seconds      Memory Limit: 65536 KB In the year 2008, the 29th Olympic ...

  8. SCM-MANAGER

    什么是SCM-MANAGER 基于Web的,集成了  Git. Mercurial .Subversion  多种代码管理工具的源代码管理平台 它有什么优点 简易安装 不需要破解配置文件,完全可配置的 ...

  9. mac下webstorm添加scss watcher

    一.前提条件: 1.安装ruby,如果我没记错的话,mac自带ruby,终端输入 ruby -v ,回车,如果显示ruby的版本号,则说明ruby环境已经安装好了.如果没有,自行安装ruby.例如我的 ...

  10. 关于px em rem的一点小总结

    2015-11-28 06:06:40 概念 都是CSS单位. px:像素 Pixel.像素 (计算机屏幕上的一个点) em:1em 等于当前的字体尺寸. 2em 等于当前字体尺寸的两倍. 例如,如果 ...