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类型 查看 ...
随机推荐
- 关于C语言的问卷调查
你对自己的未来有什么规划?做了哪些准备? 答:游戏开发,参与一些游戏的测试,通过自身的游戏体验和其他人的游戏体验来总结什么样的游戏会让人眼前一亮,爱不释手. 你认为什么是学习?学习有什么用?现在学习动 ...
- bean 接收的参数为Class类型
这两个是等价的
- CDN缓存机制
CDN也叫内容分发网络,是一个经策略性部署的整体系统,包括分布式储存.负载均衡.网络请求的重定向和内容管理4个要件.而其中内容管理和全局的网络流量管理是CDN的核心所在.通过用户就进行和服务器负载的判 ...
- DbContext 和ObjectContext两者的区别
http://blog.csdn.net/lvjin110/article/details/24642911 ObjectContext是一种模型优先的开发模式,DbContext是代码优先的开发模式 ...
- apache服务器启动时提示httpd: apr_sockaddr_info_get() failed for
apache服务器启动时提示httpd: apr_sockaddr_info_get() failed for 在RedHat Linux 5 与 CentOS 5服务器上配置好apache后,启动或 ...
- 用纯css改变下拉列表select框的默认样式(不兼容IE10以下)
在这篇文章里,我将介绍如何不依赖JavaScript用纯css来改变下拉列表框的样式. 事情是这样的,您的设计师团队向您发送一个新的PSD(Photoshop文档),它是一个新的网站的最终设计 ...
- JavaScript类库---JQuery(二)
接上: 6.Ajax: 一个基础底层函数:jQuery.ajax(); //高级工具函数都会调用此函数: 一个高级工具方法:load() ; 四个高级工具函数:jQuery.getScript ...
- Django登录访问限制 login_requeired
作用: 1. 用户登录之后才可以访问某些页面 2. 如果没登录,跳转到登录页面 3. 用户在跳转的登陆界面中完成登陆后,自动访问跳转到之前访问的地址 要实现这个需求很简单就是在相应的view前面使用装 ...
- MyBatis参数传入集合之foreach用法
传入集合list // 账户类型包括门店和分公司 List<Object> scopeList = new ArrayList<Object>(); scopeList.add ...
- Dijkstra的双栈算术表达式的求值算法
例如需要计算 ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) 我们以字符串的形式输入该表达式,利用两个栈来完成这个操作,其中一个栈保存运算符,一个栈保存操作数,过程是这样的: 表 ...