SpringBoot-Term-Definaition-BeanFactory
BeanFactory
1 |
|
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.
其子接口ListableBeanFactory
与ConfigurableBeanFactory
,对功能进行了拓展,具体有哪些拓展等后续介绍完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 |
|
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.
ListableBeanFactory
是BeanFactory
的扩展。在原有基础上新增了获取枚举容器中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 |
|
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 |
|
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 |
|
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.
ConfigurableBeanFactory
是HierarchicalBeanFactory
和SingletonBeanRegistry
的扩展,额外拓展了对BeanFactory
进行配置的功能。
那么这个配置指什么,查看ConfigurableBeanFactory
新增的方法声明,parentBeanFactory
、beanClassLoader
、tempClassLoader
、cacheBeanMetadata
...
这里出现了很多新的细节与概念,暂且搁置。而且通常用户层面更多使用BeanFactory
和ListableBeanFactory
访问容器,很少会有对容器进行配置的需求,因此此部分内容只是简单描述。
AutowireCapableBeanFactory
1 |
|
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
,接口还提供了自动装配以外的功能,例如执行容器的回调setBeanName
、setBeanFactory
,或者执行BeanPostProcessors
。
ConfigurableListableBeanFactory
1 |
|
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
和预实例化单实例的功能。由于此接口提供的方法相对简单,不多赘述。