Introduction to the Spring IoC Container and Beans
本章涵盖了 Spring Framework 实现的控制反转 (IoC) 原则。依赖注入 (DI) 是 IoC 的一种特殊形式,其中对象仅通过构造函数参数、工厂方法参数或在从工厂方法构建或返回后设置在对象实例上的属性定义其依赖项(即他们合作的其他对象)。然后,IoC 容器在其创建 bean 时注入这些依赖项。此过程从根本上讲是 bean 本身通过直接构建类或服务定位器模式等机制控制其依赖项的实例化或位置的反向(因此称为控制反转)。
This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) principle. Dependency injection (DI) is a specialized form of IoC, 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 IoC 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.
org.springframework.beans
和 org.springframework.context
包是 Spring Framework IoC 容器的基础。https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/BeanFactory.html[BeanFactory
]接口提供一种高级配置机制,能够管理任何类型的对象。https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationContext.html[ApplicationContext
]是 BeanFactory
的子接口。它添加:
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.
简而言之,BeanFactory
提供配置框架和基本功能,并且 ApplicationContext
添加更多企业特定功能。ApplicationContext
是 BeanFactory
的完整超集,并且在本节中专门用于描述 Spring 的 IoC 容器。有关使用 BeanFactory
而不是 ApplicationContext,
的更多信息,请参阅涵盖 BeanFactory
API 的部分。
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 section covering the
BeanFactory
API.
在 Spring 中,形成应用程序主干并由 Spring IoC 容器管理的对象称为 Bean。Bean 是由 Spring IoC 容器实例化、组装和管理的对象。否则,Bean 仅仅是应用程序中的众多对象之一。Bean 及其之间的依赖关系反映在容器使用的配置元数据中。
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 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.