如何在Spring中自定义标签

完整的demo地址:https://github.com/runningRookie/spring-learn main方法位于CustomerDemo类,运行可查看demo结果

  1. 标签定义文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--matrix.xsd-->
<xsd:schema xmlns="http://zhangyuyao.com/schema/matrix"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://zhangyuyao.com/schema/matrix">

    <xsd:complexType name="matrixType">
        <xsd:attribute name="id" type="xsd:ID">
            <xsd:annotation>
                <xsd:documentation>
                    <![CDATA[ 唯一性标识 ]]>
                </xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="description" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation>
                    <![CDATA[ 描述信息 ]]>
                </xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>

    <xsd:element name="matrix" type="matrixType">
        <xsd:annotation>
            <xsd:documentation>
                <![CDATA[ 自定义标签 ]]>
            </xsd:documentation>
        </xsd:annotation>
    </xsd:element>
</xsd:schema>
  1. spring.handlers配置文件
http\://zhangyuyao.com/schema/matrix=zhangyuyao.matrixsb.customer.MatrixNamespaceHandler
  1. spring.schemas配置文件
http\://zhangyuyao.com/schema/matrix/matrix.xsd=customer/matrix.xsd
  1. 自定义的命名空间处理器
/**
 * LY.com Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package zhangyuyao.matrixsb.customer;

import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

/**
 * @author zyy43688
 * @version $Id: MatrixNamespaceHandler.java, v 0.1 2018年5月18日 下午4:42:35 zyy43688 Exp $
 */
public class MatrixNamespaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("matrix", new MatrixBeanDefinitionParser(Matrix.class, true));
    }
}
  1. 自定义BeanDefinition解析器
/**
 * LY.com Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package zhangyuyao.matrixsb.customer;

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.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * @author zyy43688
 * @version $Id: MatrixBeanDefinitionParser.java, v 0.1 2018年5月18日 下午4:46:20 zyy43688 Exp $
 */
public class MatrixBeanDefinitionParser implements BeanDefinitionParser {

    private final Class<?> beanClass;

    private final boolean  required;

    /**
     * 
     * @param beanClass
     * @param required
     */
    public MatrixBeanDefinitionParser(Class<?> beanClass, boolean required) {
        this.beanClass = beanClass;
        this.required = required;
    }

    /**
     * 
     * @param element
     * @param parserContext
     * @return
     */
    @Nullable
    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        return parse(element, parserContext, beanClass, required);
    }

    /**
     * 
     * @param element
     * @param parserContext
     * @param beanClass
     * @param required
     * @return
     */
    private BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
        RootBeanDefinition beanDefinition = new RootBeanDefinition();

        beanDefinition.setBeanClass(beanClass);
        beanDefinition.setLazyInit(false);

        String id = element.getAttribute("id");

        if (!StringUtils.isEmpty(id)) {
            parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
            beanDefinition.getPropertyValues().add("id", id);
        }

        String description = element.getAttribute("description");

        if (!StringUtils.isEmpty(description)) {
            beanDefinition.getPropertyValues().add("description", description);
        }

        return beanDefinition;
    }
}
  1. 定义标签元素对应的java类
/**
 * LY.com Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package zhangyuyao.matrixsb.customer;

/**
 * @author zyy43688
 * @version $Id: Matrix.java, v 0.1 2018年5月18日 下午4:56:23 zyy43688 Exp $
 */
public class Matrix {

    protected String id;

    /**
     * 描述
     */
    private String   description;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
  1. Demo及Spring配置文件
/**
 * LY.com Inc.
 * Copyright (c) 2004-2018 All Rights Reserved.
 */
package zhangyuyao.matrixsb.customer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zyy43688
 * @version $Id: CustomerDemo.java, v 0.1 2018年5月18日 下午4:57:38 zyy43688 Exp $
 */
@Slf4j
public class CustomerDemo {
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:matrix.xml");

        Matrix matrix = (Matrix) context.getBean("zhangyuyao");

        log.info("description: {}", matrix.getDescription());
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:matrix="http://zhangyuyao.com/schema/matrix"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
               http://zhangyuyao.com/schema/matrix http://zhangyuyao.com/schema/matrix/matrix.xsd ">
    <matrix:matrix id="zhangyuyao" description="测试自定义元素解析功能!"/>
</beans>

spring.handlers和spring.schemas文件必须放置在classpath路径下的META-INF目录中,否则Spring无法识别到它们!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值