spring beans源码解读

spring Ioc容器的实现,从根源上是beanfactory,但真正可以作为一个可以独立使用的ioc容器还是DefaultListableBeanFactory,因此可以这么说,
DefaultListableBeanFactory 是整个spring ioc的始祖,研究透它的前生今世对我们理解spring ioc的概念有着重要的作用。

1. DefaultListableBeanFactory的作用:

默认实现了ListableBeanFactory和BeanDefinitionRegistry接口,基于bean definition对象,是一个成熟的bean factroy。

最典型的应用是:在访问bean前,先注册所有的definition(可能从bean definition配置文件中)。使用预先建立的bean定义元数据对象,从本地的bean definition表中查询bean definition因而将不会花费太多成本。

DefaultListableBeanFactory既可以作为一个单独的beanFactory,也可以作为自定义beanFactory的父类。

注意:特定格式bean definition的解析器可以自己实现,也可以使用原有的解析器,如:

PropertiesBeanDefinitionReader和XmLBeanDefinitionReader。

2. DefaultListableBeanFactory的继承关系

       (图片来源:http://www.myexception.cn/software-architecture-design/925888.html)

3. 好了,开始进入DefaultListableBeanFactory的大千世界

   直接继承关系:

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
}

   3.1 静态方法

复制代码
    private static Class<?> javaxInjectProviderClass = null;

    static {
        try {
            javaxInjectProviderClass =
                    ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader());
        }
        catch (ClassNotFoundException ex) {
            // JSR-330 API not available - Provider interface simply not supported then.
        }
    }
复制代码

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的重写,返回一个类的实例,区别请参考源码。

上述静态方法中返回了javax.inject.Provider的一个实例,那么我们揭开javax.inject.Provider的面纱看看它到底起了什么作用。

语法: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), 这个方法用来实现类型的自动绑定.

在DefaultListableBeanFactory中实现了AbstractAutowireCapableBeanFactory.copyConfigurationFrom

复制代码
    @Override
    public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) {
        super.copyConfigurationFrom(otherFactory);
        if (otherFactory instanceof DefaultListableBeanFactory) {
            DefaultListableBeanFactory otherListableFactory = (DefaultListableBeanFactory) otherFactory;
            this.allowBeanDefinitionOverriding = otherListableFactory.allowBeanDefinitionOverriding;
            this.allowEagerClassLoading = otherListableFactory.allowEagerClassLoading;
            this.autowireCandidateResolver = otherListableFactory.autowireCandidateResolver;
            this.resolvableDependencies.putAll(otherListableFactory.resolvableDependencies);
        }
    }
复制代码

其中:

allowBeanDefinitionOverriding定义了是否允许同名的不同bean definition再次进行注册;

allowEagerClassLoading 定义了是否允许eager类(相对于lazy)的加载,甚至延迟初始化的bean的加载。

autowireCandidateResolver是一个策略接口,用来决定一个特定的bean definition 是否满足做一个特定依赖的自动绑定的候选项,方法如下所示:

复制代码
 
  
//SimpleAutowireCandidateResolver implements AutowireCandidateResolver
@Override 
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { 
return bdHolder.getBeanDefinition().isAutowireCandidate();
    }
//GenericTypeAwareAutowireCandidateResolver implements AutowireCandidateResolver
@Override 
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
if (!bdHolder.getBeanDefinition().isAutowireCandidate()) {
            // if explicitly false, do not proceed with any other checks
            return false;
        }
        return (descriptor == null || checkGenericTypeMatch(bdHolder, descriptor));
    }
复制代码

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
scopes:域标识符和对应域的键值对集合。

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 DefaultListableBeanFactory类结构及功能 Spring DefaultListableBeanFactory类结构及功能。整个类是Sring IOC的核心类,我整理出来了。 立即下载

相关推荐

Power Automate Desktop实战:一键自动登录Chrome网站

本文详细介绍了如何使用Power Automate Desktop实现Chrome网站的一键自动登录,提升工作效率。从环境准备、基础配置到高级技巧与异常处理,全面解析自动化登录流程的构建与优化,特别适合需要频繁登录多个系统的用户。

weixin_29231725的博客 468

DefaultListableBeanFactory 解读

DefaultListableBeanFactory 解读 DefaultListableBeanFactory介绍 BeanFactory是个Factory,也就是IOC容器或对象工厂,而DefaultListableBeanFactory是Bean工厂的一个默认实现,DefaultListableBeanFactory提供了原始的BeanFactory的功能,如:对外提供getbean()方法,维护一张beanDefinitionMap表 getbean DefaultListableBeanFacto

qq_43203949的博客 6653

USB通讯速度翻倍秘诀:手把手教你用WinUsb替代HID协议(附Zadig驱动配置图解)

本文详细介绍了如何通过WinUsb技术替代传统HID协议,实现USB通讯速度的显著提升。从技术对比、实现原理到Zadig驱动配置图解,手把手教你完成免驱高速通讯方案,适用于数据采集、固件升级等场景,实测速度可达HID协议的2倍以上。

weixin_29168675的博客 137

Spring的爬坑之路(三)DefaultlistableBeanFactory

DefaultlistableBeanFactory是bean加载的核心部分,是spring注册和加载bean的默认实现方式。 而继承自 DefaultListableBeanFactoryd的XmlBeanFactory使用了自定义的 XML 读取器 XmlBeanDefinitionReader,实现了个性化的BeanDefinitionReader读取。

qq_40540178的博客 2560

GD32F470实战:如何用EXMC+SDRAM搞定LVGL大内存需求(附完整代码)

本文详细介绍了如何在GD32F470微控制器上通过EXMC接口驱动外部SDRAM,以满足LVGL图形库的大内存需求。内容涵盖硬件连接、EXMC时序配置、轻量级内存管理器实现,以及将LVGL帧缓冲和资源无缝集成到SDRAM的实战方法,并提供了完整的代码示例和调试技巧,帮助开发者构建稳定可靠的嵌入式GUI系统。

k9l0m1的博客 443

十三、FLUENT蒸发/冷凝模型实战:从沸腾模拟到常温蒸发的UDF进阶

本文深入探讨FLUENT蒸发/冷凝模型的应用实践,从沸腾模拟到常温蒸发的UDF进阶。通过分析相变模型的核心逻辑、关键陷阱及UDF动态饱和温度场的实现,帮助工程师准确模拟不同场景下的蒸发/冷凝过程。结合太阳能蒸馏器等工程案例,提供优化方案和常见问题解决方案,显著提升模拟精度与效率。

weixin_42548893的博客 571

Spring的DefaultListableBeanFactory

spring容器的实现从根源上来看的话是通过BeanFactory实现的,但是BeanFactory只是一个接口类,真正作为一个可以独立使用的容器还是通过DeafultListableBeanFactory实现的,Spring中XML文件的解析的核心类XmlBeanFactory就是继承了DefaultListableBeanFactory的子类,是Spring注册以及加载bean的默认实现,接下...

Taylar_where的博客 3115

Windows11安装UE4+Airsim

Windows11安装UE4+Airsim

Legendyyy的博客 3744

spring beans源码解读之--总结篇

spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类。org.springframework.beans.annotation, 支持包,提供对java 5注解处理bean样式的支持。org.springframework.beans.factory, 实现spring轻量级IoC容器的核心包。org...

326

OPTISYSTEM实战指南:线性调频连续波激光雷达系统仿真设计

本文提供了一份基于OPTISYSTEM软件的线性调频连续波激光雷达系统仿真实战指南。文章详细阐述了从软件界面熟悉、核心参数设置,到发射端、信道模型、相干接收端完整系统搭建的全过程,并介绍了性能优化与高级仿真技巧,旨在帮助读者快速掌握利用OPTISYSTEM进行低成本、高效率激光雷达系统设计与验证的方法。

weixin_29288313的博客 297

win10 运行lisflood_8踩过的坑

1、安装cuda及VS2019 https://www.bilibili.com/read/cv7335974/ 2、netcdf.h头文件找不到问题 https://www.cnblogs.com/lyggqm/p/12926974.html https://blog.youkuaiyun.com/u011954296/article/details/54617048 3、编码问题 https://blog.youkuaiyun.com/qq_33485434/article/details/81196843 4、

我的笔记 4115

Spring Beans包 源码解读

spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类。 org.springframework.beans.annotation, 支持包,提供对java 5注解处理bean样式的支持。 org.sprin...

333

【数据集实例】CMIP6气候模式数据下载-以河东地区为例

以河东地区为例,介绍CMIP6数据下载及处理

WW、forever的博客 4951

spring beans源码解读之--XmlBeanFactory

导读: XmlBeanFactory继承自DefaultListableBeanFactory,扩展了从xml文档中读取bean definition的能力。从本质上讲,XmlBeanFactory等同于DefaultListableBeanFactory+XmlBeanDefinitionReader ,如果有更好的需求,可以考虑使用DefaultListableBeanFactory+X

hedujiu869的专栏 326

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

上一篇: MongoDB知识整理
下一篇: assert断言的陷阱
yufei_java
博客等级 码龄10年 18粉丝 60原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值