# 1.1 IoC容器和Beans介绍

本章介绍了Spring框架中控制反转（IOC）原理的实现。IOC也称为依赖注入（DI）。它是指对象仅通过构造函数参数、工厂方法的参数或从工厂方法构造或返回对象实例后，通过在其上设置的属性来定义其依赖项（即与之一起工作的其他对象）的过程。当容器在创建bean时将会注入这些依赖项。

这个过程跟bean自己控制实例化，或者通过直接构造类或服务定位器模式来定位其依赖项的方式相反（因此称为控制反转）。

org.spring framework.beans和org.springframework.context包是Springframework的IOC容器的基础。

[BeanFactory](https://docs.spring.io/spring-framework/docs/5.1.8.RELEASE/javadoc-api/org/springframework/beans/factory/BeanFactory.html)接口提供了一种高级配置机制，能够管理任何类型的对象。

[ApplicationContext](https://docs.spring.io/spring-framework/docs/5.1.8.RELEASE/javadoc-api/org/springframework/context/ApplicationContext.html)是BeanFactory的子接口。它添加了如下功能：

* 更容易与Spring的AOP功能集成
* 消息资源处理（用于国际化）
* 事件发布
* 应用层特定的上下文，如用于Web应用程序的WebApplicationContext

简而言之，BeanFactory提供了配置框架和基本功能，ApplicationContext添加了更多特定于企业应用的功能。ApplicationContext是BeanFactory的一个完整的超集，在本章中专门用于描述Spring的IOC容器。有关使用BeanFactory的更多信息，请参见[BeanFactory](https://docs.spring.io/spring-framework/docs/5.1.8.RELEASE/javadoc-api/org/springframework/beans/factory/BeanFactory.html)。

在Spring中，构成应用程序主干并由SpringIOC容器管理的对象称为bean。bean是一个由SpringIOC容器实例化、组装和管理的对象。否则，bean只是应用程序中许多对象之一。bean以及它们之间的依赖关系反映在容器使用的配置元数据中。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flydean.com/spring-framework-documentation5/core-technologies/1.the-ioc-container/1.1introduction-to-the-spring-ioc-container-and-beans.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
