美文网首页
SpringXML解析

SpringXML解析

作者: xupengzhang | 来源:发表于2018-05-10 21:32 被阅读0次

    Spring XML解析的抽象工厂模式写法:
    类:BeanDefinitionParserDelegate.java
    行数:1356

    @Nullable
        public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
            String namespaceUri = getNamespaceURI(ele);
            if (namespaceUri == null) {
                return null;
            }
            NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
            if (handler == null) {
                error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
                return null;
            }
            return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
        }
    

    类:DefaultNamespaceHandlerResolver.java
    行数:117

    @Override
        @Nullable
        public NamespaceHandler resolve(String namespaceUri) {
            Map<String, Object> handlerMappings = 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<?> handlerClass = ClassUtils.forName(className, this.classLoader);
                    if (!NamespaceHandler.class.isAssignableFrom(handlerClass)) {
                        throw new FatalBeanException("Class [" + className + "] for namespace [" + namespaceUri +
                                "] does not implement the [" + NamespaceHandler.class.getName() + "] interface");
                    }
                    NamespaceHandler namespaceHandler = (NamespaceHandler) BeanUtils.instantiateClass(handlerClass);
                    namespaceHandler.init();
                    handlerMappings.put(namespaceUri, namespaceHandler);
                    return namespaceHandler;
                }
                catch (ClassNotFoundException ex) {
                    throw new FatalBeanException("Could not find NamespaceHandler class [" + className +
                            "] for namespace [" + namespaceUri + "]", ex);
                }
                catch (LinkageError err) {
                    throw new FatalBeanException("Unresolvable class definition for NamespaceHandler class [" +
                            className + "] for namespace [" + namespaceUri + "]", err);
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:SpringXML解析

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