第一章 手动写IOC

IOC
IOC编写
  • 加载配置文件
  • 解析配置文件
  • 存放到容器

这个高仿spring代码,类的命名和spring的一样,简化功能的实现。
com.cn.spring.context.ClassPathXmlApplicationContext

// 加载配置文件入口
 public ClassPathXmlApplicationContext(String configLocation) throws Exception {
 // configLocation 配置文件的地址  AutowireCapableBeanFactory 自动配置
 // AutowireCapableBeanFactory后面分析.............
    this(configLocation, new AutowireCapableBeanFactory());
  }

com.cn.spring.context.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String, com.cn.spring.beans.factory.AbstractBeanFactory)

 public ClassPathXmlApplicationContext(String configLocation, AbstractBeanFactory beanFactory) throws Exception {
    super(beanFactory);
    this.configLocation = configLocation;
    refresh();
  }

com.cn.spring.context.AbstractApplicationContext#AbstractApplicationContext

 public AbstractApplicationContext(AbstractBeanFactory beanFactory) {
    this.beanFactory = beanFactory;
  }

com.cn.spring.context.AbstractApplicationContext#refresh

  public void refresh() throws Exception {
    // 加载定义的bean
    loadBeanDefinitions(beanFactory);
    // 注册
    registerBeanPostProcessors(beanFactory);
    onRefresh();
 }

// 加载配置文件开始

// 委托设计模式 有对应的实现类去实现
protected abstract void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception;

com.cn.spring.context.ClassPathXmlApplicationContext#loadBeanDefinitions

protected void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception {
// 获取xml解析器
    XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(new ResourceLoader());
    // 加载、解析、存入bean 该方法是总个框架的核心方法
    xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
    for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getRegistry().entrySet()) {
      beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue());
    }
  }

com.cn.spring.beans.xml.XmlBeanDefinitionReader#loadBeanDefinitions

  public void loadBeanDefinitions(String location) throws Exception {
    // 加载资源
    InputStream inputStream = getResourceLoader().getResource(location).getInputStream();
    // 解析xml
    doLoadBeanDefinitions(inputStream);
  }

com.cn.spring.beans.xml.XmlBeanDefinitionReader#doLoadBeanDefinitions

 protected void doLoadBeanDefinitions(InputStream inputStream)throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document doc = docBuilder.parse(inputStream);
    // 解析bean
    registerBeanDefinitions(doc);
    inputStream.close();

  }

解析xml 开始啦…>>>>>>>

protected void registerBeanDefinitions(Document doc) {
    Element root = doc.getDocumentElement();
    parseBeanDefinitions(root);
  }
protected void parseBeanDefinitions(Element root) {
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
      Node node = nl.item(i);
      if (node instanceof Element) {
        Element ele = (Element) node;
        processBeanDefinition(ele);
      }
    }
  }

处理属性…

 protected void processProperty(Element ele, BeanDefinition beanDefinition) {
    NodeList propertyNode = ele.getElementsByTagName("property");
    for (int i = 0; i < propertyNode.getLength(); i++) {
      Node node = propertyNode.item(i);
      if (node instanceof Element) {
        Element propertyEle = (Element) node;
        String name = propertyEle.getAttribute("name");
        String value = propertyEle.getAttribute("value");
        if (value != null && value.length() > 0) {
          beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name, value));
        } else {
          String ref = propertyEle.getAttribute("ref");
          if (ref == null || ref.length() == 0) {
            throw new IllegalArgumentException("Configuration problem: <property> element for property '"
                + name + "' must specify a ref or value");
          }
          BeanReference beanReference = new BeanReference(ref);
          beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name, beanReference));
        }
      }
    }
  }
protected void processBeanDefinition(Element ele) {
    String name = ele.getAttribute("id");
    String className = ele.getAttribute("class");
    BeanDefinition beanDefinition = new BeanDefinition();
    processProperty(ele, beanDefinition);
    beanDefinition.setBeanClassName(className);
    // 存入bean
    getRegistry().put(name, beanDefinition);
  }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值