zoukankan      html  css  js  c++  java
  • 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;
            }
        }

      

  • 相关阅读:
    UML(Unified Modeling Language)统一建模语言
    20、ASP.NET MVC入门到精通——WebAPI
    16、ASP.NET MVC入门到精通——MVC过滤器
    .net开发过程中Bin目录下面几种文件格式的解释
    13、ASP.NET MVC入门到精通——MVC请求管道
    15、ASP.NET MVC入门到精通——MVC-路由
    14、ASP.NET MVC入门到精通——Ajax
    10、ASP.NET MVC入门到精通——Model(模型)和验证
    12、ASP.NET MVC入门到精通——HtmlHelper
    8、ASP.NET MVC入门到精通——View(视图)
  • 原文地址:https://www.cnblogs.com/jonrain0625/p/13622532.html
Copyright © 2011-2022 走看看