原文及翻译
This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) principle.
本章涵盖了spring框架中关于IOC原理的实现
IoC is also known as dependency injection (DI).
IOC也叫依赖注入(DI)。
It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.
The container then injects those dependencies when it creates the bean.
This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes or a mechanism such as the Service Locator pattern.
@frameStart@
@frameTitle@我最帅@frameTitle@
它是一个这样的过程:
对象通过构造器参数,工厂方法·参数,对象实例的属性来定义他们的依赖,然后容器就在创建bean时注入这些依赖。
这个过程从根本上来说就是这样的反转:bean自己通过使用构造器或者一种机制(比如Service Locator pattern)控制他们依赖的初始化或者位置。
注:反转的就是就是不需要bean自己控制了,而是由容器来控制。
@frameEnd@
The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object.
ApplicationContext is a sub-interface of BeanFactory. It adds:
- Easier integration with Spring’s AOP features
- Message resource handling (for use in internationalization)
- Event publication
- Application-layer specific contexts such as the WebApplicationContext for use in web applications.
@frameStart@
@frameTitle@我最帅@frameTitle@
org.springframework.beans包和org.springframework.context 包是IOC容器的基础。BeanFactory 接口提供了一个能够管理任意类型对象的先进的配置机制。
ApplicationContext 是BeanFactory的子类,它新增了一下几个功能点:
- 更容易和Spring AOP 功能点整合
- 消息资源处理(为了国际化中的使用)
- 事件发布
- 应用层面上的特定上下文,比如web应用中的会使用WebApplicationContext
@frameEnd@
In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality.
The ApplicationContext is a complete superset of the BeanFactory and is used exclusively in this chapter in descriptions of Spring’s IoC container.
For more information on using the BeanFactory instead of the ApplicationContext, see The BeanFactory.
@frameStart@
@frameTitle@我最帅@frameTitle@
简言之,BeanFactory 提供了配置框架和一些别的基本的功能,而ApplicationContext则在此基础上增加了一些适用于企业应用中的功能。
由于ApplicationContext完完整整的继承了BeanFactory的功能点,本章中将只使用前者来说明spring IOC容器。
如果想了解更多的关于BeanFactory而不是ApplicationContext的信息,请看The BeanFactory.
@frameEnd@
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application.
Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
@frameStart@
@frameTitle@我最帅@frameTitle@
在spring中,beans是这样的对象:组成你的应用的骨架,并且受spring ioc容器的管理。
bean的初始化,组装和其他都受spring ioc 容器的管理;否则bean就仅仅只是你应用中众多对象的一个。
bean以及他们之间的依赖关系都反映在容器所使用的配置元数据中
@frameEnd@
个人补充
本章主要介绍了spring ioc容器的概念,applicationcontext和beanfactory的区别以及bean的定义,bean和容器的关系。
下一章我们将主要介绍一下容器的概念。