spring beans 源码解读
从把spring下下来,导入到eclipse,花了几个小时的时间。
本来壮志雄心的说要,满满深入学习研读spring源码,现在看来还是不太现实,太难懂了,各种依赖,说明都是英文,整个串起来理解,深入研读太难了。
想想还是自己学得不够深入,现在研读还为时尚早吧。还是等一段时间再研读。现在我还是小菜鸟,深读太困难了。。。
下面贴出对spring beans的学习:
spring提供一些实用的综合工具类如ClassUtils,ClassUtils提供了对类的实用方法,主要用在框架内部,想要了解更全面的类的工具方法可以参考apache commons lang。
Commons Lang
The standard Java libraries fail to provide enough methods for manipulation of its core classes. Apache Commons Lang provides these extra methods. Lang provides a host of helper utilities for the java.lang API, notably String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization and System properties. Additionally it contains basic enhancements to java.util.Date and a series of utilities dedicated to help with building methods, such as hashCode, toString and equals
ClassUtils.forName方法是class.forname的重写,返回一个类的实例,区别请参考源码。
语法:public interface Provider<T>
提供了一个 T的实例
. 通常作为一个依赖注入容器器的父接口. 可以注入任何类型的 T
, 当然也可以注入 Provider<T>
. 相对于直接注入 T
,注入 Provider<T>
有如下作用:
- 检索多个实例.
- 延迟或者选择性的检索一个实例.
- 打破循环依赖.
- 抽象的scope,可以从一个包含scope的更小的scope中检索一个实例。
实例代码:

class Car {
@Inject Car(Provider<Seat> seatProvider) {
Seat driver = seatProvider.get();
Seat passenger = seatProvider.get();
...
}
}

3.2 继承自AbstractAutowireCapableBeanFactory的方法:
AbstractAutowireCapableBeanFactory的作用:
提供bean的创建 (有construct方法), 属性注值, 绑定 (包括自动绑定)和初始化.
处理运行时bean引用, 解析管理的集合, 调用初始化方法。
AutowireCapableBeanFactory.resolveDependency(DependencyDescriptor, String, Set, TypeConverter)
,resolvableDependencies:定义了依赖类型和其对应的自动绑定值的键值对集合。
AbstractAutowireCapableBeanFactory的copyConfigurationFrom方法:

@Override
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
super.copyConfigurationFrom(otherFactory);
if (otherFactory instanceof AbstractAutowireCapableBeanFactory) {
AbstractAutowireCapableBeanFactory otherAutowireFactory =
(AbstractAutowireCapableBeanFactory) otherFactory;
this.instantiationStrategy = otherAutowireFactory.instantiationStrategy;
this.allowCircularReferences = otherAutowireFactory.allowCircularReferences;
this.ignoredDependencyTypes.addAll(otherAutowireFactory.ignoredDependencyTypes);
this.ignoredDependencyInterfaces.addAll(otherAutowireFactory.ignoredDependencyInterfaces);
}
}

其中,instantiationStrategy 为创建bean 实例的策略,默认值:
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
allowCircularReferences 确定是否自动尝试去解析循环引用的bean。
ignoredDependencyTypes 定义了在依赖检查和自动绑定时要忽略的依赖类型,是一组类对象,例如string,默认为没有。
ignoredDependencyInterfaces 定义了在依赖检查和自动绑定时要忽略的依赖接口,是一组类对象,默认情况下,只有beanFactory接口被忽略。
父类AbstractBeanFactory的copyConfigurationFrom的实现如下:

@Override
public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
Assert.notNull(otherFactory, "BeanFactory must not be null");
setBeanClassLoader(otherFactory.getBeanClassLoader());
setCacheBeanMetadata(otherFactory.isCacheBeanMetadata());
setBeanExpressionResolver(otherFactory.getBeanExpressionResolver());
if (otherFactory instanceof AbstractBeanFactory) {
AbstractBeanFactory otherAbstractFactory = (AbstractBeanFactory) otherFactory;
this.customEditors.putAll(otherAbstractFactory.customEditors);
this.propertyEditorRegistrars.addAll(otherAbstractFactory.propertyEditorRegistrars);
this.beanPostProcessors.addAll(otherAbstractFactory.beanPostProcessors);
this.hasInstantiationAwareBeanPostProcessors = this.hasInstantiationAwareBeanPostProcessors ||
otherAbstractFactory.hasInstantiationAwareBeanPostProcessors;
this.hasDestructionAwareBeanPostProcessors = this.hasDestructionAwareBeanPostProcessors ||
otherAbstractFactory.hasDestructionAwareBeanPostProcessors;
this.scopes.putAll(otherAbstractFactory.scopes);
this.securityContextProvider = otherAbstractFactory.securityContextProvider;
}
else {
setTypeConverter(otherFactory.getTypeConverter());
}
}

其中
customEditors 是自定义的属性编辑器,适用于该beanFactory的所有bean。
propertyEditorRegistrars属性编辑器的注册器,使用于该beanFactory的所有bean。
beanPostProcessors 创建bean时应用的beanPostProcessors。
hasInstantiationAwareBeanPostProcessors 表明是否注册有任何的InstantiationAwareBeanPostProcessors。
hasDestructionAwareBeanPostProcessors表明是否注册有任何的DestructionAwareBeanPostProcessors
securityContextProvider 运行SecurityManager时使用的security context
3.3 继承自ListableBeanFactory接口的方法
ListableBeanFactory是beanFactory接口的扩展接口,它可以枚举所有的bean实例,而不是客户端通过名称一个一个的查询得出所有的实例。要预加载所有的bean定义的beanfactory可以实现这个接口来。该 接口定义了访问容器中Bean基本信息的若干方法,如查看Bean的个数、获取某一类型Bean的配置名、查看容器中是否包括某一Bean等方法;

containsBeanDefinition(String beanName)
Check if this bean factory contains a bean definition with the given name. findAnnotationOnBean(String beanName, Class<A> annotationType)
Find a Annotation of annotationType on the specified bean, traversing its interfaces and super classes if no annotation can be found on the given class itself. getBeanDefinitionCount()
Return the number of beans defined in the factory. getBeanDefinitionNames()
Return the names of all beans defined in this factory. getBeanNamesForType(Class<?> type)
Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)
Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. getBeansOfType(Class<T> type)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. getBeansWithAnnotation(Class<? extends Annotation> annotationType)
Find all beans whose Class has the supplied Annotation type.

3.4 继承自ConfigurableListableBeanFactory接口的方法
ConfigurableListableBeanFactory 它同时继承了ListableBeanFactory,AutowireCapableBeanFactory和ConfigurableBeanFactory,提供了对bean定义的分析和修改的便利方法,同时也提供了对单例的预实例化。

void freezeConfiguration()
Freeze all bean definitions, signalling that the registered bean definitions will not be modified or post-processed any further. BeanDefinition getBeanDefinition(String beanName)
Return the registered BeanDefinition for the specified bean, allowing access to its property values and constructor argument value (which can be modified during bean factory post-processing). void ignoreDependencyInterface(Class<?> ifc)
Ignore the given dependency interface for autowiring. void ignoreDependencyType(Class<?> type)
Ignore the given dependency type for autowiring: for example, String. boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor)
Determine whether the specified bean qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type. boolean isConfigurationFrozen()
Return whether this factory's bean definitions are frozen, i.e. void preInstantiateSingletons()
Ensure that all non-lazy-init singletons are instantiated, also considering FactoryBeans. void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue)
Register a special dependency type with corresponding autowired value.

3.5 继承自BeanDefinitionRegistry接口的方法
BeanDefinitionRegistry:Spring配置文件中每一个<bean>节点元素在Spring容器里都通过一个BeanDefinition对象表示,它描述了Bean的配置信息。而BeanDefinition Registry接口提供了向容器手工注册BeanDefinition对象的方法。

boolean containsBeanDefinition(String beanName)
Check if this registry contains a bean definition with the given name. BeanDefinition getBeanDefinition(String beanName)
Return the BeanDefinition for the given bean name. int getBeanDefinitionCount()
Return the number of beans defined in the registry. String[] getBeanDefinitionNames()
Return the names of all beans defined in this registry. boolean isBeanNameInUse(String beanName)
Determine whether the given bean name is already in use within this registry, i.e. void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
Register a new bean definition with this registry. void removeBeanDefinition(String beanName)
Remove the BeanDefinition for the given name.

3.6 序列化支持
private void writeObject(java.io.ObjectOutputStream out)throws IOException
private void readObject(java.io.ObjectInputStream in)throws IOException, ClassNotFoundException;
private void readObjectNoData()throws ObjectStreamException;
4 小结:
spring Ioc容器的实现,从根源上是beanfactory,但真正可以作为一个可以独立使用的ioc容器还是DefaultListableBeanFactory,因此可以这么说,
DefaultListableBeanFactory 是整个spring ioc的始祖,研究透它的前生今世对我们理解spring ioc的概念有着重要的作用。
DefaultListableBeanFactory功能的实现是通过实现特定功能的接口来完成。
AbstractAutowireCapableBeanFactory 实现属性的自动绑定功能。
ConfigurableListableBeanFactory提供了对bean定义的分析和修改的便利方法,同时也提供了对单例的预实例化。
ListableBeanFactory提供枚举所有的bean实例,而不是客户端通过名称一个一个的查询得出所有的实例。
BeanDefinitionRegistry 提供了beanDefinition的管理。
spring beans 源码解读的更多相关文章
- 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源码解读之--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等 ...
- spring beans源码解读之--BeanFactory的注册
beanFactory的继承关系如下图所示: (图片来源:http://www.myexception.cn/software-architecture-design/925888.html) 在上节 ...
- spring beans源码解读之--BeanFactory进化史
BeanFactory是访问bean容器的根接口,它是一个bean容器的基本客户端视图. 先让我们看看beanfactory的前生后世吧! beanFactory有四个重要的子接口: SimpleJn ...
随机推荐
- kuangbin大佬模板(侵删)- hdu 2222
2017-08-13 19:54:08 kuangbin的AC自动机模板 可以直接过 入门题目 hdu2222 #include<cstdio> #include<cstring&g ...
- Maven 一段时间知识小结
二种打包命令生成后的jar包比较 1.clean install -P dev 2.clean package -Dmaven.test.skip=true -P dev //clean packa ...
- mac 下 安装 mongodb
使用brew安装,不过brew不再更新, 通过 sudo chown -R $(whoami):admin /usr/local 这条语句终端有提醒的 xcode-select --install 需 ...
- 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决
问题描述: 在配置spring的applicationContext.xml中的默认事务管理器的时候可能会出现这样的错误: Error occured processing XML 'org/spri ...
- 浏览器检测-js
昨天有一同学问我为什么attachEvent在非IE浏览器下不能起作用,我也跟他解释了一番:attachEvent是IE下独有的,只能在IE下使用,其他浏览器下应该用addEventListener来 ...
- getline
istream& istream::getline(char*, streamsize,char= '\n'); 函数getline与get的区别在于,函数get当遇到分隔符后,停止获取,并将 ...
- guava的事件发布订阅功能
事件的重要性,不用说很重要,在很多时候我们做完一个操作的时候,需要告知另外一个对象让他执行相应操作,比如当用户注册成功的时候,需要抛出一个注册成功的事件,那么有监听器捕获到这个事件,完成后续用户信息初 ...
- p标签多行文字内容实现上下垂直居中兼容ie8
之前实现上下居中一般都是用height和line-height的来设置. 今天在修改样式的时候,p标签的文字内容可能是一行也可能是两行, 所以用height和line-height就没效果. 今天找到 ...
- Markdown - 让网络书写变得简单
概述 宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所 ...
- SQL语句往Oracle数据库中插入日期型数据(to_date的用法)
Oracle 在操作数据库上相比于其他的 T-sql 有微小的差别,但是在插入时间类型的数据是必须要注意他的 to_date 方法,具体的情况如下: --SQL语句往Oracle数据库中插入日期型数据 ...