基於XmlBeanFactory加載bean分析:parseCustomElement分支(四)

在分析的時候(三)中遇到一些問題,直接讓我懷疑人生。可是通過多方面查找資料,發如今IDEA環境中啓用了enable toString後,就會隱式執行toString方法(可是調試時,不會進toString方法)因此我猜測應該是IDEA工具編譯器的問題,在Eclipse中就不會存在這樣的問題。java

public NamespaceHandler resolve(String namespaceUri) {
    //獲取當前系統全部的META-INF/spring.handlers文件(其底層經過Enumeration urls = classLoaderToUse != null?classLoaderToUse.getResources(resourceName):ClassLoader.getSystemResources(resourceName);來獲取資源的)
	//返回的map格式例如:http://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler
	//這裏就根據命名空間key,來獲取對應該命名空間的處理器(Handler)並實例化這個處理器
	Map handlerMappings = this.getHandlerMappings();
	Object handlerOrClassName = handlerMappings.get(namespaceUri);
	if(handlerOrClassName == null) {
		return null;
	} else if(handlerOrClassName instanceof NamespaceHandler) {
		return (NamespaceHandler)handlerOrClassName;
	} else {
		String className = (String)handlerOrClassName;

		try {
			Class err = ClassUtils.forName(className, this.classLoader);
			if(!NamespaceHandler.class.isAssignableFrom(err)) {
				throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri + "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
			} else {
			    //完成處理器的建立並調用了init方法初始化
				NamespaceHandler namespaceHandler = (NamespaceHandler)BeanUtils.instantiateClass(err);
				namespaceHandler.init();
				handlerMappings.put(namespaceUri, namespaceHandler);
				return namespaceHandler;
			}
		} catch (ClassNotFoundException var7) {
			throw new FatalBeanException("NamespaceHandler class [" + className + "] for namespace [" + namespaceUri + "] not found", var7);
		} catch (LinkageError var8) {
			throw new FatalBeanException("Invalid NamespaceHandler class [" + className + "] for namespace [" + namespaceUri + "]: problem with handler class file or dependent class", var8);
		}
	}
}
public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
	String namespaceUri = this.getNamespaceURI(ele);
	//在上面的方法中具體講解了this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri)幹了些什麼事情?(就是完成了根據命名空間完成了該命名空間對應的處理器初始化等操做)
	NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
	if(handler == null) {
		this.error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
		return null;
	} else {
		//這裏就根據實際的處理器進行處理。(例如apo,tx,context,task)
		return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
	}
}
具體命名空間以及處理器的對應關係
http://www.springframework.org/schema/p value:org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler
http://www.springframework.org/schema/util value:org.springframework.beans.factory.xml.UtilNamespaceHandler
http://www.springframework.org/schema/jee value:org.springframework.ejb.config.JeeNamespaceHandler
http://www.springframework.org/schema/aop value:org.springframework.aop.config.AopNamespaceHandler
http://www.springframework.org/schema/redis value:org.springframework.data.redis.config.RedisNamespaceHandler
http://www.springframework.org/schema/cache value:org.springframework.cache.config.CacheNamespaceHandler
http://www.springframework.org/schema/c value:org.springframework.beans.factory.xml.SimpleConstructorNamespaceHandler
http://www.springframework.org/schema/tx value:org.springframework.transaction.config.TxNamespaceHandler
http://www.springframework.org/schema/lang value:org.springframework.scripting.config.LangNamespaceHandler
http://www.springframework.org/schema/task value:org.springframework.scheduling.config.TaskNamespaceHandler
http://www.springframework.org/schema/context value:org.springframework.context.config.ContextNamespaceHandler
//經過這裏咱們發現spring確實太強大啦,各個模塊都是面向接口編程,完成的鬆耦合。
//【注】若是要具體分析這些處理器背後到底作了一些什麼事情,能夠在細化研究分析

到此爲止,關於spring裏面的自定義標籤的BeanDefinition定義就處理完畢了redis

下一節須要分析parseDefaultElement默認節點處理例如spring

<bean name="ApplicationContextUtil;zxsApplicationUtil" class="com.wolf.util.ApplicationContextUtil"/>
相關文章
相關標籤/搜索