随笔 - 44  文章 - 3  评论 - 4  阅读 - 94228 

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;
        }
    }
复制代码

 

  

 

posted on   JonRain0625  阅读(398)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示