spring 容器注册bean , 会把bean包装成beanDefinition 放进spring容器中,beanDefinitionLoader就是加载bean的类 。

一、源码

class BeanDefinitionLoader {
private final Object[] sources;
private final AnnotatedBeanDefinitionReader annotatedReader;
private final XmlBeanDefinitionReader xmlReader;
private BeanDefinitionReader groovyReader;
private final ClassPathBeanDefinitionScanner scanner;
private ResourceLoader resourceLoader; BeanDefinitionLoader(BeanDefinitionRegistry registry, Object... sources) {
Assert.notNull(registry, "Registry must not be null");
Assert.notEmpty(sources, "Sources must not be empty");
this.sources = sources;
this.annotatedReader = new AnnotatedBeanDefinitionReader(registry);
this.xmlReader = new XmlBeanDefinitionReader(registry);
if (this.isGroovyPresent()) {
this.groovyReader = new GroovyBeanDefinitionReader(registry);
} this.scanner = new ClassPathBeanDefinitionScanner(registry);
this.scanner.addExcludeFilter(new BeanDefinitionLoader.ClassExcludeFilter(sources));
} int load() {
int count = 0;
Object[] var2 = this.sources;
int var3 = var2.length; for(int var4 = 0; var4 < var3; ++var4) {
Object source = var2[var4];
count += this.load(source);
} return count;
} }

二、工作原理

  构造方法需要两个参数一个是 registry  一个是 resource 资源 。那么这个两个是代表什么呢

  1.registry 通常会是一个ApplicationContent  ,去梳理ApplicationContent 的依赖关系可以看到   ApplicationContent 实现了BeanDeflectionRegistry  这个接口

  2.resoures .有四种类型 分别 是class 、Resource 、PackPackage 、CharSequence 。在load 的时候判断资源的类型 。分别用  AnnotatedBeanDefinitionReader、 XmlBeanDefinitionReader、 BeanDefinitionReader 、ClassPathBeanDefinitionScanner

去加载对应的资源放到ApplicationContent中,而每个Reader 或scanner 中在构造方法中已经把 BeanDeflectionRegistry (ApplicationContent) 注入进去了 。

  对资源的判断,分别重载了对应的load方法 。

 private int load(Object source) {
Assert.notNull(source, "Source must not be null");
if (source instanceof Class) {
return this.load((Class)source);
} else if (source instanceof Resource) {
return this.load((Resource)source);
} else if (source instanceof Package) {
return this.load((Package)source);
} else if (source instanceof CharSequence) {
return this.load((CharSequence)source);
} else {
throw new IllegalArgumentException("Invalid source type " + source.getClass());
}
}

class 资源的load 方法  ,如果不是Groovy 类型资源会调用AnnotateBeanDefinitionReader去注册bean

private int load(Class<?> source) {
if (this.isGroovyPresent() && BeanDefinitionLoader.GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
BeanDefinitionLoader.GroovyBeanDefinitionSource loader = (BeanDefinitionLoader.GroovyBeanDefinitionSource)BeanUtils.instantiateClass(source, BeanDefinitionLoader.GroovyBeanDefinitionSource.class);
this.load(loader);
} if (this.isComponent(source)) {
this.annotatedReader.register(new Class[]{source});
return 1;
} else {
return 0;
}
}

  

BeanDefinitionLoader spring Bean的加载器的更多相关文章

  1. Spring Bean 的加载过程

    Spring Bean 的加载过程 一个是populateBean,一个是initializeBean,这两个方法完成了bean的赋值与初始化. 这里有一个BeanDefinitionValueRes ...

  2. Spring Bean 的加载顺序

    一,单一Bean 装载 1. 实例化; 2. 设置属性值; 3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name; 4. 如果实现BeanFacto ...

  3. Spring Bean配置加载为BeanDefinition全过程(注解配置)

    生产中有很多形式的的配置方式,本文仅分析注解配置.对于其他形式的配置区别主观以为主要在配置文件的解析过程不同,不一一分析了.本文以利用Dubbo框架开发rpc服务端为例详细阐述配置类的解析.数据保存. ...

  4. Spring Bean的加载

    Spring加载bean的原则:不等bean创建完成就会将创建bean的ObjectFactory提早曝光加入到缓存中.   单例bean在Spring容器里只会创建一次,后续创建时会首先从缓存中获取 ...

  5. spring bean容器加载后执行初始化处理@PostConstruct

    先说业务场景,我在系统启动后想要维护一个List常驻内存,因为我可能经常需要查询它,但它很少更新,而且数据量不大,明显符合缓存的特质,但我又不像引入第三方缓存.现在的问题是,该List的内容是从数据库 ...

  6. Spring源码加载BeanDefinition过程

    本文主要讲解Spring加载xml配置文件的方式,跟踪加载BeanDefinition的全过程. 源码分析 源码的入口 ClassPathXmlApplicationContext构造函数 new C ...

  7. 手撸Spring框架,设计与实现资源加载器,从Spring.xml解析和注册Bean对象

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你写的代码,能接的住产品加需求吗? 接,是能接的,接几次也行,哪怕就一个类一片的 i ...

  8. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  9. 【Spring源码分析系列】bean的加载

    前言 以 BeanFactory bf  = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过 ...

随机推荐

  1. 地表最强IDE ——Visual Studio 2022正式发布

    地表最强IDE--Visual Studio 2022昨天正式发布啦! 堪称宇宙第一IDE工具集的Visual Studio,在经过不断更新优化之后,新版本就要与大家见面了.本次新版本发布,有许多令人 ...

  2. Mplus数据分析:随机截距交叉之后的做法和如何加协变量,写给粉丝

    记得之前有写过如何用R做随机截距交叉滞后,有些粉丝完全是R小白,还是希望我用mplus做,今天就给大家写写如何用mplus做随机截距交叉滞后. 做之前我们需要知道一些Mplus的默认的设定: obse ...

  3. GDI绘制Winform工作流组件、具有独立图层的增删处理、防PPT效果

    最近接了个小项目10K.用了2个下班时间写完,共花费了6-7个小时完成.如有同类需求的可以与本人联系,QQ:120772981 功能目标: 需要写一个仿PPT画泳道图的组件.之前写过工作流的组件,其实 ...

  4. [loj3463]表达式求值

    类似cf582E,先建出表达式树,然后树形dp+离散+min和max卷积的优化,复杂度为$o(nm|E|)$,无法通过 考虑我们仅关心于这$n$个数的大小关系,具体来说,假设给出的数组是$a_{i,j ...

  5. littlevgl架构浅析

    一.   littlevgl有几个线程,作用是什么? 三个,主线程一个,和在主线程的hal_init函数中创建的另两个sdl线程. 主线程完成一系列初始化工作后,循环每10ms调用在lv_init函数 ...

  6. Java 插入html字符串到PPT幻灯片

    通过Java后端代码操作PPT幻灯片时,可直接在幻灯片中绘制形状,并在形状中添加文本字符串内容.本篇文章,介绍一种通过html字符串来添加内容到PPT幻灯片的的方法,可添加文字.图片.视频.音频等.下 ...

  7. springboot访问静态资源404

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  8. 植物GO注释

    本文主要是对没有GO term库的植物进行注释. 1.选用AgriGo 进行注释,在agriGO中点击species后,查看与你目标物种相近的物种作为库 2.比如我以甜菜为例 为了找到和GO term ...

  9. eclipse 配置黑色主题(转载)

    转载:http://www.cnblogs.com/csulennon/p/4231405.html 虽然以前也使用eclipse的黑色主题,但是配置起来稍微麻烦一点. 这里先声明,下面的方式适合最新 ...

  10. Excel-数据分列的多种方法实现

    2.数据->分列 (数据格式统一的精准分列)<=> 手动快捷键ctrl+E+等待 ("模糊模仿""分列)<=> 用函数实现(精准分列) 用函 ...