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 2020-09-06 17:54  JonRain0625  阅读(388)  评论(0编辑  收藏  举报