spring容器管理 ——The IoC Container官网原文解析(1)
创始人
2024-06-01 19:04:38
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
这些东西,对于我们自定义容器使用非常重要!!,待会细说

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...