spring自定义标签使用

一、新建实体类

@Data
public class Linxh{

    private Integer age;

    public Linxh(Integer age){
        this.age = age;
    }
}

二、新建类 LinxhBeanDefinitionParser,beandefinition转换器

package com.linxh;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

/**
 * @Description
 * @Author linxh
 * @Date 2019/12/18
 * @Version 1.0
 */
public class LinxhBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    protected Class<?> getBeanClass(Element element) {
        return Linxh.class;
    }

    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String age = element.getAttribute("age");

        builder.addConstructorArgValue(Integer.parseInt(age));
    }
}

 三、新建TestTagNamespaceHandler标签命名处理器

package com.linxh;

import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Node;

/**
 * @Description
 * @Author linxh
 * @Date 2019/12/18
 * @Version 1.0
 */
public class TestTagNamespaceHandler extends NamespaceHandlerSupport {

    public void init() {
        this.registerBeanDefinitionParser("linxh",
                new LinxhBeanDefinitionParser());
    }

    @Override
    public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) {
        return super.decorate(node, definition, parserContext);
    }
}

四、 在项目资源路径resources路径下新建META-INF文件夹,文件夹下新建spring.handles文件

http\://www.linxh.com/schema/test=com.linxh.TestTagNamespaceHandler

 五、在META-INF文件夹,文件夹下新建spring.schemas文件

http\://www.linxh.com/schema/test.xsd=META-INF/test.xsd

六、在META-INF文件夹,文件夹下新建test.xsd文件

 

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.linxh.com/schema/test"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.linxh.com/schema/test"
            elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:element name="linxh">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string"></xsd:attribute>
            <xsd:attribute name="age" type="xsd:string"></xsd:attribute>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

七、 在项目资源路径resources路径下新建my.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:test="http://www.linxh.com/schema/test"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.linxh.com/schema/test
    http://www.linxh.com/schema/test.xsd"
       default-lazy-init="false">
    <test:linxh id="linxh" age="18"></test:linxh>
</beans>

 

八、创建测试类

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

/**
 * @Description
 * @Author linxh
 * @Date 2019/12/18
 * @Version 1.0
 */
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext anno = new ClassPathXmlApplicationContext("my.xml");
        Linxh linxh = (Linxh) anno.getBean("linxh");
        System.out.println(linxh.getAge());
    }
}

 

输出结果: 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值