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类型 查看 ...
随机推荐
- RS-232, RS-422, RS-485 Serial Communication General Concepts(转载)
前面转载的几篇文章重点介绍了UART及RS-232.在工控领域除了RS-232以外,常用的串行通信还有RS-485.本文转载的文章重点介绍了RS-232.RS-422和RS-485. Overview ...
- MVC 问答
1.View含有什么,默认就念有Models吗? 不是,ViewBag是一个空对象.ViewBag 与 Models 不是必须一起使用的 . 2.Models 可用可不用?存在意义?
- java多线程系类:JUC锁:01之框架
本章,我们介绍锁的架构:后面的章节将会对它们逐个进行分析介绍.目录如下:01. Java多线程系列--"JUC锁"01之 框架02. Java多线程系列--"JUC锁&q ...
- 关于DOS与cmd(windows系统)
dos是计算机的最初期的操作系统,对电脑操作必须输入各种dos命令窗口,可以理解成运行计算机机器内部语言,知道编程吗?其实早期dos命令操作系统就是运行计算机内部的编程命令,因此操作人员都必须具有一定 ...
- Jquery easy ui datagrid動態加載列問題
1.如下图效果是当选择不同的日期范围时datagrid则会加载出对应的列数
- hibernate与ibatis比较
hibernate 是当前最流行的o/r mapping框架,它出身于sf.net,现在已经成为jboss的一部分了. ibatis 是另外一种优秀的o/r mapping框架,目前属于apache的 ...
- 如何理解vue.js组件的作用域是独立的
vue.js组件的作用域是独立,可以从以下三个方面理解: 1.父组件模板在父组件作用域内编译,父组件模板的数据用父组件内data数据:2.子组件模板在子组件作用域内编译,子组件模板的数据用子组件内da ...
- learn mips
可以使用MARS来编汇编,MARS是一个用java编的IDE,它是一个模拟环境. 样例:重要的句子输出三遍 .data str: .asciiz "weidiao is great\n&qu ...
- 配置javac环境
初始的javac是默认不可用,如下图: 系统变量->新建->变量名:JAVA_HOME 变量值:(C:\Program Files\Java\jdk1.7.0_03)(这只是我的JDK安装 ...
- 数据结构图文解析之:AVL树详解及C++模板实现
0. 数据结构图文解析系列 数据结构系列文章 数据结构图文解析之:数组.单链表.双链表介绍及C++模板实现 数据结构图文解析之:栈的简介及C++模板实现 数据结构图文解析之:队列详解与C++模板实现 ...