spring beans源码解读之--BeanFactory的注册
beanFactory的继承关系如下图所示:

(图片来源:http://www.myexception.cn/software-architecture-design/925888.html)
在上节beanFactory的进化史,我们就讲到了上图的左边部分,这次我们来分析一下图的右边部分。AliasRegistry 是一个用于别名管理的通用接口,BeanDefinitionRegistry继承了该接口。
SimpleAliasRegistry作为一个基类,实现了AliasRegistry接口。
SingletonBeanRegistry是spring 提供的一个共享bean的注册接口。实现该接口可以方便对单例进行管理。
public interface SingletonBeanRegistry {
/**
* Register the given existing object as singleton in the bean registry,
* under the given bean name.
* <p>The given instance is supposed to be fully initialized; the registry
* will not perform any initialization callbacks (in particular, it won't
* call InitializingBean's {@code afterPropertiesSet} method).
* The given instance will not receive any destruction callbacks
* (like DisposableBean's {@code destroy} method) either.
* <p>When running within a full BeanFactory: <b>Register a bean definition
* instead of an existing instance if your bean is supposed to receive
* initialization and/or destruction callbacks.</b>
* <p>Typically invoked during registry configuration, but can also be used
* for runtime registration of singletons. As a consequence, a registry
* implementation should synchronize singleton access; it will have to do
* this anyway if it supports a BeanFactory's lazy initialization of singletons.
* @param beanName the name of the bean
* @param singletonObject the existing singleton object
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.DisposableBean#destroy
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#registerBeanDefinition
*/
void registerSingleton(String beanName, Object singletonObject);
/**
* Return the (raw) singleton object registered under the given name.
* <p>Only checks already instantiated singletons; does not return an Object
* for singleton bean definitions which have not been instantiated yet.
* <p>The main purpose of this method is to access manually registered singletons
* (see {@link #registerSingleton}). Can also be used to access a singleton
* defined by a bean definition that already been created, in a raw fashion.
* <p><b>NOTE:</b> This lookup method is not aware of FactoryBean prefixes or aliases.
* You need to resolve the canonical bean name first before obtaining the singleton instance.
* @param beanName the name of the bean to look for
* @return the registered singleton object, or {@code null} if none found
* @see ConfigurableListableBeanFactory#getBeanDefinition
*/
Object getSingleton(String beanName);
/**
* Check if this registry contains a singleton instance with the given name.
* <p>Only checks already instantiated singletons; does not return {@code true}
* for singleton bean definitions which have not been instantiated yet.
* <p>The main purpose of this method is to check manually registered singletons
* (see {@link #registerSingleton}). Can also be used to check whether a
* singleton defined by a bean definition has already been created.
* <p>To check whether a bean factory contains a bean definition with a given name,
* use ListableBeanFactory's {@code containsBeanDefinition}. Calling both
* {@code containsBeanDefinition} and {@code containsSingleton} answers
* whether a specific bean factory contains a local bean instance with the given name.
* <p>Use BeanFactory's {@code containsBean} for general checks whether the
* factory knows about a bean with a given name (whether manually registered singleton
* instance or created by bean definition), also checking ancestor factories.
* <p><b>NOTE:</b> This lookup method is not aware of FactoryBean prefixes or aliases.
* You need to resolve the canonical bean name first before checking the singleton status.
* @param beanName the name of the bean to look for
* @return if this bean factory contains a singleton instance with the given name
* @see #registerSingleton
* @see org.springframework.beans.factory.ListableBeanFactory#containsBeanDefinition
* @see org.springframework.beans.factory.BeanFactory#containsBean
*/
boolean containsSingleton(String beanName);
/**
* Return the names of singleton beans registered in this registry.
* <p>Only checks already instantiated singletons; does not return names
* for singleton bean definitions which have not been instantiated yet.
* <p>The main purpose of this method is to check manually registered singletons
* (see {@link #registerSingleton}). Can also be used to check which singletons
* defined by a bean definition have already been created.
* @return the list of names as a String array (never {@code null})
* @see #registerSingleton
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionNames
* @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionNames
*/
String[] getSingletonNames();
/**
* Return the number of singleton beans registered in this registry.
* <p>Only checks already instantiated singletons; does not count
* singleton bean definitions which have not been instantiated yet.
* <p>The main purpose of this method is to check manually registered singletons
* (see {@link #registerSingleton}). Can also be used to count the number of
* singletons defined by a bean definition that have already been created.
* @return the number of singleton beans
* @see #registerSingleton
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry#getBeanDefinitionCount
* @see org.springframework.beans.factory.ListableBeanFactory#getBeanDefinitionCount
*/
int getSingletonCount();
}
注册接口的默认实现为:DefaultSingletonBeanRegistry,主要作用是提供能被所有调用者共享bean的注册,调用者可用通过bean 名称获取到该bean的实例。同时该类也提供了对注册bean的销毁方法。
DefaultSingletonBeanRegistry也实现了AliasRegistry接口,增加了对别名的管理。
FactoryBeanRegistrySupport 集成了DefaultSingletonBeanRegistry对bean的单例bean的管理功能,是一个基类,用来处理FactoryBean实例的单例bean的注册。
我们重点介绍来研究一下AbstractBeanFactory的代码:
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
// to do more
}
从继承关系上来讲,AbstractBeanFactory具备了ConfigurableBeanFactory 的完整功能,
spring beans源码解读之--BeanFactory的注册的更多相关文章
- spring beans源码解读之--BeanFactory进化史
BeanFactory是访问bean容器的根接口,它是一个bean容器的基本客户端视图. 先让我们看看beanfactory的前生后世吧! beanFactory有四个重要的子接口: SimpleJn ...
- spring beans源码解读之 ioc容器之始祖--DefaultListableBeanFactory
spring Ioc容器的实现,从根源上是beanfactory,但真正可以作为一个可以独立使用的ioc容器还是DefaultListableBeanFactory,因此可以这么说, DefaultL ...
- spring beans源码解读
spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.anno ...
- spring beans源码解读之--总结篇
spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.anno ...
- spring beans 源码解读
从把spring下下来,导入到eclipse,花了几个小时的时间. 本来壮志雄心的说要,满满深入学习研读spring源码,现在看来还是不太现实,太难懂了,各种依赖,说明都是英文,整个串起来理解,深入研 ...
- spring beans源码解读之--Bean的注解(annotation)
随着spring注解的引入,越来越多的开发者开始使用注解,这篇文章将对注解的机制进行串联式的讲解,不求深入透彻,但求串起spring beans注解的珍珠,展示给大家. 1. spring beans ...
- spring beans源码解读之--bean definiton解析器
spring提供了有两种方式的bean definition解析器:PropertiesBeanDefinitionReader和XmLBeanDefinitionReader即属性文件格式的bean ...
- spring beans源码解读之--Bean的定义及包装
bean的定义,包装是java bean的基础.再怎么强调它的重要性都不为过,因此深入 了解这块的代码对以后的代码研究可以起到事半功倍的功效. 1. Bean的定义BeanDefinition 1.1 ...
- spring beans源码解读之--XmlBeanFactory
导读: XmlBeanFactory继承自DefaultListableBeanFactory,扩展了从xml文档中读取bean definition的能力.从本质上讲,XmlBeanFactory等 ...
随机推荐
- Day19 Django之Form表单验证、CSRF、Cookie、Session和Model操作
一.Form表单验证 用于做用户提交数据的验证1.自定义规则 a.自定义规则(类,字段名==html中的name值)b.数据提交-规则进行匹配代码如下: """day19 ...
- ListView的setOnItemClickListener和setOnItemLongClickListener同时响应的问题
lvContentList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(A ...
- 2016021902 - linux解压缩命令
转载自:http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz 解压2:gzip -d Fil ...
- Warning File `.depend' has modification time 1.6 s in the future
一.前提知识 主机时间与虚拟机时间不同步所致.我们在某一操作系统所属磁盘空间下创建一个文件,那么这个文件的创建时间是以磁盘所属的操作系统的时钟为基准的. 我们假设主机windows的系统时间是10:0 ...
- start_amboot()函数分析
一.整体流程 start_amboot()函数是执行完start.S汇编文件后第一个C语言函数,完成的功能自然还是初始化的工作 . 1.全局变量指针r8设定,以及全局变量区清零 2.执行一些类初始化函 ...
- Data Guard配置后续检查
Data Guard配置后续检查 1.打开生产端节点192.166.1.190上的数据库实例: SQL>startup; 2.打开容灾端节点192.166.1.60上的数据库实例: SQL> ...
- Codeforces Round #204 (Div. 2): B
很简单的一个题: 只需要将他们排一下序,然后判断一下就可以了! 代码: #include<cstdio> #include<algorithm> #define maxn 10 ...
- nodejs发展
http://www.infoq.com/cn/news/2012/11/netease-nodejs-framework http://www.jlmonteagudo.com/2013/06/ja ...
- GNOME Shell叫板Ubuntu Unity:优劣PK
转自GNOME Shell叫板Ubuntu Unity:优劣PK GNOME Shell 对阵 Ubuntu Unity--默认桌面界面的战火一触即发.双方在台上已经对峙了很长时间,现在是时候决定谁会 ...
- 使用php对多维维数组排序。
要多php的多维数组排序,可以使用php里的内置函数:array_multisort(); 语法:array_multisort(array1,sorting order,sorting type,a ...