当前位置: 首页 > news >正文

Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件


(1)下边的一个java类包含了所有Map、Set、List、数组、属性集合等这些容器,主要用于演示Spring的注入配置;

package com.lc.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> empMaps;//map集合
	private Properties pp;//Properties的使用

	
	public Set<Employee> getEmpsets() {
		return empsets;
	}
	public void setEmpsets(Set<Employee> empsets) {
		this.empsets = empsets;
	}
	public String[] getEmpName() {
		return empName;
	}
	public void setEmpName(String[] empName) {
		this.empName = empName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Employee> getEmpList() {
		return empList;
	}
	public void setEmpList(List<Employee> empList) {
		this.empList = empList;
	}
	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;
	}

}

(2)Spring配置文件beans.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"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		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
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="department" class="com.hsp.collection.Department">
<property name="name" value="财务部"/>

<!-- 给数组注入值 -->
<property name="empName">
	<list>
		<value>小明</value>
		<value>小明小明</value>
		<value>小明小明小明小明</value>
	</list>
</property>

<!-- 给list注入值 list 中可以有相当的对象 -->
<property name="empList">
	<list>
		<ref bean="emp2" />
		<ref bean="emp1"/>
		<ref bean="emp1"/>
		<ref bean="emp1"/>
		<ref bean="emp1"/>
		<ref bean="emp1"/>
		<ref bean="emp1"/>
	</list>
</property>

<!-- 给set注入值 set不能有相同的对象 -->
<property name="empsets">
	<set>
		<ref bean="emp1" />
		<ref bean="emp2"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
		<ref bean="emp2"/>
	</set>
</property>

<!-- 给map注入值 map只有key不一样,就可以装配value -->
<property name="empMaps">
	<map>
		<entry key="11" value-ref="emp1" /> 
		<entry key="22" value-ref="emp2"/>
		<entry key="22" value-ref="emp1"/>
	</map>
</property>

<!-- 给属性集合配置 -->
<property name="pp">
	<props>
		<prop key="pp1">abcd</prop>
		<prop key="pp2">hello</prop>
	</props>
</property>
</bean>

<bean id="emp1" class="com.hsp.collection.Employee">
	<property name="name" value="北京"/>
	<property name="id" value="1"/>
</bean>
<bean id="emp2" class="com.hsp.collection.Employee">
	<property name="name" value="天津"/>
	<property name="id" value="2"/>
</bean>

</beans>


(3)如何使用

package com.lc.collection;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App1 {

	
	public static void main(String[] args) {

		ApplicationContext ac=new ClassPathXmlApplicationContext("com/lc/collection/beans.xml");
		Department department=(Department) ac.getBean("department");
		System.out.println(department.getName());
		for(String emName:department.getEmpName()){
			System.out.println(emName);
		}
		/*
		 * 通过list集合取出数据
		 */
		System.out.println("**********通过list集合取出数据*****");
		for(Employee e : department.getEmpList()){
			System.out.println("name="+e.getName()+" "+e.getId());
		}
		/*
		 * 通过set集合取出数据
		 */
		System.out.println("**********通过set集合取出数据*****");
		for(Employee e : department.getEmpsets()){
			
			System.out.println("name="+e.getName());
		}
		/*
		 * 通过map集合取出数据 迭代器
		 */
		System.out.println("*******通过map集合取出数据 迭代器****");
		
		//1.迭代器
		Map<String,Employee> empmaps=department.getEmpMaps();
		Iterator it=empmaps.keySet().iterator();
		while(it.hasNext()){
			String key=(String) it.next();
			Employee emp=empmaps.get(key);
			System.out.println("key="+key+" "+emp.getName());
		}
		
		System.out.println("*******通过map集合取出数据 简洁方法****");
		//2.简洁方法
		for(Entry<String,Employee> entry1:department.getEmpMaps().entrySet()){
			
			System.out.println(entry1.getKey()+" "+entry1.getValue().getName());
		}
		
		System.out.println("*****通过Propertis取出数据*****");
		Properties pp=department.getPp();
		for(Entry<Object,Object> entry:pp.entrySet()){
			System.out.println(entry.getKey().toString()+" "+entry.getValue().toString());
		}
		System.out.println("*****通过Enumeration取出*****");
		Enumeration en= pp.keys();
		while(en.hasMoreElements()){
			String key=(String) en.nextElement();
			System.out.println(key+" "+pp.getProperty(key));
		}
	}

}


(4)以后那些不知道的粘贴拷贝即可


注:转载请注明出处!


相关文章:

  • 设计模式-23种参考模式
  • Spring中继承配置的注入方法
  • Spring控制反转和依赖注入
  • springMVC框架搭建流程
  • 四舍五入的方法
  • Java代码生成二维码图片
  • 2.Cocos2dx 3.2重力系统Box2D
  • Ubuntu下安装C/C++开发环境【!!!有更新!!!Ubuntu14.10下使用eclipse搭建C语言开发环境】
  • 翻译一篇文章:It's Difficult to Grow a Test Developer(成为测试开发工程师的艰辛)...
  • Spring的核心机制:依赖注入
  • Spring获取ApplicationContext对象工具类
  • linux基础学习8
  • Ubuntu系统更新命令笔记
  • 大龄屌丝自学笔记--Java零基础到菜鸟--038
  • VMWare虚拟机提示:锁定文件失败,打不开磁盘的解决办法
  • ES6系列(二)变量的解构赋值
  • FastReport在线报表设计器工作原理
  • JavaScript 基础知识 - 入门篇(一)
  • js数组之filter
  • Linux快速复制或删除大量小文件
  • node和express搭建代理服务器(源码)
  • PHP 使用 Swoole - TaskWorker 实现异步操作 Mysql
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • Spring-boot 启动时碰到的错误
  • TCP拥塞控制
  • 订阅Forge Viewer所有的事件
  • 关于extract.autodesk.io的一些说明
  • 跨域
  • 漂亮刷新控件-iOS
  • 设计模式 开闭原则
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • 国内开源镜像站点
  • 扩展资源服务器解决oauth2 性能瓶颈
  • 我们雇佣了一只大猴子...
  • ​3ds Max插件CG MAGIC图形板块为您提升线条效率!
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #define 用法
  • $ is not function   和JQUERY 命名 冲突的解说 Jquer问题 (
  • ( 10 )MySQL中的外键
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (JS基础)String 类型
  • (附源码)springboot 校园学生兼职系统 毕业设计 742122
  • (转)C#调用WebService 基础
  • ****Linux下Mysql的安装和配置
  • *1 计算机基础和操作系统基础及几大协议
  • .halo勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET Micro Framework 4.2 beta 源码探析
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .Net Remoting(分离服务程序实现) - Part.3
  • .NET 中创建支持集合初始化器的类型
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .net连接oracle数据库
  • .NET中winform传递参数至Url并获得返回值或文件
  • .sys文件乱码_python vscode输出乱码