- 创建一个需要扩展的组件
在com.test.beans下创建User类
package com.test.beans;
public class User {
private String username;
private String email;
public User() {
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", email='" + email + '\'' +
'}';
}
}
- 定义一个XSD文件描述组件内容
在resources下创建META-INF文件夹,在META-INF下创建user.xsd文件,文件内容为:
<?xml version="1.0" encoding="UTF-8" ?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.test.tag.com/schema/user"
elementFormDefault="qualified">
<element name="user">
<complexType>
<attribute name="id" type="string"/>
<attribute name="username" type="string"/>
<attribute name="email" type="string"/>
</complexType>
</element>
</schema>
- 创建一个文件,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义
在com.test.tag包下创建UserBeanDefinitionParser类,继承自AbstractSingleBeanDefinitionParser类
package com.test.tag;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
import com.test.beans.User;
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 email = element.getAttribute("email");
if (StringUtils.hasText(username)) {
builder.addPropertyValue("username", username);
}
if (StringUtils.hasText(email)) {
builder.addPropertyValue("email", email);
}
}
}
- 创建一个Handler文件,扩展自NamespaceHandlerSupport,目的是将组件注册到Spring容器
在com.test.tag包下创建UserNamespaceHandler类,继承自NamespaceHandlerSupport类
package com.test.tag;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
}
}
- 在META-INF下编写Spring.schemas和Spring.handlers文件
Spring.handlers文件:
http\://www.test.tag.com/schema/user=com.test.tag.UserNamespaceHandler
Spring.schemas文件:
http\://www.test.tag.com/schema/user.xsd=META-INF/user.xsd
- 标签的使用
1.在resources下的spring-context.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:bean="http://www.test.tag.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.test.tag.com/schema/user http://www.test.tag.com/schema/user.xsd">
<bean:user id="test" username="张三" email="zhang3@163.com"/>
</beans>
2.测试
package com.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.beans.User;
public class TestTag {
@Test
public void testTag() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");
User user = (User) ctx.getBean("test");
System.out.println(user.getUsername() + " : " + user.getEmail());
}
}
3.输出
张三 : zhang3@163.com