Skip to content

Latest commit

 

History

History
16 lines (9 loc) · 4.36 KB

31-spring-ioc容器和bean的简介.md

File metadata and controls

16 lines (9 loc) · 4.36 KB

3.1 Spring IoC容器和bean的简介

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (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 (IoC), 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.

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; and application-layer specific contexts such as the WebApplicationContext for use in web applications.

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, refer to Section 3.16, “The BeanFactory”.

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.

本章介绍了Spring框架实现的控制反转(IoC)[1]原理。 IoC也称为依赖注入(DI)。它是一个过程,对象通过构造函数参数,工厂方法的参数或在对象实例构造或从工厂方法返回后在对象实例上设置的属性来定义它们的依赖关系,即它们一起合作的其他对象。 然后容器在创建bean时注入那些依赖。这个过程基本上是相反的,因此称为控制反转(IoC),通过使用类的直接构造或诸如服务定位器模式的机制来控制其依赖性的实例化或位置的bean自身的名称。

org.springframework.beansorg.springframework.context包是Spring FrameworkIoC容器的基础。 BeanFactory接口提供了一种能够管理任何类型的对象的高级配置机制。 ApplicationContextBeanFactory的子接口。它增加了与SpringAOP特性的更容易的集成到一起的实现;消息资源处理(用于国际化),事件发布;和应用程序层特定上下文(如WebApplicationContext)以用于Web应用程序。

简而言之,BeanFactory提供了配置框架和基本功能,ApplicationContext添加了更多的企业特定功能。 ApplicationContextBeanFactory的完整超集,并且在本章中专门用于描述Spring的IoC容器。有关使用BeanFactory而不是ApplicationContext的更多信息,请参见第3.16节“BeanFactory”。

Spring中,构成应用程序主干并由Spring IoC容器管理的对象称为beanbean是由Spring IoC容器实例化,组装和以其他方式管理的对象。此外,bean只是应用程序中许多对象之一。 Bean及其之间的依赖关系被容器所使用的配置元数据所反射 。