Spring中如何自定义标签呢?

Spring 发布时间:2024-11-21 14:50:08 阅读数:1826 1
下文笔者讲述Spring中自定义标签的方法及示例分享,如下所示
我们从Spring源码中的得知
   spring中除 http://www.springframework.org/schema/beans 提供默认标签
     其他都采用自定义标签的处理模式,那么我们自己如何自定义一个自定义标签
   下文笔者将一一道来,如下所示
  
========================================================================
1.定义 Entity  UserInfo
2.定义 UserNameSpaceHandler
3.定义 UserBeanDefinitionParser
4.定义 spring.handlers (自动加载相应的处理器)
5.定义 spring.schemas (定义xsd的位置)
6.定义 xsd文件
7.定义 applicationContext.xml 将 xsd 
例:
 
 UserBeanDefinitionParser 
 
 package com.jd.customTag;

import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {


    protected Class getBeanClass(org.w3c.dom.Element element) {
        return com.jd.customTag.UserInfo.class;
    }

    protected void doParse(org.w3c.dom.Element element,
                           org.springframework.beans.factory.xml.ParserContext parserContext,
                           org.springframework.beans.factory.support.BeanDefinitionBuilder builder) {


        String id = element.getAttribute("id");
        String name = element.getAttribute("userName");
        String email = element.getAttribute("email");

        if(StringUtils.hasText(id)) {
            builder.addPropertyValue("id", id);
        }

        if(StringUtils.hasText(name)) {
            builder.addPropertyValue("name", name);
        }

        if(StringUtils.hasText(email)) {
            builder.addPropertyValue("email", email);
        }
    }
}



UserInfo

package com.jd.customTag;

public class UserInfo {

    private String id;
    private String name;
    private String email;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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 String toString()
    {
    	return "UserInfo [id="+id+",name="+name+",email="+email+"]";
    }

}



UserNameSpaceHandler.java


package com.jd.customTag;

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

public class UserNameSpaceHandler extends NamespaceHandlerSupport {

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


spring.handlers 

http\://www.java265.com/schema/user=com.jd.customTag.UserNameSpaceHandler

spring.schemas

http\://www.java265.com/schema/user=com.jd.customTag.UserNameSpaceHandler

user.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.java265.com/schema/user"
            elementFormDefault="qualified" >

    <xsd:element name="user">
        <xsd:complexType>
            <xsd:attribute name="id" type="xsd:string"/>
            <xsd:attribute name="userName" type="xsd:string"/>
            <xsd:attribute name="email" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

</xsd:schema>


applicationContext.xml

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


     <!--userExtend是命名空间 http://www.java265.com/schema/user的别名-->
    <userExtend:user   id="userInfo" userName="最爱德网站" email="admin@java265.com"/>

</beans>
Spring自定义标签
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/Spring/202411/8185.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者