Spring容器中,对于Bean的属性,或者说是集合,可以使用Spring容器进行值的注入和加载。包括基本类型的值的注入和容器类的注入。首先需要写一个Bean.

package com.bird.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import com.bird.dao.PersonDao; public class PersonServerBean { private PersonDao personDao; private String name; private Set<String> set = new HashSet<String>(); private List<String> list = new ArrayList<String>(); private Properties properties = new Properties(); private Map<String,String> map = new HashMap<String,String>(); public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public String getName() { return name; } public void setName(String name) { this.name = name; } public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void init() { personDao.add(); System.out.println(name); for(String value : set){ System.out.println(value); } for(String temp:list){ System.out.println(temp); } for(int i = 1; i <= properties.size(); i++){ System.out.println(properties.getProperty(String.valueOf(i))); } for(Entry<String, String> entry :map.entrySet()){ System.out.println(entry.getKey()+"=="+entry.getValue()); } } }
在Bean.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" 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" > <context:annotation-config/> <!-- <bean id="personDao" class="com.bird.dao.impl.PersonDaoBean"></bean> <bean id="personService" class="com.bird.service.impl.PersonServerImpl" init-method="init" destroy-method="destory" scope="prototype"></bean> <bean id="personServerBean" class="com.bird.service.impl.PersonServerBean"> <property name="personDao" ref="personDao"></property> </bean> --> <bean id="personServerBean" class="com.bird.service.impl.PersonServerBean"> <property name="personDao"> <bean class="com.bird.dao.impl.PersonDaoBean"></bean> </property> <property name="name" value="bird"></property> <property name="set"> <set> <value>第一个</value> <value>第二个</value> <value>第三个</value> </set> </property> <property name="list"> <list> <value>one</value> <value>two</value> <value>three</value> </list> </property> <property name="properties"> <props> <prop key="1">Properties1</prop> <prop key="2">Properties2</prop> <prop key="3">Properties3</prop> </props> </property> <property name="map"> <map> <entry key="key-1" value="value-1"></entry> <entry key="key-2" value="value-2"></entry> <entry key="key-3" value="value-3"></entry> </map> </property> </bean> </beans>
这样就可以完成对容器类的属性注入了

转载于:https://www.cnblogs.com/sp2012/archive/2012/02/25/2465738.html

Spring对字段和集合的注入---依赖注入相关推荐

  1. 详解spring的IOC控制反转和DI依赖注入

    转载 详解spring的IOC控制反转和DI依赖注入 2018-06-05 15:45:34 jiuqijack 阅读数 2945 文章标签: spring IOC控制反转 DI依赖注入 更多 分类专 ...

  2. (八)Spring之IOC控制反转、DI依赖注入介绍和使用(详解)

    文章目录 前言 Spring Spring IOC 简介 Bean IOC 概述 IOC 本质理解 Spring IOC 应用 IOC xml装配 IOC 依赖注入 IOC Bean的作用域 IoC ...

  3. Spring的bean的注创建、依赖注入、自动装配

    一.bean 定义:被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象. bean作用 ...

  4. Spring源码分析(十)依赖注入源码解析3:DefaultListableBeanFactory#doResolveDependency 真正开始解析依赖项

    4.2 真正开始解析依赖项(最核心方法) org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveD ...

  5. Spring-初识Spring框架-IOC控制反转(DI依赖注入)

    ---恢复内容开始--- IOC :控制反转 (DI:依赖注入) 使用ioc模式开发 实体类必须有无参构造方法 1.搭建Spring环境 下载jar http://maven.springframew ...

  6. SSM之Spring文件配置/Spring怎么在pom文件中进行依赖注入/不同的依赖注入类型有哪些以及怎么写

    写在前面: 接着记录自己的Spring学习之旅,若看不懂则建议先看上一篇博客SSM框架之Spring介绍开发流程/IDEA如何建立Spring项目,详细代码可在我的Gitee仓库ssm-learnin ...

  7. Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)

    目录 1 什么是spring框架 2 spring框架的特点 3 spring容器 3.1 什么是spring容器 3.2 spring容器创建对象的编程步骤 3.4 spring容器创建对象的方式 ...

  8. Spring框架中的控制反转和依赖注入

    控制反转: 控制反转是用来降低代码之间的耦合度的,基本思想就是借助"第三方"实现具有依赖对象的解耦. 为什么需要控制反转,因为项目中对象或多或少存在耦合.控制反转的关键在于Ioc容 ...

  9. Spring MVC应用@Autowired和@Service进行依赖注入

    在前面学习的控制器中并没有体现 MVC 的 M 层,这是因为控制器既充当 C 层又充当 M 层.这样设计程序的系统结构很不合理,应该将 M 层从控制器中分离出来. Spring MVC 框架本身就是一 ...

  10. 三大框架之spring框架+IoC控制反转、DI依赖注入

    三大框架:业务层框架Spring+IoC+DI 往期文章:jsp与cookie.重定向与RESTFul架构支持 下一章节: 持久层框架MyBatis 初识Spring框架 MyBatis 入门http ...

最新文章

  1. php如何导入大文件数据库,PHP读取CSV大文件导入数据库的示例
  2. 【修炼1】《序章》关于要写给谁看的问题
  3. php中按引用传递参数,如何通过PHP中的引用传递可变参数的参数?
  4. C++ 避免内存泄漏
  5. java数据结构之折半查找
  6. QMap排序方法运用实例
  7. 张正友相机标定法--相机去畸变(C++)
  8. 基于opencv的身份证识别系统
  9. 1965: 求矩阵中最小元素及其位置
  10. 【老生谈算法】matlab实现传染病模型源码——传染病模型
  11. 神经网络 mse一直不变_自动扩增:从数据中学习扩增策略|扩增|top|算法|神经网络|样本...
  12. 中国农大计算机保研,中国农业大学2021届保研情况
  13. html messagebox确定取消,提示组件 - MessageBox 弹框 - 《ElementUI v2.15 使用手册》 - 书栈网 · BookStack...
  14. DSO windowed optimization 代码 (4)
  15. CAS单点登录-密码管理(十三)
  16. 通过一张照片,获取照片的地址信息
  17. 解决Windows 10不显示打字框
  18. 无法更新计算机配置系统时间,电脑怎么设置系统时间自动更新
  19. Abaqus基础问题解答
  20. PTA - 数据库合集54

热门文章

  1. Unity 2D角色复活点与复活等待时间设置
  2. 【转】wget 使用技巧
  3. 【转】飞鸽端口号被占用时的解决方法
  4. 网络营销教程—SEO 第五章 单面页最佳优化
  5. Wannafly挑战赛28
  6. 王者荣耀交流协会 — Alpha阶段中间产物
  7. iOS 使用UIView的一种有效方法
  8. 1020. Tree Traversals
  9. HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效...
  10. wow和scrollreveal动画