Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件
转:http://blog.csdn.net/xlgen157387/article/details/40349929
(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)以后那些不知道的粘贴拷贝即可
注:转载请注明出处!
Spring中使用Map、Set、List、数组、属性集合的注入方法配置文件相关推荐
- Spring中IoC两种接口和两种依赖注入方式的比较
spring是一个开源框架,是为了解决企业应用程序开发的复杂性而创建的,为J2EE应用程序开发提供集成的框架.简单来说,spring是一个轻量级的控制反转IOC和面向切面AOP的容器框架.spring ...
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过注解@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 ...
- 九、Spring中使用@Value和@PropertySource为属性赋值
首先回顾下在xml中我们是如何为spring的bean进行属性赋值呢? 大体是这样的 <bean id="person" class="com.atguigu.be ...
- python阈值计算_python – 在numpy中计算超过阈值的数组值的最快方法
使用cython可能是一个不错的选择. import numpy as np cimport numpy as np cimport cython from cython.parallel impor ...
- 事务例子_Spring事务专题(四)Spring中事务的使用、抽象机制及模拟Spring事务实现...
Spring中事务的使用示例.属性及使用中可能出现的问题 前言 本专题大纲如下: 「对于专题大纲我又做了调整哈,主要是希望专题的内容能够更丰富,更加详细」,本来是想在源码分析的文章中附带讲一讲事务使用 ...
- Spring中Bean对象的存储和获取
目录 1. 更简单的将bean存储到spring中 1.0 前置工作,在配置文件中设置bean扫描的根路径 1.1 通过注解将bean存储到spring中 1.1.1 @Controller[控制器] ...
- Spring中DI介绍
文章目录 Spring中DI介绍 一.基于XML配置注入依赖 1. 有参构造函数注入依赖 2. set方法注入依赖 3. 注入自定义类型 4. 注入集合类型 二.基于注解形式注入依赖 1. @Valu ...
- html5有foreach吗,JavaScript中的map()和forEach()有什么区别?
来源:https://medium.com/better-programming,作者:Moon,翻译:公众号<前端全栈开发者> JavaScript中一些最受欢迎的功能可能是map和fo ...
- spring中@Value读取.properties配置文件中文乱码问题
spring中读取.properties文件中文乱码原因是因为springmvc或spring配置文件加载配置文件时没有设定字符集编码问题导致的,其默认是会将.properties配置文件转码为uni ...
最新文章
- request的生命周期
- load和initialize
- Dijkstra 算法——计算有权最短路径(边有权值)
- IOS解惑(1)之@property(nonatomic,getter=isOn) BOOL on;中的getter解惑
- HttpURLConnection上传文件(图片)小试
- 使用Arcpy进行数据批处理-批量裁剪
- 下列不属于计算机完成科学计算机特点的是,通知|1906统考模拟题计算机试卷2...
- 11-TensorFlow 基于ResNet的轴承故障诊断
- 网络连接状态断开服务器无响应,解决SSH自动断线,无响应的问题。
- Spring核心框架体系结构
- android相机固定焦距,如何找到Android相机的焦距?
- JQuery插件二--colorbox
- 「雕爷学编程」Arduino动手做(13)——触摸开关模块
- 惠州生物实验室建设宝典
- Android:访问存储失败.FileNotFoundException open failed: XXXXXXX EPERM (Operation not permitted)
- 杂记——在开发ByteTalk中遇到的困难以及解决
- 随笔---有关爬虫的总结
- 如何测试数字硅麦软件,硅麦参考电路及layout注意事项.PDF
- 在php输出字符串时执行html标签,把字符串作为PHP代码执行
- 嵌入式linux矩阵键盘,基于嵌入式Linux的矩阵键盘驱动程序开发