Spring中集合注入方法
集合注入重要是对数组、List、Set、map的注入,具体注入方法请参照一下代码(重点是applicationContext.xml中对这几个集合注入的方式):
1.在工程中新建一个Department类,该类包含在com.LHB.collection包当中
package com.LHB.collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Department {
private String name;
private String[] empName;
private List<Employee> empList; //List集合
private Set<Employee> empSets; //Set集合
private Map<String,Employee> empMap; //map集合
private Properties pp; //Properties的使用 public Properties getPp() {
return pp;
}
public void setPp(Properties pp) {
this.pp = pp;
}
public Map<String, Employee> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<String, Employee> empMap) {
this.empMap = empMap;
}
public Set<Employee> getEmpSets() {
return empSets;
}
public void setEmpSets(Set<Employee> empSets) {
this.empSets = empSets;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empName) {
this.empName = empName;
}
}
2.继续在包中创建Employee类
package com.LHB.collection;
public class Employee {
private String name;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
3.创建applicationContext.xml配置文件,配置重点在数组,List,Set,Map,propertes装载值的环节
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="department" class="com.LHB.collection.Department">
<property name="name" value="财务部门" />
<!-- 给数组注入值 -->
<property name="empName">
<list>
<value>小米</value>
<value>小明</value>
<value>小四</value>
</list>
</property> <!-- 给list注入值 可以有相同的多个对象 -->
<property name="empList">
<list>
<ref bean="emp1" />
<ref bean="emp2"/>
</list>
</property>
<!-- 给set注入值 不能有相同的对象 -->
<property name="empSets">
<set>
<ref bean="emp1" />
<ref bean="emp2"/>
</set>
</property> <!-- 给map注入值 只要map中的key值不一样就可以装配value -->
<property name="empMap">
<map>
<entry key="1" value-ref="emp1" />
<entry key="2" value-ref="emp2" />
</map>
</property> <!-- 给属性集合配置 -->
<property name="pp">
<props>
<prop key="pp1">hello</prop>
<prop key="pp2">world</prop>
</props>
</property>
</bean>
<bean id="emp1" class="com.LHB.collection.Employee">
<property name="name">
<value>北京</value>
</property>
</bean>
<bean id="emp2" class="com.LHB.collection.Employee">
<property name="name">
<value>天津</value>
</property>
</bean> </beans>
4.继续在该包中新建App1.java测试类
package com.LHB.collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App1 { public static void main(String[] args) {
// TODO Auto-generated method stub //通过类路径应用上下文获取配置文件applicationContext.xml
ApplicationContext ac = new ClassPathXmlApplicationContext("com/LHB/collection/applicationContext.xml");
//通过getBean()获取到applicationContext.xml文件中Bean对象
Department dm = (Department) ac.getBean("department");
System.out.println(dm.getName());
//取出数组中的值
for(String emName : dm.getEmpName()){
System.out.println(emName);
} System.out.println("*********通过List集合取出数据******");
for(Employee e : dm.getEmpList()){
System.out.println("name = "+ e.getName());
} System.out.println("*********通过Set集合取出数据******");
for(Employee e : dm.getEmpSets()){
System.out.println("name = "+ e.getName());
} System.out.println("*********通过Map集合取出数据(迭代器)******");
//迭代器
Map<String,Employee> empMap = dm.getEmpMap();
Iterator it = empMap.keySet().iterator();
while(it.hasNext()){
String key = (String) it.next();
Employee emp = empMap.get(key);
System.out.println("key = " + key + " " + emp.getName());
}
System.out.println("*********通过Map集合取出数据(Emtry简洁法)******");
//简洁方法
for(Entry<String,Employee> entry : dm.getEmpMap().entrySet()){ System.out.println(entry.getKey()+ " " + entry.getValue().getName());
} System.out.println("*********通过Propertis取出数据(通过Entry对象取)******");
Properties pp = dm.getPp();
for(Entry<Object,Object> entry : pp.entrySet()){
System.out.println(entry.getKey().toString() + ", "+ entry.getValue().toString());
}
System.out.println("*********通过Propertis取出数据(通过Enumeration对象取)******");
Enumeration en = pp.keys();
while(en.hasMoreElements()){
String key = (String) en.nextElement();
System.out.println(key + " " + pp.getProperty(key));
}
}
}
运行结果如下:
Spring中集合注入方法的更多相关文章
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
- 使用IDEA详解Spring中依赖注入的类型(上)
使用IDEA详解Spring中依赖注入的类型(上) 在Spring中实现IoC容器的方法是依赖注入,依赖注入的作用是在使用Spring框架创建对象时动态地将其所依赖的对象(例如属性值)注入Bean组件 ...
- spring中构造函数注入
spring中构造函数注入,简单来说,就是通过beans.xml中,设置对应的值.而且通过bean类中的构造函数进行注入这些值. 文件结构 watermark/2/text/aHR0cDovL2Jsb ...
- Spring中属性注入的几种方式以及复杂属性的注入
在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...
- spring中依赖注入
理解依赖注入:参考https://blog.csdn.net/taijianyu/article/details/2338311 一.依赖注入让bean与bean之间以配置文件组织在一起,而不是以硬编 ...
- Spring中属性注入的几种方式以及复杂属性的注入详解
在spring框架中,属性的注入我们有多种方式,我们可以通过set方法注入,可以通过构造方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List.Map.Prope ...
- Spring中的注入方式 和使用的注解 详解
注解:http://www.cnblogs.com/liangxiaofeng/p/6390868.html 注入方式:http://www.cnblogs.com/java-class/p/4727 ...
- Spring中的destroy-method方法
1. Bean标签的destroy-method方法 配置数据源的时候,会有一个destroy-method方法 <bean id = "dataSource" class ...
- Spring中集合类型属性注入
我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看. 这里将介绍如何给Map list set Array Properties 这些属性注入值. ...
随机推荐
- 启动虚拟机提示"Units specified don’t exist SHSUCDX can’t install"
新建虚拟机快速分区后启动报"Units specified don’t exist SHSUCDX can’t install",试过网上说的 修改BIOS设置方法不起作用 修改虚 ...
- MonkeyRunner_真机_运行脚本(二)
# -*- coding: UTF-8 -*- #手机分辨率为1080*1920 import sys from com.android.monkeyrunner import MonkeyRunne ...
- idea导出可执行jar包
一. 在菜单中选择 File->project structure. 二. 在弹出的窗口中左侧选中"Artifacts",点击"+"选择jar,然后选择& ...
- 在asp.net中使用瀑布流,无限加载
页面中代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1 ...
- 什么是ELK
为什么用到ELK: 一般我们需要进行日志分析场景:直接在日志文件中 grep.awk 就可以获得自己想要的信息.但在规模较大的场景中,此方法效率低下,面临问题包括日志量太大如何归档.文本搜索太慢怎么办 ...
- 深入理解Java虚拟机笔记
1. Java虚拟机所管理的内存 2. 对象创建过程 3. GC收集 4. HotSpot算法的实现 5. 垃圾收集器 6. 对象分配内存与回收细节 7. 类文件结构 8. 虚拟机类加载机制 9.类加 ...
- weblogic学习教程(一)
一.简介 WebLogic是美国Oracle公司出品的一个application server,确切的说是一个基于JAVAEE架构的中间件,WebLogic是用于开发.集成.部署和管理大型分布式Web ...
- AsyncStorage和Promise配合使用
代码: AsyncStorage封装 import {AsyncStorage} from "react-native"; class DeviceStorage { //保存数据 ...
- 谷歌浏览器安装octotree插件
Octotree Chrome安装与使用方法 Octotree Chrome作用: 主要使你在github查看项目时可以清晰明了的看到项目的结构以及具体代码,使下载代码更具有目的性,减少不必要代码的下 ...
- [CC]Mesh文件保存
CC中有两个地方使用了文件保存,一个是MainWindow,另一个是ccCommandLineParser. MainWindow的保存按钮关联的槽是doActionSaveFile()方法,实现了点 ...