Spring自定义标签解析
整体流程:
自定义一个<msb:user username email age>,完成整个标签的处理工作
流程:1,需要解析的实体类User;
2,自定义标签解析器UserBeanDefinitionParser,继承于AbstractSingleBeanDefinitionParser,重写两个方法getBeanClass()和doParse();
3,解析器的处理类UserNamespaceHandler,继承于NamespaceHandlerSupport,重写方法init();
4,解析器的配置文件和处理类的配置文件,放在META-INF下面,分别为spring.schemas,spring.handlers
5,编写配置文件selftag.xml;
6,编写测试类TestSelfTag
1、创建实体类
User.java
package com.mashibing.selftag;
public class User {
private String username;
private String email;
private String password;
public User() {
}
public User(String username, String email, String password) {
this.username = username;
this.email = email;
this.password = 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;
}
}
2、自定义解析器,继承AbstractSingleBeanDefinitionParser
UserBeanDefinitionParser.java
package com.mashibing.selftag;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
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
UserNamespaceHandler.java
package com.mashibing.selftag;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class UserNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
}
}
4、自定义配置文件spring.handlers、spring.schemas
spring.handlers
http\://www.mashibing.com/schema/user=com.mashibing.selftag.UserNamespaceHandler
spring.schemas
http\://www.mashibing.com/schema/user.xsd=META-INF/user.xsd
5、配置applicationContext.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:msb="http://www.mashibing.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.mashibing.com/schema/user
http://www.mashibing.com/schema/user.xsd
">
<msb:user id="msb" userName="qiangShan" email="1059931090@qq.com" password="root123456"></msb:user>
</beans>
6、编写测试类TestSelfTag.java
TestSelfTag.java
import com.mashibing.selftag.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSelfTag {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user =(User) context.getBean("msb");
System.out.println("username"+user.getUsername()+" "+"email"+user.getEmail());
}
}