spring核心编程思想—beanfacotry和applicationcontext区别

beanfacotry和applicationcontext区别

先看官网

官网
https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/spring-framework-reference/core.html

Introduction to the Spring IoC Container and Beans

This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) 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) 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
  • 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, see[beans-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.

翻译

本章介绍控制反转(IoC)原则的Spring框架实现。
IoC也称为依赖注入(dependency injection,DI)。这是一个过程,在这个过程中,对象只通过构造函数参数、工厂方法的参数或在对象实例构造或从工厂方法返回后在对象实例上设置的属性来定义它们的依赖项(即它们使用的其他对象)。然后容器在创建bean时注入这些依赖项。这个过程基本上与bean本身相反(因此称为控制反转),它使用类的直接构造或服务定位器模式等机制来控制其依赖项的实例化或位置。
这个org.springframework.beans以及org.springframework.context包是Spring框架的IoC容器的基础。BeanFactory接口提供了一种高级配置机制,能够管理任何类型的对象。ApplicationContext是BeanFactory的一个子接口。它补充道:

  • 更容易与Spring的AOP特性集成
  • 消息资源处理(用于国际化)
  • 事件发布
  • 特定于应用程序层的上下文,如用于web应用程序的WebApplicationContext。

简而言之,BeanFactory提供了配置框架和基本功能,而ApplicationContext则添加了更多特定于企业的功能。ApplicationContext是BeanFactory的一个完整的超集,在本章中专门用于描述Spring的IoC容器。有关使用BeanFactory而不是ApplicationContext的更多信息,请参阅[BeanBeanFactory]。

类图

ApplicationContext类图
在这里插入图片描述
DefaultListableBeanFactory类图
在这里插入图片描述

总结

• BeanFactory 是Spring 底层IoC 容器
• ApplicationContext 是具备应用特性的BeanFactory 超集

猜你喜欢

转载自blog.csdn.net/e891377/article/details/109308326