如何在Spring中自定义标签

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

  1. 标签定义文件




    
        
            
                
                    
                
            
        
        
            
                
                    
                
            
        
    

    
        
            
                
            
        
    

  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());
    }
}


    

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

你可能感兴趣的:(Spring)