美文网首页
2.自定义标签实现及使用

2.自定义标签实现及使用

作者: faunjoe | 来源:发表于2018-07-03 14:55 被阅读0次

首先我们来理一理如何自定义一个spring的标签,像bean标签那样使用,我们先概览一些整体的流程:

1)创建一个需要扩展的组件
2)定义一个XSD文件,描述组件内容
3)创建一个java类,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义
4)创建一个Handler类,扩展子NameSpaceHandlerSupport,目的是将组件注册到容器。
5)编写(添加)Spring.handlers和Spring.schemas文件。

完成以上工作的话,那么我们就可以使用我们自定义的标签了,接下来我们来看详细的例子实现。

创建pojo类

package net.itaem.vo;
public class User {
    private String name;
    private String sex;
    private String email;
    private String id;
    
    //set get method...
}

定义一个xsd文件描述组件类容

以前spring使用的是dtd文件,现在几乎使用的都是xsd文件了,xsd文件的定义如果不了解的朋友可以去http://www.phpstudy.net/e/schema/ 这个网址看看,用法比较简单,这里就不介绍如何使用了,下面是按照schema的规则定义一个xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.lexueba.com/schema/user"
            xmlns:tns="http://www.lexueba.com/schema/user"
            elementFormDefault="qualified">
<element name="user">
    <complexType>
        <attribute name="id" type="string" />
        <attribute name="name" type="string" />
        <attribute name="sex" type="string" />
        <attribute name="email" type="string" />
    </complexType>
</element>
 
</schema>

实现AbstractSingleBeanDefinitionParser接口

实现了这个接口,当spring加载文档的时候,遇到你定义的标签,他就会回调你的这个解析方法,进行你自定义的属性解析。

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package net.itaem.parser;
 
import net.itaem.vo.User;
 
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
 
 
/**
 * 
 * @author Administrator
 */
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
 
    /* (non-Javadoc)
     * @see org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser#getBeanClass(org.w3c.dom.Element)
     */
    @Override
    protected Class<?> getBeanClass(Element element) {
        return  User.class;
    }
    
    //从elelment中解析并提取对应的元素
    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
     
        String name=element.getAttribute("name");
        String email=element.getAttribute("sex");
        String sex=element.getAttribute("sex");
        //将提取到的数据放入beanDefinitionBuilder 中,待完成所有的bean解析后统一放到beanfactory
        if(StringUtils.hasText(name)){
            builder.addPropertyValue("name", name);
        }
        if(StringUtils.hasText(email)){
            builder.addPropertyValue("email", email);
        }
        if(StringUtils.hasText(sex)){
            builder.addPropertyValue("sex", sex);
        }
    }
}

继承抽象类NamespaceHandlerSupport

定义了标签,这里就是标签的处理器,处理器中会把解析类(UserBeanDefinitionParser)的实例传入spring中,使得当spring解析到该标签的时候可以回调该实例的方法。

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package net.itaem.handler;
 
import net.itaem.parser.UserBeanDefinitionParser;
 
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
 
 
/**
 * 
 * @author Administrator
 */
public class MyNamespaceHandler extends NamespaceHandlerSupport {
 
    /* (non-Javadoc)
     * @see org.springframework.beans.factory.xml.NamespaceHandler#init()
     */
    @Override
    public void init() {
        registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
    }
}

修改(添加)spring.handlers文件和spring.schemas文件

这两个文件默认路径是在META-INF这个路径下面
修改spring.handlers 文件,添加以下内容

http://www.lexueba.com/schema/user(自定义的)=net.itaem.handler.MyNamespaceHandler

修改spring.schemas文件的内容,添加以下内容

http://www.lexueba.com/schema/user.xsd(自定义的)=org/springframework/beans/factory/xml/use.xsd

修改好之后在使用自定义标签的时候便会把这些加到xsi:schemaLocation里面去,然后spring会自己去找xsd文件以及处理器(handler)

创建测试配置文件

使用的时候可以加上自己定义的命名空间,然后再xsi:schemaLocation加上对应的内容,就可以使用自己定义的标签了。

<?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:myname="http://www.lexueba.com/schema/user"
 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.lexueba.com/schema/user http://www.lexueba.com/schema/user.xsd">
 
     <myname:user id="beantest" name="whx" email="494863082@qq.com" sex="男"/>   
</beans>

编写测试代码

package net.itaem.test;
 
import net.itaem.vo.User;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
    
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("net/itaem/source/custom_user.xml");
        User bt=(User) context.getBean("beantest");
        System.out.println(bt);
    }
     
}

运行结果可以看到,程序正常输出bean的信息


image.png

总结

自定义spring的标签,由于spring为我们做了大量的封装,自定义起来总体来说还是比较简单的,后续我们会看到,其实spring的aop的标签,类似这些非spring默认的标签,这些都是需要经过这些流程去实现自定义标签的。

相关文章

  • 2.自定义标签实现及使用

    首先我们来理一理如何自定义一个spring的标签,像bean标签那样使用,我们先概览一些整体的流程: 1)创建一个...

  • VUE自定义弹窗指令

    目标 使用vue2.0实现自定义弹窗指令,当标签有该指令时,点击标签可以弹出弹窗 实现

  • javaweb自定义标签

    为什么使用自定义标签 减少在jsp写java代码。简言之,就是简化jsp。 如何创建自定义标签 1、方式一 :实现...

  • Spring系列之IOC(3)——自定义标签

    自定义标签在aop、事务标签等都有使用,因此值得好好分析一下。 1、代码分析 自定义的标签如下: 具体实现:对于在...

  • JavaWeb基础恢复之标签、EL表达式、国际化

    1.JSTL标签库 1.1 核心标签 使用 表达式控制标签 流程控制标签 URL操作标签 2.自定义标签 2.1....

  • Android 边框模糊阴影的实现

    使用标签,多个 套 标签实现 2.可以是用Android CardView控件实现,不过这...

  • 使用Apache Tiles进行视图布局

    1、在web.xml中配置项目使用的自定义标签及引入所有标签库。 参考:taglib and jsp-prope...

  • JavaWeb之Tag File

    Tag File: 用于简化自定义标签,使用Tag File可以无须定义标签处理类和标签库文件 自定义标签的使用与...

  • C++ 语言类中各个重要函数原理

    1.命名空间的自定义及使用 全局使用及局部使用。 命名空间变量及函数重复的使用方式 命名空间嵌套 2.构造函数详解...

  • 常用的HTML标签

    1. iframe标签 功能:嵌套页面 1.1 a标签和iframe标签一起使用实现嵌套页面间的切换 2. a标签...

网友评论

      本文标题:2.自定义标签实现及使用

      本文链接:https://www.haomeiwen.com/subject/xzaduftx.html