1. 程式人生 > >如何在Spring中自定義標籤

如何在Spring中自定義標籤

  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無法識別到它們!!