Spring中集合类型属性注入
我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看。
这里将介绍如何给Map list set Array Properties 这些属性注入值。
1.创建一个类:员工类Employee
package cn.entity;
/**
* 员工类
*
* @author hyj
*
*/
public class Employee {
//员工年龄
private Integer age;
//员工姓名
private String name;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(Integer age, String name) {
super();
this.age = age;
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.创建第二个类里面包含上面所包含的集合和数组
package cn.collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import cn.entity.Employee;
public class Collection {
private String [] empName;//数组
private List<Employee> empList;//list集合
private Set<Employee> empsets;//set集合
private Map<String,Employee> empMaps;//map集合
private Properties pp;//Properties的使用
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;
}
}
3.创建ApplicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 准备员工 -->
<bean id="emp1" class="cn.entity.Employee">
<property name="age" value="18"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="emp2" class="cn.entity.Employee">
<property name="age" value="18"></property>
<property name="name" value="李四"></property>
</bean>
<!-- Collection对象-->
<bean id="collection" class="cn.collection.Collection">
<!-- 给数组赋值 -->
<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集合赋值 -->
<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="110" >hello</prop>
<prop key="120">Spring</prop>
</props>
</property>
</bean>
</beans>
4.测试类:
package cn.test;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.collection.Collection;
import cn.entity.Employee;
public class TestHappy {
/**
* 给数组赋值
*/
@Test
public void setArray(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Collection collection=(Collection)context.getBean("collection");
for (String item : collection.getEmpName()) {
System.out.println(item);
}
}
/**
* 给list集合赋值
*/
@Test
public void setList(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Collection collection=(Collection)context.getBean("collection");
for (Employee item : collection.getEmpList()) {
System.out.println("年龄:"+item.getAge()+"姓名:"+item.getName());
}
}
/**
* 给set集合赋值
*/
@Test
public void setSet(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Collection collection=(Collection)context.getBean("collection");
for (Employee item : collection.getEmpsets()) {
System.out.println("年龄:"+item.getAge()+"姓名:"+item.getName());
}
}
/**
* 给map集合赋值
*/
@Test
public void setMap(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Collection collection=(Collection)context.getBean("collection");
Map<String, Employee> map=collection.getEmpMaps();
Iterator<String> iterator=map.keySet().iterator();
while (iterator.hasNext()) {
Employee employee=map.get(iterator.next());
System.out.println("年龄:"+employee.getAge()+"姓名:"+employee.getName());
}
}
@Test
/**
* Properties集合
*/
public void testSet(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Collection collection=(Collection)context.getBean("collection");
for(Entry<Object,Object> entry: collection.getPp().entrySet()){
System.out.println(entry.getKey().toString()+" "+entry.getValue().toString());
}
}
}
Spring中集合类型属性注入的更多相关文章
- Spring中@value以及属性注入的学习
1.简单的Java配置 配置文件(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://1 ...
- 【坑】Spring中抽象父类属性注入,子类调用父类方法使用父类注入属性
运行环境 idea 2017.1.1 spring 3.2.9.RELEASE 需求背景 需要实现一个功能,该功能有2个场景A.B,大同小异 抽象一个抽象基类Base,实现了基本相同的方法BaseMe ...
- Spring学习日记03_IOC_属性注入_集合类型属性
Ioc操作Bean管理(xml注入集合属性) 注入数组类型属性 注入List集合类型属性 注入Map集合类型属性 Stu类 public class Stu { //1. 数组类型属性 private ...
- Spring、基本类型属性和集合类型属性的注入
Spring 还可以对基本属性和集合类型属性进行注入: public interface PersonIService { public String getBaseProperty(); publi ...
- IoC容器-Bean管理XML方式(注入集合类型属性)
Ico操作Bean管理(xml注入集合属性) 1,注入数组类型属性 2,注入List集合类型属性 3,注入Map集合类型属性 (1)创建类,定义数组.list.map.set类型属性,生成对应set方 ...
- 【Spring实战】—— 7 复杂集合类型的注入
之前讲解了Spring的基本类型和bean引用的注入,接下来学习一下复杂集合类型的注入,例如:List.Set.Map等. 对于程序员来说,掌握多种语言是基本的技能. 我们这里做了一个小例子,程序员们 ...
- Spring 依赖注入(DI) 的三种方式 和 对集合类型的注入
// 分别省略了getter setter public class Student { private String name; private int age; private Teacher t ...
- spring中bean配置和注入场景分析
bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean,并 ...
- Hive中集合类型
Hive中集合类型 创建表,集合是以 - 分割的 数据文件 加载数据 查询数据 查询数组中第一个字段 再建一个表,使用map 查看数据文件 加载数据 查询数据 查询键值 创建表,struct类型 查看 ...
随机推荐
- postman使用之一:安装启动篇
官网介绍:http://www.getpostman.com/docs/ postman是干什么的就不介绍了,本文从postman的安装开始介绍,后续会有其它使用的介绍. 安装: 1.mac app安 ...
- C++系统预定义4个用于标准数据流对象
C++系统预定义4个用于标准数据流对象 cin cout cerr clog C++里关于cerr,clog,cout三者的区别: cerr(无缓冲标准错误)-----没有缓冲,发送给它的 ...
- [Unity3D]深度相机 Depth Camera
作为3D世界里最重要的窗口,摄像机的应用就显得很重要,毕竟在屏幕上看到的一切都得用摄像机矩阵变换得来的嘛.论坛上看到了一篇帖子讲非天空盒的背景做法,让我想起其实很多界面合成画面可以用摄像机之间的交互来 ...
- IIS7.5使用web.config设置伪静态的二种方法(转)
近几天公司里开发的项目有几个运行在IIS7.5上,由于全站采用的是伪静态,因此从网上找到两两种方法来实现.这两种方法各有优势:第一种比较灵活,只要把文件拷到根目录下,即可直接显示所有伪静态页面(适用于 ...
- 【转】【C#】C# 5.0 新特性——Async和Await使异步编程更简单
一.引言 在之前的C#基础知识系列文章中只介绍了从C#1.0到C#4.0中主要的特性,然而.NET 4.5 的推出,对于C#又有了新特性的增加--就是C#5.0中async和await两个关键字,这两 ...
- 原生JS实现MVVM模式
欢迎大家指导与讨论 : ) 前言 关于MVVM的原理大家可以参考这篇文章.<[翻译]Object.observe()带来的数据绑定变革 >http://www.tuicool.com/ar ...
- webpack --- 详解
官网: http://webpack.github.io/docs/using-loaders.html 简书: http://www.jianshu.com/p/42e11515c10f
- canvas弹动效果
弹动效果,用物体与目标的距离乘上系数再累加至速度上,让物体呈加速度运动,再让速度乘与摩擦力系数,让物体最终停止运动 代码如下所示 var canvas = document.getElementByI ...
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
string.IsNullOrEmpty 都知道,这个功能是判断字符串是否为:null或者string.Empty.如果是如"\t"这样的字符就返回false了,为了达到判断过滤这 ...
- 【USACO 3.1】Contact(01子串按出现次数排序)
题意:给你一个01字符串,将长度为a到b之间(包含a.b)的子串按照出现次数排序.注意输入输出格式 题解:01子串对应一个二进制,为了区别11和011这样的不同子串,我们把长度也记录下来,官方题解是在 ...