IoC 的简史
• 1983年,Richard E. Sweet 在《The Mesa Programming Environment》中提出“Hollywood Principle”(好莱坞原则)
好莱坞原则:不要来打电话给我,我会打电话给你
这里的“我”指的是资源,你就是应用系统(模块)
• 1988年,Ralph E. Johnson & Brian Foote 在《Designing 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 主要实现策略
• 维基百科(https://en.wikipedia.org/wiki/Inversion_of_control)
Implementation techniques 小节的定义:
“In object-oriented programming, there are several basic techniques to implement inversion of control. These are:
在面向对象编程中,这里有几种基本的技术实现 IoC
• Using a service locator pattern 服务定位模式
• Using dependency injection, for example 依赖注入
• Constructor injection 构造器注入
• Parameter injection 参数注入
• Setter injection setter方法注入
• Interface injection 接口注入
• Using a contextualized lookup 上下文依赖查询
• Using template method design pattern 模版方法设计模式
• Using strategy design pattern 策略模式
文献:《Expert One-on-One™ J2EE™ Development without EJB™》提到的主要实现策略:
“IoC is a broad concept that can be implemented in different ways. There are two main types:
Dependency Lookup 依赖查找
• 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 依赖注入
• 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 容器的职责
• 维基百科(https://en.wikipedia.org/wiki/Inversion_of_control) 在 Overview 小节中提到:
“Inversion of control serves the following design purposes:
IoC 服从以下几个设计的目的
• 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”.”
IoC 容器的实现
• 主要实现
• Java SE
• Java Beans
• Java ServiceLoader SPI
• JNDI(Java Naming and Directory Interface) • Java EE
• EJB(Enterprise Java Beans) • Servlet
• 开源
• Apache Avalon(http://avalon.apache.org/closed.html) • PicoContainer(http://picocontainer.com/) • Google Guice(https://github.com/google/guice) • Spring Framework(https://spring.io/projects/spring-framework)
传统 IoC 容器的实现
• Java Beans 作为 IoC 容器
• 特性
• 依赖查找
• 生命周期管理
• 配置元信息 propertyDescriptor
• 事件
• 自定义
• 资源管理
• 持久化
• 规范
• JavaBeans:https://www.oracle.com/technetwork/java/javase/tech/index-jsp-138795.html
• BeanContext:https://docs.oracle.com/javase/8/docs/technotes/guides/beans/spec/beancontext.html
什么是 Java Beans?
简单的理解可以为是一个java的 POJO
在java beans中 对getter 与 setter 理解为 writable 与 readable