扩展Spring自定义标签配置大概需要以下几步:
AbstractSingleBeanDefinitionParser
接口的类,用来解析XSD文件中的定义和组件定义接下来我们按着上面的步骤进行创建,这里我会创建两个组件,也可以创建一个,大同小异,后面演示用。
package com.vipbbo.spring.bean.customtag2;
/**
* 需要扩展的组件
*/
public class User {
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.vipbbo.spring.bean.customtag2;
/**
* 需要扩展的组件
*/
public class Phone {
private String color;
private int size;
private String remark;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Phone{" +
"color='" + color + '\'' +
", size=" + size +
", remark='" + remark + '\'' +
'}';
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://hellospring.vipbbo.com/schema/custom"
targetNamespace="http://hellospring.vipbbo.com/schema/custom"
elementFormDefault="qualified">
<xsd:element name="user">
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string">
</xsd:attribute>
<xsd:attribute name="name" type="xsd:string">
</xsd:attribute>
<xsd:attribute name="age" type="xsd:int">
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="phone">
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string">
</xsd:attribute>
<xsd:attribute name="color" type="xsd:string">
</xsd:attribute>
<xsd:attribute name="size" type="xsd:int">
</xsd:attribute>
<xsd:attribute name="remark" type="xsd:string">
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
这里需要注意的是xmlns
和targetNamespace
要保持一致
该类是一个继承了AbstractSingleBeanDefinitionParser
的Parser类,并实现了getBeanCladd()
和doParse()
两个方法。主要用于解析XSD文件中的定义和组件的定义,这里我定义了两个Parser类,一个解析User,一个解析Phone
package com.vipbbo.spring.bean.customtag2;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(Element element) {
return User.class;
}
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String name = element.getAttribute("name");
String age = element.getAttribute("age");
if (StringUtils.hasText(name)){
builder.addPropertyValue("name",name);
}
if (StringUtils.hasText(age)) {
builder.addPropertyValue("age",age);
}
}
}
package com.vipbbo.spring.bean.customtag2;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
public class PhoneBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(Element element) {
return Phone.class;
}
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String color = element.getAttribute("color");
String size = element.getAttribute("size");
String remark = element.getAttribute("remark");
if (StringUtils.hasText(color)) {
builder.addPropertyValue("color",color);
}
if (StringUtils.hasText(size)) {
builder.addPropertyValue("size",size);
}
if (StringUtils.hasText(remark)) {
builder.addPropertyValue("remark",remark);
}
}
}
定义一个Handler类,继承NamespaceHandlerSupport
,主要目的是将上面定义的解析器Parser类注册到Spring容器中。
package com.vipbbo.spring.bean.customtag2;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class CustomNameSpaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
registerBeanDefinitionParser("phone",new PhoneBeanDefinitionParser());
}
}
这个类主要是将解析器 UserBeanDefinitionParser和 PhoneBeanDefinitionParser 的实例放到全局的Map中,key为user和phone。看源码
NamespaceHandlerSupport.java
)private final Map<String, BeanDefinitionParser> parsers = new HashMap<>();
//省略......
protected final void registerBeanDefinitionParser(String elementName, BeanDefinitionParser parser) {
this.parsers.put(elementName, parser);
}
编写Spring.handlers和spring.schemas文件,默认放在META-INF文件夹下
http://hellospring.vipbbo.com/schema/custom=com.vipbbo.spring.bean.customtag2.CustomNameSpaceHandler
http://hellospring.vipbbo.com/schema/tag/customtag.xsd=META-INF/customtag.xsd
<?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:customtag="http://hellospring.vipbbo.com/schema/custom"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://hellospring.vipbbo.com/schema/custom
http://hellospring.vipbbo.com/schema/tag/customtag.xsd
">
<customtag:user id="user" name="paidaxing" age="18"/>
<customtag:phone id="phone" color="black" size="14" remark="pro"/>
</beans>
xmlns:customtag表示customtag的命名空间是http://hellospring.vipbbo.com/schema/custom,在文章开头的判断处 if (delegate.isDefaultNamespace(ele)) 肯定会返回false,将进入到自定义标签的解析
package com.vipbbo.spring.bean.customtag2;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CustomTagTest {
@Test
public void TagTest(){
ApplicationContext ac =
new ClassPathXmlApplicationContext("custom-tag2.xml");
User user = (User) ac.getBean("user");
Phone phone = (Phone) ac.getBean("phone");
System.out.println(user);
System.out.println(phone);
}
}
测试截图
项目层级
好了,本次的Spring的自定义标签的使用就说完了,希望大家新年快乐啦。