Spring技術內幕筆記(2)

org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parsePropertyElements

//這裏對指定Bean元素的property子元素集合進行解析
public void parsePropertyElements(Element beanEle, BeanDefinition bd) {
    NodeList nl = beanEle.getChildNodes();
//遍歷所有Bean元素下定義的property元素
    for(int i = 0; i < nl.getLength(); ++i) {
        Node node = nl.item(i);
        if (this.isCandidateElement(node) && this.nodeNameEquals(node, "property")) {
		//判斷通過以後,進行解析
            this.parsePropertyElement((Element)node, bd);
        }
    }

}

org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parsePropertyElement

public void parsePropertyElement(Element ele, BeanDefinition bd) {
//這裏獲取property的名字
    String propertyName = ele.getAttribute("name");
    if (!StringUtils.hasLength(propertyName)) {
        this.error("Tag 'property' must have a 'name' attribute", ele);
    } else {
        this.parseState.push(new PropertyEntry(propertyName));

        try {
		//如果同一個Bean中已經有同名的property存在,則不進行解析,直接返回this.error
            if (!bd.getPropertyValues().contains(propertyName)) {
                Object val = this.parsePropertyValue(ele, bd, propertyName);
                PropertyValue pv = new PropertyValue(propertyName, val);
                this.parseMetaElements(ele, pv);
                pv.setSource(this.extractSource(ele));
                bd.getPropertyValues().addPropertyValue(pv);
                return;
            }

            this.error("Multiple 'property' definitions for property '" + propertyName + "'", ele);
        } finally {
            this.parseState.pop();
        }

    }
}

org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parsePropertyValue

//這裏取得property元素的值,也許一個list或其他
    public Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {
        String elementName = propertyName != null ? "<property> element for property '" + propertyName + "'" : "<constructor-arg> element";
        NodeList nl = ele.getChildNodes();
        Element subElement = null;

        for(int i = 0; i < nl.getLength(); ++i) {
            Node node = nl.item(i);
            if (node instanceof Element && !this.nodeNameEquals(node, "description") && !this.nodeNameEquals(node, "meta")) {
                if (subElement != null) {
                    this.error(elementName + " must not contain more than one sub-element", ele);
                } else {
                    subElement = (Element)node;
                }
            }
        }

        boolean hasRefAttribute = ele.hasAttribute("ref");
        boolean hasValueAttribute = ele.hasAttribute("value");
		//這裏判斷property屬性,是ref還是value,不允許同時是ref和value
        if (hasRefAttribute && hasValueAttribute || (hasRefAttribute || hasValueAttribute) && subElement != null) {
            this.error(elementName + " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element", ele);
        }

		//如果是ref,創建一個ref的數據對象RuntimeBeanReference,這個對象封裝了ref信息
        if (hasRefAttribute) {
            String refName = ele.getAttribute("ref");
            if (!StringUtils.hasText(refName)) {
                this.error(elementName + " contains empty 'ref' attribute", ele);
            }

            RuntimeBeanReference ref = new RuntimeBeanReference(refName);
            ref.setSource(this.extractSource(ele));
            return ref;
			//如果是value,則創建一個TypedStringValue對象, 封裝vlaue信息 
        } else if (hasValueAttribute) {
            TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute("value"));
            valueHolder.setSource(this.extractSource(ele));
            return valueHolder;
			//如果還有子元素,觸發對子元素的解析
        } else if (subElement != null) {
            return this.parsePropertySubElement(subElement, bd);
        } else {
            this.error(elementName + " must specify a ref or value", ele);
            return null;
        }
    }

org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parseListElement

  public List parseListElement(Element collectionEle, BeanDefinition bd) {
        String defaultElementType = collectionEle.getAttribute("value-type");
        NodeList nl = collectionEle.getChildNodes();
        ManagedList<Object> target = new ManagedList(nl.getLength());
        target.setSource(this.extractSource(collectionEle));
        target.setElementTypeName(defaultElementType);
        target.setMergeEnabled(this.parseMergeAttribute(collectionEle));
		//具體的List元素的解析過程
        this.parseCollectionElements(nl, target, bd, defaultElementType);
        return target;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章