美文网首页我爱编程
tiny-spring源码解析(五)读取xml配置来初始化bea

tiny-spring源码解析(五)读取xml配置来初始化bea

作者: Maceys | 来源:发表于2018-04-10 14:45 被阅读0次

    git checkout step-4-config-beanfactory-with-xml

    这里面有一个XmlBeanDefinitionReader 接口 有一个实现loadBeanDefinitions


    image.png

    这个是载入resources目录下的配置文件


    image.png
    我们看看做了什么
    ···

    public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {

    public XmlBeanDefinitionReader(ResourceLoader resourceLoader) {
        super(resourceLoader);
    }
    
    @Override
    public void loadBeanDefinitions(String location) throws Exception {
        InputStream inputStream = getResourceLoader().getResource(location).getInputStream();
        doLoadBeanDefinitions(inputStream);
    }
    

    ···
    很明显是读取io流文件进来 之后调用doLoadBeanDefinitions方法取出name节点和class节点的值 然后就是第一篇的一样
    ···

    protected void processBeanDefinition(Element ele) {
        String name = ele.getAttribute("name");
        String className = ele.getAttribute("class");
        BeanDefinition beanDefinition = new BeanDefinition();
        processProperty(ele,beanDefinition);
        beanDefinition.setBeanClassName(className);
        getRegistry().put(name, beanDefinition);
    }
    

    ···
    还有xml bean配置的property节点的值 也就是将这些值一样的放入BeanDefinition的 propertyValues集合基本在基于前四篇讲的多了IO加载xml bean 和属性 然后初始化工厂时候初始化bean
    最后


    image.png

    相关文章

      网友评论

        本文标题:tiny-spring源码解析(五)读取xml配置来初始化bea

        本文链接:https://www.haomeiwen.com/subject/ecpphftx.html