自定义springXML中的标签
思路:
❝1、创建一个对应的解析器处理类(在init方法中添加parser类)
2、创建一个 普通的spring.handlers(类型为properties)配置文件,让程序能够完成加载
3、创建对应标签的parser类
❞
实体类
User
public class User {
private String username;
private String email;
private String password;
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
命名空间handler初始化解析类
UserNamespaceHandler
(用来解析user标签)
public class UserNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
}
}
定义解析类
UserBeanDefinitionParser
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);
}
}
}
添加handler、xsd等
在resources下面目录新建META-INF
目录,新建spring.handlers
、spring.schemas
、user.xsd
「spring.handlers:」
http\://www.xxx.com/schema/user=UserNamespaceHandler
❝对应到handler的全限定类
❞
「spring.schemas:
」
http\://www.xxx.com/schema/user.xsd=META-INF/user.xsd
「user.xsd
」
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xxx.com/schema/user"
xmlns:tns="http://www.xxx.com/schema/user"
elementFormDefault="qualified">
<element name="user">
<complexType>
<attribute name ="id" type = "string"/>
<attribute name ="userName" type = "string"/>
<attribute name ="email" type = "string"/>
<attribute name ="password" type="string"/>
</complexType>
</element>
</schema>
在xml文件中使用
myxml.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:bbb="http://www.xxx.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.xxx.com/schema/user http://www.xxx.com/schema/user.xsd">
<bbb:user id = "testbean" userName = "jjl" email = "bbb"/>
</beans>
main方法调用
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("myxml.xml");
User user=(User)context.getBean("testbean");
System.out.println("username:"+user.getUsername()+" "+"email:"+user.getEmail());
}
文章转载自蓝眼睛小妖怪,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




