数据,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. Spoj-COINS-记忆化dp

    COINS - Bytelandian gold coins #dynamic-programming In Byteland they have a very strange monetary sy ...

  2. yii2在linux下面无法启用gii

    原因:linux下面默认的Gii只能通过127.0.0.1来访问,也就是本机访问,安全: 解决:在conf/main-local.php添加自己的ip

  3. C++高级编程1 C++速成

    C++高级编程1 C++速成 1.常用的预处理指令 #include [file] #define key value 这个是在C中经常使用的,定义常量数值,用于字符串替换的工作. #ifndef k ...

  4. I/O复用服务器端+回声客户端

    并发服务器的第二种实现方法:I/O复用 服务器端: #include <arpa/inet.h> #include <unistd.h> #include <algori ...

  5. vue 侧边导航栏递归显示

    import axios from "axios"; import tabs1 from "../tab_content/tab1.vue"; import m ...

  6. New Concept English three(12)

    25W/m 76% Most of us have formed an unrealistic picture of life on a desert island. We sometimes ima ...

  7. vue 问题集合 |

    vue做类似选项卡                                                 点击改变curIndex ,  选项内容显示用 v-show="$inde ...

  8. Buildroot构建指南--快速上手与实用技巧

    Buildroot官方全英文使用手册的链接是https://buildroot.org/downloads/manual/manual.html,需要知道每一个细节的朋友,可以仔细查阅,这篇文章只是我 ...

  9. React-Native进阶_5.导航 Naviagtion

    有这样一个组件 他可以控制页面跳转 返回,在移动端叫做导航控制器, 在RN中叫路由 我们使用的  react-native-navigation 是一个开源组件库介绍:A complete nativ ...

  10. 我也说说Emacs吧(6) - Lisp速成

    前面我们学习了基本操作,也走马观花地看了不少emacs lisp的代码.这一章我们做一个lisp的速成讲座. Lisp的含义是表处理语言.它的代码组成结构都是用括号组成的表来表示的.Lisp中的功能, ...