写点什么

Spring- 自定义标签

作者:
  • 2023-10-09
    上海
  • 本文字数:1578 字

    阅读完需:约 5 分钟

Spring-自定义标签

一个简单的自定义标签

<mytag:user id="user" username="test0001" password="test-123456"/>
复制代码

User.class

public class User {	private String username;	private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
@Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; }}
复制代码

META_INF/user.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.void.com/schema/mytag-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="password" type="xsd:string"/> </xsd:complexType> </xsd:element></xsd:schema>
复制代码

UserBeanDefinitionParser.class

public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
@Override protected Class<?> getBeanClass(Element element) { return User.class; }
@Override protected void doParse(Element element, BeanDefinitionBuilder builder) { String username = element.getAttribute("username"); String password = element.getAttribute("password"); if (StringUtils.hasText(username)) builder.addPropertyValue("username", username); if (StringUtils.hasText(password)) builder.addPropertyValue("password", password); }}
复制代码

可继承 PropertyPlaceholderBeanDefinitionParser 或者 AbstractSingleBeanDefinitionParser。

PropertyPlaceholderBeanDefinitionParser 是 AbstractSingleBeanDefinitionParser 的子类,其中的 doParse 方法中多添加了一些通用的属性设置。

MytagUserNamespaceHandler.class

public class MytagUserNamespaceHandler extends NamespaceHandlerSupport {	@Override	public void init() {		registerBeanDefinitionParser("user", new UserBeanDefinitionParser());	}}
复制代码

META-INF/spring.handlers

www.void.com 可以随意编写

http\://www.void.com/schema/mytag-user=com.debug.selftag.MytagUserNamespaceHandler
复制代码

META-INF/spring.schemas

http\://www.void.com/schema/mytag-user.xsd=META-INF/user.xsd
复制代码

applicationContext.xml

添加两行内容

xmlns:mytag="http://www.void.com/schema/mytag-user"

http://www.void.com/schema/mytag-user http://www.void.com/schema/mytag-user.xsd

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	   xmlns="http://www.springframework.org/schema/beans"     xmlns:mytag="http://www.void.com/schema/mytag-user"	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd	   http://www.void.com/schema/mytag-user http://www.void.com/schema/mytag-user.xsd">
<mytag:user id="user" username="test0001" password="test-123456"/>
</beans>
复制代码

测试

	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");	User user = (User) context.getBean("user");	System.out.println(user);
复制代码

输出: User{username='test0001', password='test-123456'}


用户头像

关注

还未添加个人签名 2019-08-17 加入

还未添加个人简介

评论

发布
暂无评论
Spring-自定义标签_spring_生_InfoQ写作社区