Spring自定义标签

前言

在Spring源码学习过程中,解析xml文件中标签的方法,有一个分支是解析自定义标签的,那么在分析自定义标签的解析之前,先来了解下自定义标签如何使用是非常有必要的。

自定义标签使用步骤

1. 首先创建一个普通的 POJO

这个POJO 没有任何特别之处,只是用来接收配置文件。

package com.jun.test.customtag;

public class User {
    private String name;
    private String email;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", age=" + age +
                '}';
    }
}

2. 定义一个 XSD 文件

文件名为Spring-test.xsd,文件内容为描述组件内容,如下 

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.jun.com/schema/user"
        xmlns:tns="http://www.jun.com/schema/user"
        elementFormDefault="qualified">
    <element name="user ">
        <complexType>
            <attribute name="id" type="string"/>
            <attribute name="name" type="string"/>
            <attribute name="email" type="string"/>
            <attribute name="age" type="integer"/>
        </complexType>
    </element>
</schema>

在上面的 XSD 文件中描述了一个新的 targetNamespace, 并在这个空间中定义了一个 name 为 user 的 element, user 有4 个属性 id、 name 、email 和 age。

3. 创建 UserBeanDefinitionParser

继承 AbstractSimpleBeanDefinitionParser ,用来解析 XSD 文件中的定义和组件定义。 

package com.jun.test.customtag;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class UserBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {
    // Element 对应的类
    protected Class getBeanClass(Element element) {
        return User.class;
    }

    // 从 element 中解析并提取对应的元索
    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        String name = element.getAttribute("name");
        String email = element.getAttribute("email");
        String age = element.getAttribute("age");

        // 将提取的数据放入到 BeanDefinitionBuilder 中,待到完成所有 bean 的解析后统一注册到beanFactory 中
        if (StringUtils.hasText(name)) {
            bean.addPropertyValue("name", name);
        }
        if (StringUtils.hasText(email)) {
            bean.addPropertyValue("email", email);
        }
        if (StringUtils.hasText(age)) {
            bean.addPropertyValue("age", Integer.parseInt(age));
        }
    }

}

4. 创建 MyNamespaceHandler

扩展自 NamespaceHandlerSupport,目的是将组件注册到 Spring 容器。
package com.jun.test.customtag;

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

public class MyNamespaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
    }
}

5. 编写 Spring.handlers 和 Spring.schemas 文件

默认位置是在工程的 META-INF/ 文件夹 下,当然,你也可以通过 Spring 的扩展或者修改源码的方式改变路径。
 

Spring.handlers

http\://www.jun.com/schema/user=com.jun.test.customtag.MyNamespaceHandler

 Spring.schemas

http\://www.jun.com/schema/user.xsd=customtag/Spring-test.xsd

6 创建测试配置文件

在配置文件 customtagTest.xml 中引入对应的命名空间以及 XSD 后,便可以直接使用 向定义标签了。 

<?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:mytag="http://www.jun.com/schema/user"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.jun.com/schema/user http://www.jun.com/schema/user.xsd">

    <mytag:user id="testCustomtagBean" name="abc" email="abc@ll.com" age="22"/>
</beans>

7.测试

public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("customtag/customtagTest.xml");
        User user = (User) ac.getBean("testCustomtagBean");
        System.out.println(user.toString());
    }

8. 输出结果

User{name='abc', email='abc@ll.com', age=22}

好了,以上就是自定义标签的使用了,有了上面的使用经验,接下来去分析自定义标签的解析就轻松多了,最后祝各位小伙伴们 good goog study,day day up !!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值