博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring的自定义标签
阅读量:4658 次
发布时间:2019-06-09

本文共 4063 字,大约阅读时间需要 13 分钟。

当Spring拿到一个元素时首先要做的是根据命名空间进行解析,如果是默认的命名空间,则使用parseDefaultElement方法进行元素解析,否则使用parseCustom Element方法进行解析。

protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {        if (delegate.isDefaultNamespace(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;                    if (delegate.isDefaultNamespace(ele)) {                        parseDefaultElement(ele, delegate);                    }                    else {                        delegate.parseCustomElement(ele);                    }                }            }        }        else {            delegate.parseCustomElement(root);        }    }    public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {        String namespaceUri = getNamespaceURI(ele);        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));    }

自定义标签的使用

扩展Spring自定义标签配置大致需要以下几个步骤(前提是要把Spring的Core包加入项目中)。

  1. 创建一个需要扩展的组件。
  2. 定义一个XSD文件描述组件内容。
  3. 创建一个文件,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义。
  4. 创建一个Handler文件,扩展自NamespaceHandlerSupport,目的是将组件注册到Spring容器。
  5. 编写Spring.handlers和Spring.schemas文件。

现在我们就按照上面的步骤一步步地体验自定义标签的过程。

第一步:

public class TestBean {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "TestBean{" +                "name='" + name + '\'' +                '}';    }}

第二步:

第三步:

import org.springframework.beans.factory.config.BeanDefinition;  import org.springframework.beans.factory.support.RootBeanDefinition;  import org.springframework.beans.factory.xml.BeanDefinitionParser;  import org.springframework.beans.factory.xml.ParserContext;  import org.w3c.dom.Element;    public class TestCustomBeanDefinitionParser implements BeanDefinitionParser {        public BeanDefinition parse(Element element, ParserContext parserContext) {            String id = element.getAttribute("id");          String name = element.getAttribute("name");            RootBeanDefinition beanDefinition = new RootBeanDefinition();          beanDefinition.setBeanClass(TestBean.class);          beanDefinition.getPropertyValues().addPropertyValue("name", name);          parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);            return beanDefinition;      }  }

第四步:

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;  public class TestNamespaceHandler extends NamespaceHandlerSupport {      public void init() {          registerBeanDefinitionParser("custom", new TestCustomBeanDefinitionParser());      }  }

第五步:

spring.handlers:

http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
spring.schemas:
http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd

第六步:

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { String xml = "classpath:test.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xml }); System.out.println(context.getBean("testCustom")); } } 上例输出为:TestBean {name=this is a test custom tag}

 

转载于:https://www.cnblogs.com/wade-luffy/p/6068813.html

你可能感兴趣的文章
BZOJ 1677: [Usaco2005 Jan]Sumsets 求和
查看>>
缓冲流
查看>>
DIV不用图片做可变可到处用的圆角
查看>>
luogu3899谈笑风生
查看>>
博客推荐
查看>>
MyBatis-Spring配置简单了解
查看>>
汇编语言 Part 1——简介、基本语法、内存分段与内存地址
查看>>
java创建线程的三种方式及其对照
查看>>
unity常见问题之20题
查看>>
AI类第四周进度
查看>>
SQLServer学习笔记系列7
查看>>
【bzoj1712】[Usaco2007 China]Summing Sums 加密 矩阵乘法
查看>>
如何解决git创建密匙时报错Too many arguments
查看>>
python学习笔记-25 实例属性和类属性
查看>>
python 单例模式
查看>>
Java知识积累——String引用的判断问题
查看>>
Asp.Net Web API 2第七课——Web API异常处理
查看>>
bzoj 2339: [HNOI2011]卡农
查看>>
[Canvas]新版箴言钟表
查看>>
杭电(hdu)2053 Switch Game 水题
查看>>