Spring中如何自定义标签呢?
下文笔者讲述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>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。