SpringBoot-Term-Definaition-BeanFactory

BeanFactory

1
2
package org.springframework.beans.factory
public interface BeanFactory

The root interface for accessing a Spring bean container.

今天讲到Spring中最关键的概念BeanFactory。从注释中可以得到,beanFactory就是平常说的容器,后文会不加区分使用beanFactory和容器。

This is the basic client view of a bean container; further interfaces such as ListableBeanFactory and org.springframework.beans.factory.config.ConfigurableBeanFactory are available for specific purposes.

其子接口ListableBeanFactoryConfigurableBeanFactory,对功能进行了拓展,具体有哪些拓展等后续介绍完BeanFactory再说。

This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. Depending on the bean definition, the factory will return either an independent instance of a contained object (the Prototype design pattern), or a single shared instance (a superior alternative to the Singleton design pattern, in which the instance is a singleton in the scope of the factory). Which type of instance will be returned depends on the bean factory configuration: the API is the same. Since Spring 2.0, further scopes are available depending on the concrete application context (e.g. "request" and "session" scopes in a web environment).

BeanFactory的最主要的功能,就是储存了一系列BeanDefinition,并且每个都使用了一个唯一的字符串来标识。我们可以通过这个唯一字符串,检索到BeanDefinition,并根据此来决定是使用已有的对象(Singleton),还是新建一个对象(Prototype)。

容器中大部分BeanDefinition,都是Singleton类型,因此BeanDefinition和大部分对象也是一一对应的关系,从而字符串到大部分对象也是一一对应的关系。所以常规中,我们会认为容器完成了字符串到对象的映射。

The point of this approach is that the BeanFactory is a central registry of application components, and centralizes configuration of application components (no more do individual objects need to read properties files, for example). See chapters 4 and 11 of "Expert One-on-One J2EE Design and Development" for a discussion of the benefits of this approach.

容器是一种把应用中用到的所有组件,中心化配置的一种设计理念。并引用了一本书中的内容来说明这么做的好处。在此不多赘述。

Note that it is generally better to rely on Dependency Injection ("push" configuration) to configure application objects through setters or constructors, rather than use any form of "pull" configuration like a BeanFactory lookup. Spring's Dependency Injection functionality is implemented using this BeanFactory interface and its subinterfaces.

spring应用中,可以在对象初始化时,或在对象创建完后,通过构造函数或setter方法完成依赖注入,把依赖注册为成员变量。这是更推荐的“推式”注入。与之对应的是在方法内需要用到时,通过BeanFactory的查询方法获取对象,此为不推荐的“拉式”注入。无论是“推式”还是“拉式”,都会使用到BeanFactory来完成功能,因此两者的最大区别是注入的时机。广泛使用的@Resource@Autowired就是“推式”注入。

Normally a BeanFactory will load bean definitions stored in a configuration source (such as an XML document), and use the org.springframework.beans package to configure the beans. However, an implementation could simply return Java objects it creates as necessary directly in Java code. There are no constraints on how the definitions could be stored: LDAP, RDBMS, XML, properties file, etc. Implementations are encouraged to support references amongst beans (Dependency Injection).

BeanFactory获取对象,先加载BeanDefinition,并使用SpringBean包来创建具体对象并返回,这不是绝对的。在某些情况BeanDefinition来源特殊,可以越过上述过程,直接获取对象。

In contrast to the methods in ListableBeanFactory, all of the operations in this interface will also check parent factories if this is a HierarchicalBeanFactory. If a bean is not found in this factory instance, the immediate parent factory will be asked. Beans in this factory instance are supposed to override beans of the same name in any parent factory.

HierarchicalBeanFactory会同时检查父容器,而ListableBeanFactory不会。当存在多级容器时,会遵循“就近原则”,子容器的同名BeanDefination会覆盖父容器。

以上是BeanFactory的注释内容。查看BeanFactory中定义的方法,基本就是根据名称/类型获取对象,根据名称检查BeanDefination是否存在和其属性值。总的来说,容器的功能就是储存名称和BeanDefination,并可以通过名称获取BeanDefination对应的对象。

ListableBeanFactory

1
2
org.springframework.beans.factory;
public interface ListableBeanFactory extends BeanFactory

Extension of the BeanFactory interface to be implemented by bean factories that can enumerate all their bean instances, rather than attempting bean lookup by name one by one as requested by clients. BeanFactory implementations that preload all their bean definitions (such as XML-based factories) may implement this interface.

ListableBeanFactoryBeanFactory的扩展。在原有基础上新增了获取枚举容器中BeanDefinition数量、名称的功能。

If this is a HierarchicalBeanFactory, the return values will not take any BeanFactory hierarchy into account, but will relate only to the beans defined in the current factory. Use the BeanFactoryUtils helper class to consider beans in ancestor factories too.

BeanFactory不同的是,ListableBeanFactory扩展的功能,不会考虑层级结构,也就是不会搜索父容器中定义的BeanDefinition

The methods in this interface will just respect bean definitions of this factory. They will ignore any singleton beans that have been registered by other means like org.springframework.beans.factory.config.ConfigurableBeanFactory's registerSingleton method, with the exception of getBeanNamesForType and getBeansOfType which will check such manually registered singletons too. Of course, BeanFactory's getBean does allow transparent access to such special beans as well. However, in typical scenarios, all beans will be defined by external bean definitions anyway, so most applications don't need to worry about this differentiation.

ListableBeanFactory扩展的功能基本只考虑根据BeanDefinition创建的bean。通过其他方法例如registerSingleton注册进容器的bean,不予考虑。

总的来说,ListableBeanFactory的扩展就在于Listable上,使得容器增加了枚举所有BeanDefinition的功能。

HierarchicalBeanFactory

1
2
package org.springframework.beans.factory;
public interface HierarchicalBeanFactory extends BeanFactory

Sub-interface implemented by bean factories that can be part of a hierarchy.

The corresponding setParentBeanFactory method for bean factories that allow setting the parent in a configurable fashion can be found in the ConfigurableBeanFactory interface.

HierarchicalBeanFactory接口非常简单,在BeanFactory上扩展了层级功能。容器可以指定其父容器。为了区分是否搜索父容器的场景,新增了containsLocalBean方法。见名知意,这个方法不搜索父容器,而BeanFactory中声明的containsBean则会搜索父容器。

SingletonBeanRegistry

1
2
package org.springframework.beans.factory.config
public interface SingletonBeanRegistry

Interface that defines a registry for shared bean instances. Can be implemented by org.springframework.beans.factory.BeanFactory implementations in order to expose their singleton management facility in a uniform manner.

SingletonBeanRegistry看似和BeanFactory没有直接关系,但通常被BeanFactory的实现类同时实现,以提供把某个bean实例注册进容器中。

注册bean实例和上文提到的大部分注册beanDefinition的主要区别是,注册的实例默认为单实例模式,并且默认已完成初始化。已完成初始化意味着,BeanFactory不会对其进行生命周期控制,不会执行初始化、销毁函数的回调。

SingletonBeanRegistry还提供了查看容器内已初始化的Singleton,由于不是很重要,在此不再赘述。

ConfigurableBeanFactory

1
2
package org.springframework.beans.factory.config
public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry

Configuration interface to be implemented by most bean factories. Provides facilities to configure a bean factory, in addition to the bean factory client methods in the BeanFactory interface.

This bean factory interface is not meant to be used in normal application code: Stick to BeanFactory or org.springframework.beans.factory.ListableBeanFactory for typical needs. This extended interface is just meant to allow for framework-internal plug'n'play and for special access to bean factory configuration methods.

ConfigurableBeanFactoryHierarchicalBeanFactorySingletonBeanRegistry的扩展,额外拓展了对BeanFactory进行配置的功能。

那么这个配置指什么,查看ConfigurableBeanFactory新增的方法声明,parentBeanFactorybeanClassLoadertempClassLoadercacheBeanMetadata...

这里出现了很多新的细节与概念,暂且搁置。而且通常用户层面更多使用BeanFactoryListableBeanFactory访问容器,很少会有对容器进行配置的需求,因此此部分内容只是简单描述。

AutowireCapableBeanFactory

1
2
package org.springframework.beans.factory.config
public interface AutowireCapableBeanFactory extends BeanFactory

Extension of the BeanFactory interface to be implemented by bean factories that are capable of autowiring, provided that they want to expose this functionality for existing bean instances.

This subinterface of BeanFactory is not meant to be used in normal application code: stick to BeanFactory or org.springframework.beans.factory.ListableBeanFactory for typical use cases.

AutowireCapableBeanFactory关注自动装配相关的功能。通常是对那些在容器外部创建bean实例执行自动装配功能,内部的bean可以通过@AutoWired@Resource来配置需要使用自动装配功能的地方。

虽然名称叫AutowireCapableBeanFactory,接口还提供了自动装配以外的功能,例如执行容器的回调setBeanNamesetBeanFactory,或者执行BeanPostProcessors

ConfigurableListableBeanFactory

1
2
package org.springframework.beans.factory.config
public interface ConfigurableListableBeanFactory extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory

Configuration interface to be implemented by most listable bean factories. In addition to ConfigurableBeanFactory, it provides facilities to analyze and modify bean definitions, and to pre-instantiate singletons.

从继承关系来看,ConfigurableListableBeanFactory就是上文提到三个的合体版。此外,提供了分析修改BeanDefinition和预实例化单实例的功能。由于此接口提供的方法相对简单,不多赘述。


SpringBoot-Term-Definaition-BeanFactory
http://dracoyus.github.io/2024/04/23/SpringBoot-Term-Definaition-BeanFactory/
作者
DracoYu
发布于
2024年4月23日
许可协议