依赖注入的方式有四种:

1、Setter注入(属性注入)

2、构造器注入

3、P命名空间注入

4、集合类型值注入

1、Setter注入(属性注入)

Employee 员工实体类

package com.spring.pojo;public class Employee {private Integer id;private String name;private Department department;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}@Overridepublic String toString() {return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]";}public Employee(Integer id, String name, Department department) {super();this.id = id;this.name = name;this.department = department;}public Employee() {super();// TODO Auto-generated constructor stub
    }}

View Code

Department 部门实体类

package com.spring.pojo;public class Department {private Integer id;private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Department() {super();// TODO Auto-generated constructor stub
    }public Department(Integer id, String name) {super();this.id = id;this.name = name;}@Overridepublic String toString() {return "Department [id=" + id + ", name=" + name + "]";}}

View Code

主配置文件里面

2、构造器注入

3、P命名空间注入

添加命名空间

xmlns:p="http://www.springframework.org/schema/p"

使用P标签

4、集合类型注入

创建collection集合实体类

package com.spring.pojo;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class CollectionBean {private List<String> list;private Set<String> set;private Map<String, Object> map;private Properties properties;private String[] array;public CollectionBean() {super();// TODO Auto-generated constructor stub
    }public CollectionBean(List<String> list, Set<String> set, Map<String, Object> map, Properties properties,String[] array) {super();this.list = list;this.set = set;this.map = map;this.properties = properties;this.array = array;}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 Map<String, Object> getMap() {return map;}public void setMap(Map<String, Object> map) {this.map = map;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}public String[] getArray() {return array;}public void setArray(String[] array) {this.array = array;}}

View Code

主配置文件中,依赖注入

 <bean id="collectionBean" class="com.spring.pojo.CollectionBean"><!--有序可重复  --><property name="list"><list><value>list1</value><value>list2</value><value>list3</value></list></property><!--无序不可重复  --><property name="set" ><set><value>set1</value><value>set2</value><value>set3</value></set></property><property name="map"><map><entry key="key1" value="法海1"></entry><entry key="key2" value="法海2"></entry><entry key="key3" value="法海3"></entry></map></property><property name="array"><array><value>String1</value><value>String2</value><value>String3</value></array></property><!--properties是特殊的Map  --><property name="properties"><props><prop key="prokey1">values1</prop><prop key="prokey2">values2</prop><prop key="prokey3">values3</prop>             </props>     </property></bean>

View Code

转载于:https://www.cnblogs.com/luojack/p/11141004.html

框架学习 Spring之依赖注入DI相关推荐

  1. Spring学习4之依赖注入(DI)

    前言 上节学习了IOC创建对象的方式,我们在不知不觉中使用了最简单的构造注入,什么是构造注入,什么又是依赖注入呢? 一.首先我们要了解DI是什么? 创建对象的过程中Spring可以依据配置对象的属性进 ...

  2. java框架篇---spring IOC依赖注入

    spring依赖注入的方式有4种 构造方法注入 属性注入 工厂注入 注解注入 下面通过一个实例统一讲解: User.java package com.bjsxt.model;public class ...

  3. Spring框架—③依赖注入DI、Bean作用域及自动装配

    依赖注入 DI,Dependency injection 依赖: 指bean对象的创建依赖于Spring容器 注入: 指Bean对象所依赖的资源,由容器来设置和装配 在beans.xml中配置 1.常 ...

  4. Spring的依赖注入(DI)和面向切面(AOP)代码解析说明

    转载自: https://www.cnblogs.com/zmmi/p/7922186.html 1 依赖注入(DI)  大部分的Spring的新手(我)在学习之初对依赖注入这个词感到迷茫,事实上它并 ...

  5. java-12:spring MVC - 控制反转IOC,依赖注入DI

    学习spring框架之前,先理解几个概念: 1.第一部分:依赖倒置原则 2.第二部分:控制反转,控制反转容器(实例) 3.第三部分:控制反转,控制反转容器(全面理解,面试题) 综合性理解:控制反转(I ...

  6. 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程

    一.依赖注入DI 学习目标 1)搞清楚构造参数依赖注入的过程及类 2)搞清楚注解方式的属性依赖注入在哪里完成的. 学习思路 1)思考我们手写时是如何做的 2)读 spring 源码对比看它的实现 3) ...

  7. Spring学习第4篇:Spring 的依赖注入

    大家家好,我是一名网络怪咖,北漂五年.相信大家和我一样,都有一个大厂梦,作为一名资深Java选手,深知Spring重要性,现在普遍都使用SpringBoot来开发,面试的时候SpringBoot原理也 ...

  8. Spring框架----Spring的依赖注入

    1.spring的依赖注入的概念 依赖注入:dependency Injection IOC的作用:降低程序之间的依赖关系,但不是消除. 依赖关系的管理:以后都交给了spring来维护 在当前类中需要 ...

  9. 控制反转IOC与依赖注入DI

    为什么80%的码农都做不了架构师?>>>    1. IoC理论的背景 我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最 ...

最新文章

  1. oracle数据库是db还是dbnms,Oracle数据库中各种类型的文件损坏与修复过程详解(2)...
  2. LOL手游上线!同步专属限量游戏红包封面,还不快来拿?
  3. 新人新博客新学习家园
  4. RTP协议之Header结构解析
  5. 百练OJ:4151:电影节
  6. python上下文管理关键字_详解 Python 中的 with 与 上下文管理器
  7. decltype 和 auto
  8. oracle表数据如何恢复,ORACLE如何恢复被delete的表数据
  9. 【论文写作】学生信息管理系统总体设计如何写
  10. [需求管理-9]:需求规格说明书SRS
  11. java基础习题_Java基础知识练习题及答案
  12. darknet源码解析:networ结构体之input_pinned_gpu
  13. python求15的因数_十五道Python小案例,学会这些,Python基础已过关!
  14. Java开发面试(持续更新)
  15. Hadoop 服务器IP地址的配置
  16. 现代 React Web 开发实战——kanban实现卡片拖拽
  17. VMware虚拟机安装Windows11(无需设置TMP密码)
  18. easyCVR接入华为Vpaas(VCN/IVS) GB28181记录
  19. WIN10,配置adb环境
  20. MinGW-w64 安装和使用

热门文章

  1. 树莓派Raspberry 操作GPIO--LED
  2. css3修改input[type=radio]样式
  3. CSS之media Query
  4. Vue基础之Vue模板语法
  5. 互联网大佬学历背景大揭秘,看看是你的老乡还是校友
  6. 知乎问答:现在程序员的工资是不是被高估了?
  7. redis nginx session tomcat
  8. Spring MVC 5 + Thymeleaf 基于Java配置和注解配置
  9. 【python之路】数据库2
  10. [c++]访MSN浮出窗口的示例