spring扩展
自定义标签并解析
目标:解析 <chen:user id="chen" userName="c" email="c@.com" passWord="123">/chen:user
首先定义标签实体类
package com.chen.selftag;
/*
@author chen yun
@date 2022/7/4 14:26
*/
public class User {
private String userName;
private String passWord;
private String email;
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
第二步定义解析器
继承AbstractSingleBeanDefinitionParser类,重写两个方法
package com.chen.selftag;
/*
@author chen yun
@date 2022/7/4 14:27
*/
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 username = element.getAttribute("userName");
String email = element.getAttribute("email");
String password = element.getAttribute("passWord");
if (StringUtils.hasText(username)){
builder.addPropertyValue("userName",username);
}
if (StringUtils.hasText(email)){
builder.addPropertyValue("email",email);
}
if (StringUtils.hasText(password)){
builder.addPropertyValue("passWord",password);
}
}
}
3、注册解析器
继承NamespaceHandlerSupport类,重写方法,讲解析器注册
package com.chen.selftag;
/*
@author chen yun
@date 2022/7/4 14:36
*/
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class UserNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
}
}
4、配置文件
META-INF
spring.handlers
spring.schemas
user.xsd
在resourses目录下,配置如上文件
spring.handlers
http\://www.springframework.org/schema/user=com.chen.selftag.UserNamespaceHandler
spring.schemas
http\://www.springframework.org/schema/user.xsd=META-INF/user.xsd
user.xsd
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.springframework.org/schema/user"
xmlns:tns="http://www.springframework.org/schema/user"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:element name="user">
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string"></xsd:attribute>
<xsd:attribute name="userName" type="xsd:string"></xsd:attribute>
<xsd:attribute name="passWord" type="xsd:string"></xsd:attribute>
<xsd:attribute name="email" type="xsd:string"></xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
以上这些都可以仿照源码进行编写
所有步骤已经完成
测试
public.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:chen="http://www.springframework.org/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/user http://www.springframework.org/schema/user.xsd ">
<chen:user id="chen" userName="c" email="c@.com" passWord="123"></chen:user>
</beans>
测试类
package com.chen.student;
/*
@author chen yun
@date 2022/6/23 10:15
*/
import com.chen.selftag.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("public.xml");
User user = (User) context.getBean("chen");
System.out.println(user.getPassWord());
}
}
任务完成!!