BeanDefinitionLoader spring Bean的加载器
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的加载器的更多相关文章
- Spring Bean 的加载过程
Spring Bean 的加载过程 一个是populateBean,一个是initializeBean,这两个方法完成了bean的赋值与初始化. 这里有一个BeanDefinitionValueRes ...
- Spring Bean 的加载顺序
一,单一Bean 装载 1. 实例化; 2. 设置属性值; 3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name; 4. 如果实现BeanFacto ...
- Spring Bean配置加载为BeanDefinition全过程(注解配置)
生产中有很多形式的的配置方式,本文仅分析注解配置.对于其他形式的配置区别主观以为主要在配置文件的解析过程不同,不一一分析了.本文以利用Dubbo框架开发rpc服务端为例详细阐述配置类的解析.数据保存. ...
- Spring Bean的加载
Spring加载bean的原则:不等bean创建完成就会将创建bean的ObjectFactory提早曝光加入到缓存中. 单例bean在Spring容器里只会创建一次,后续创建时会首先从缓存中获取 ...
- spring bean容器加载后执行初始化处理@PostConstruct
先说业务场景,我在系统启动后想要维护一个List常驻内存,因为我可能经常需要查询它,但它很少更新,而且数据量不大,明显符合缓存的特质,但我又不像引入第三方缓存.现在的问题是,该List的内容是从数据库 ...
- Spring源码加载BeanDefinition过程
本文主要讲解Spring加载xml配置文件的方式,跟踪加载BeanDefinition的全过程. 源码分析 源码的入口 ClassPathXmlApplicationContext构造函数 new C ...
- 手撸Spring框架,设计与实现资源加载器,从Spring.xml解析和注册Bean对象
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 你写的代码,能接的住产品加需求吗? 接,是能接的,接几次也行,哪怕就一个类一片的 i ...
- Spring 系列教程之 bean 的加载
Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...
- 【Spring源码分析系列】bean的加载
前言 以 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过 ...
随机推荐
- #web开发# 知道cookie hostonly属性的请举手。
Cookie常见姿势.疑难梳理 目前w3c定义浏览器存放每个cookie需要包含以下字段: cookie属性 基本描述 举例 备注 name=value cookie键值对 id=a3fWa expi ...
- 关于 better-scroll 设置了以后无法滚动或不生效的问题
首先在mounted里面注册组件 例:let scroll = new BScroll("#commondityLeftList") 然后打印实例化对象,例:console. ...
- SqlServer新建表操作DDL
创建新表:1,五要素 2,not null 3,默认值 4,字段注释,表名称 5,索引 6,指定约束名称 -- ------------------------------ Table structu ...
- 2020 天翼杯 部分wp
天翼杯 呜呜呜呜 是我太菜了 Web APItest 源码 const express = require("express"); const cors = require(&qu ...
- 美妙绝伦面向node引用-zico图标(逐浪矢量全真图标)1.9发布
15年前,那个农村小伙初入广告行业被讥笑没有审美 于是他狠下决心,积极研发,缔就技术之核, 再后来,那些PPT和美工er们随便怎么自好,无法让其心怵. 因为他是中华人民共和国唯一具备web.cms.o ...
- [hdu6595]Everything Is Generated In Equal Probability
计算一对逆序对的贡献,即在n个数期望要删多少步才能删掉其中的两个数,设f(n)表示此时的期望,则有方程$f[n]=3/4+(\sum_{i=2}^{n}f[i]\cdot c(n-2,i-2))/2^ ...
- I.MX启动方式和头部
1. 启动方式 2. 头部信息 编译好的bin文件烧写到SD卡中,需要加一些头部文件,才可以执行. Image vector table,简称 IVT,IVT 里面包含了一系列的地址信息,这些地址信息 ...
- redis可以设置过期key回调实现延时队列
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- docker创建mongodb并且测试代码
mongodb docker 安装mongodb-创建用户 docker run -itd --name mongo -p 27017:27017 mongo --auth 进入数据库添加密码 ...
- java8特性表达式
public static void main(String[] args) { JFrame jframe = new JFrame("My JFrame"); JButton ...