spring容器管理 ——The IoC Container官网原文解析(1)
创始人
2024-06-01 19:18:22
0

【前情提要】项目中涉及到一个实现类似spring bean管理功能的框架

对于像我这样的新手来说,网上那些关于spring的容器的描述、教程,都太过于抽象,或只言片语,模式化、套路化,非常难理解。所以本着自己动手的思想,这个专栏,要好好梳理一下这个东西。

Q1:容器和bean 框架中整个流程是什么样?

Q2:要用到哪些类、接口?

Q3:用到什么设计思想,和设计模式?

Q3:如何定制化?

在spring官网中: 我们找到关于spring framework的首页 ,看到Feature
核心技术:依赖注入、事件、资源、i18n、验证、数据绑定、类型转换、SpEL、AOP。

我们知道,容器的核心思想 就是 IOC (控制反转),而IOC的根本实现手段,就是 依赖注入(DI)

我们继续点入Core Technologies 看看
在这里插入图片描述

1.1. Introduction to the Spring IoC Container and Beans (ioc容器 和Bean的总体介绍)

看看这段描述
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.

【小结】
两个关键的包:
org.springframework.beans org.springframework.context

BeanFactory接口提供了一种高级配置机制,能够管理任何类型的对象。
ApplicationContext是BeanFactory的子接口。它是BeanFactory的完整超集。

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.

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.

【小结】
BeanFactory 提供了配置框架和基本功能
ApplicationContext 添加了更多特定于企业的功能 (enterprise-specific)

在Spring中,构成应用程序主干并由Spring IoC容器管理的对象称为bean。
【解释】 你的service ,Component, Configuration 不就是整个程序的核心主干吗?(backbone)。
以前获取他们的实例,你要去new ,现在统统交给ioc容器去 实例化、组装、管理。
bean以及它们之间的依赖关系反映在容器使用的配置元数据中。(注意这个配置元数据的名称,下文会解释)

1.2. Container Overview (容器总览)

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It lets you express the objects that compose your application and the rich interdependencies between those objects..
【小结】强调!!!
ApplicationContext 接口 就是容器!!!
ApplicationContext 接口 就是容器!!!
ApplicationContext 接口 就是容器!!!

容器通过读取配置元数据来获得关于实例化、配置和组装哪些对象的指令。
配置元数据以XML、Java注释或Java代码表示。
它允许您表达组成应用程序的对象以及这些对象之间丰富的相互依赖关系。

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext. While XML has been the traditional format for defining configuration metadata, you can instruct the container to use Java annotations or code as the metadata format by providing a small amount of XML configuration to declaratively enable support for these additional metadata formats.
【小结】 ApplicationContext 接口的实现类,在不同场景下来完成对应的配置元数据的获取,比如 :

在这里插入图片描述
在这里插入图片描述
从IDEA上看到的继承关系 和官网API(同版本) 有一定的出入

In most application scenarios, explicit user code is not required to instantiate one or more instances of a Spring IoC container.

【小结】回忆一下,spring项目如何实例化一个容器的?
正常情况下,我们并不需要去显示的实例化一个容器,这些事情,都是项目启动时底层帮我们完成的。
我们要做的事,就是去配置文件、注解中,配置好,我们需要交给容器的各个Bean,剩下的事就交给容器去完成。

官方给了一张图:
容器把我们自己的业务类 和配置元数据相关联,当ApplicationContext初始化之后,然后生产可以使用的bean

容器消费生产模型
在这里插入图片描述

1.2.1. Configuration Metadata

As the preceding diagram shows, the Spring IoC container consumes a form of configuration metadata. This configuration metadata represents how you, as an application developer, tell the Spring container to instantiate, configure, and assemble the objects in your application.
【小结】IOC容器消费配置元数据,并生产可用的bean (请在大脑里保持这种消费生产模型)
我们通过 配置元数据 告诉Spring容器,如何实例化、配置和组装 应用程序中的对象

所谓的配置元数据,我们在此 理解为 xml文件,就是你开发spring项目里 resource下的 那个 配置Bean的 applicationContext.xml,或者你常用的那些注解
@Component @Service等

官网中有这么一个note,来描述配置元数据:
配置元数据

1.2.2. Instantiating a Container (实例化一个容器)

这里暂时没啥好说的。根据不同容器,传入资源路径

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
1.2.3. Using the Container

我们重点看一下这一段:
The most flexible variant is GenericApplicationContext in combination with reader delegates — for example, with XmlBeanDefinitionReader for XML files, as the following example shows:

GenericApplicationContext 是适用性最广的容器,见名知义。

GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();

这里出现了几个重要的东西。beanDefinitionReader context的refresh
这些东西,对于我们自定义容器使用非常重要!!,待会细说

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...