极客时间 - 小马哥讲Spring核心编程思想

第二章 重新认识IoC

什么是 IOC

In software engineering, inversion of control (IoC) is a programming principle. IoC inverts the flow of control as compared to traditional control flow. In IoC, custom-written portions of a computer program receive the flow of control from a generic framework. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code.

– https://en.wikipedia.org/wiki/Inversion_of_control

IoC 的简史

  • 1983年,Richard E. Sweet 在《The Mesa Programming Environment》中提出“Hollywood Principle”(好莱坞原则)
  • 1988年,Ralph E. Johnson & Brian Foote 在 《Desiging Reusable Classes》中提出“Inversion of control”(控制反转)
  • 1996年,Michael Mattsson 在《Object-Oriented Frameworks,A survey of methodological issues》中将“Inversion of control”命名为“Hollywood Principle”
  • 2004年,Martin Fowler 在《Inversion of Control Containers and the Dependency Injection pattern》中提出了自己对Ioc以及DI的理解。
  • 2005年,Martin Fowler 在《InversionOfControl》对Ioc做出了进一步的说明

Ioc 主要实现策略

In object-oriented programming, there are several basic techniques to implement inversion of control. These are:

  • Using a service locator pattern
  • Using dependency injection, for example
    • Constructor injection
    • Parameter injection
    • Setter injection
    • Interface injection
  • Using a contextualized lookup
  • Using template method design pattern
  • Using strategy design pattern

– https://en.wikipedia.org/wiki/Inversion_of_control#Implementation techniques


在《Expert One-on-OneTM J2EETM Development without EJBTM》的 128页提到:

IoC is a broad concept that can be implemented in different ways. There are two main types:

❑ Dependency Lookup: The container provides callbacks to components, and a lookup context.
This is the EJB and Apache Avalon approach. It leaves the onus on each component to use container APIs to look up resources and collaborators. The Inversion of Control is limited to the
container invoking callback methods that application code can use to obtain resources.

❑ Dependency Injection: Components do no look up; they provide plain Java methods enabling
the container to resolve dependencies. The container is wholly responsible for wiring up components, passing resolved objects in to JavaBean properties or constructors. Use of JavaBean
properties is called Setter Injection; use of constructor arguments is called Constructor Injection

Ioc 的职责

Inversion of control serves the following design purposes:

  • To decouple the execution of a task from implementation.
  • To focus a module on the task it is designed for.
  • To free modules from assumptions about how other systems do what they do and instead rely on contracts.
  • To prevent side effects when replacing a module.

Inversion of control is sometimes facetiously referred to as the "Hollywood Principle: Don't call us, we'll call you".

– https://en.wikipedia.org/wiki/Inversion_of_control


  • 通用职责
  • 依赖处理
    • 依赖注入
    • 依赖查找
  • 生命周期管理
    • 容器的生命周期
    • 托管的资源(Java Beans 或 其他资源:event …)
  • 配置
    • 容器
    • 外部化配置
    • 托管的资源

Ioc 容器的主要实现

  • Java SE

    • Java Beans
    • Java ServiceLoader SPI
    • JNDI (java Naming and Directory Interface)
  • Java EE
    • EJB(Enterprise Java Beans)
    • Servlet
  • 开源
    • Apache Avalon (close)
    • PicoContainer
    • Google Guice
    • Spring Framework

Java Beans 做为 ioc 容器

  • 特性

    • 依赖查找
    • 生命周期管理
    • 配置元信息
    • 事件
    • 自定义
    • 资源管理
    • 持久化
  • 规范
    • JavaBeans
    • BeanContext
    • http://download.oracle.com/javase/tutorial/javabeans/
    • https://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html

如何界定 IOC 容器的轻重

《Expert One-on-OneTM J2EETM Development without EJBTM》的第六章第一小节

依赖查找 VS 依赖注入

使用时要因地制宜

构造器注入和 Setter 注入

小马哥讲Spring核心编程思想 - 第二章 重新认识IoC相关推荐

  1. 《小马哥讲Spring核心编程思想》-第一章学习笔记(1)

    <小马哥讲Spring核心编程思想>-第一章学习笔记(1) 一.课程介绍 1.为什么要学习spring? 2.深入学习spring的难点有哪些? 3.课程的设计思路是怎样的? 二.内容综述 ...

  2. 小马哥spring编程核心思想_小马哥讲Spring核心编程思想

    小马哥讲Spring核心编程思想 ├─第01章:Spring Framework总览 (12讲) │      01丨课程介绍.mp4 │      02丨内容综述.mp4 │      03丨课前准 ...

  3. 小马哥讲 Spring 核心编程思想-课程学习

    https://time.geekbang.org/course/detail/100042601-179534 Spring版本特性 Spring版本 Java标准版 Java企业版 1.x aop ...

  4. Spring Boot 核心编程思想-第二部分-读书笔记

    怕什么真理无穷 进一步有近一步的欢喜 说明 本文是Spring Boot核心编程思想记录的笔记,书籍地址:Spring Boot编程思想(核心篇): 这篇文档会记录这本我的一些读书的思考,内容可能比较 ...

  5. 小马哥spring编程核心思想_极客小马哥Spring核心编程思想

    42 | 依赖查找的今世前生:Spring IoC容器从Java标准中学到了什么? 43 | 单一类型依赖查找:如何查找已知名称或类型的Bean对象? 44 | 集合类型依赖查找:如何查找已知类型多个 ...

  6. Spring核心编程思想

    第01章:Spring Framework总览 (12讲)       01.课程介绍.mp4       02.内容综述.mp4       03.课前准备:学习三件套(工具.代码与大脑).mp4 ...

  7. spring核心编程思想—beanfacotry和applicationcontext区别

    文章目录 beanfacotry和applicationcontext区别 先看官网 Introduction to the Spring IoC Container and Beans 翻译 类图 ...

  8. 【0】python核心编程,第二章

    1.print语句也支持将输入重定向到文件,示例: 1 logfile = open('/tmp/mylog.txt', 'a') 2 print >> logfile, 'Fatal e ...

  9. 小马哥spring编程核心思想_求小马哥讲Spring栈核心编程思想 Spring IoC+Bean+Framework教程...

    这次搜集了下小马哥讲Spring栈核心编程思想 Spring IoC+Bean+Framework,最强Spring全面核心实战的视频教程,有需要的朋友可以自行下载学习. 课程简介: 小马哥出手的Sp ...

  10. [201903][Spring Boot 编程思想][核心篇][小马哥][著]

    [201903][Spring Boot 编程思想][核心篇][小马哥][著] The Java Community Process(SM) Program https://jcp.org/en/ho ...

最新文章

  1. JAVA07 接口与继承
  2. YUV图像实时去雾算法的优化与改进
  3. 子类可以继承到父类上的注解吗
  4. 11g cursor_sharing 参数说明
  5. android studio访问webservice如何传递类对象报错_小白学习web service,这是最最最基础的了,只用JDK还不会吗?...
  6. 【Qt教程】1.4 - Qt5第一个控件 按钮QPushButton 指定父对象
  7. SQL SERVER数据库 查询sql 多表联查 top命令
  8. geotools读取shp数据的数据类型
  9. 小学三年级英语听力测试软件,人教小学三年级英语(下)期末检测题+听力测试...
  10. python3之url编码互转
  11. Mugeda(木疙瘩)H5案例课—教学课件如何做-岑远科-专题视频课程
  12. opencv RGB 颜色 灰色图片显示
  13. Get “https://github.com/electron-userland/electron-builder-binaries/releases/download/appimage-12.0.
  14. python语音识别库kaldi_Kaldi 语音识别基础教程
  15. 【JUC源码专题】Striped64 核心源码分析(JDK8)
  16. 同主机容器基于XDP的连通性实验
  17. MongoDB菜鸟教程
  18. 中国人必须知道的76个常识。。。看了才发现,自己知道的实在太少了
  19. 电视PPTV服务器响应异常,pptv出现异常错误怎么解决
  20. [更新中]【超强搞笑语录】给大家开心一下,不看你一定后悔

热门文章

  1. 如何进行敌我识别(IFF)应用中的二次监视雷达(SSR)的模块化产线测试
  2. 有道词典Chrome划词插件
  3. 如何使用网易有道词典翻译英文文档(word格式、pdf格式)
  4. IntelliJ IDEA插件-翻译插件
  5. HBase二级索引的设计
  6. linux打印重定向并打印机,将虚拟打印机的输出重定向到Python脚本
  7. 傅里叶变换 FFT 频谱泄漏?
  8. 窥探PTAM之模板搜索
  9. (minio学习过程2)纠错码
  10. 二维码的纠错码原理及细节