1.自定义 parser类 用于标签的解析
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);
}
}
}
public class User {
private String userName;
private String 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;
}
}
2.自定义handler类 注册解析类
public class UserNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("user",new UserBeanDefinitionParser());
}
}
3.再resources下创建 META_INF 文件夹存放 spring.handlers spring.schemas spring-user.xsd 文件
spring.handlers的内容
http\://www.wjs.com/schema/user=com.example.handler.UserNamespaceHandler
spring.schemas的内容
http\://www.wjs.com/schema/user/spring-user.xsd=META-INF/spring-user.xsd
spring-user.xsd的内容
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.wjs.com/schema/user" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.wjs.com/schema/user" elementFormDefault="qualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:element name="user">
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="userName" type="xsd:string"/>
<xsd:attribute name="email" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
4.springCustomTag.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:myTag="http://www.wjs.com/schema/user"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.wjs.com/schema/user http://www.wjs.com/schema/user/spring-user.xsd">
<myTag:user id="testUser" userName="name" email="lx@163.com"/>
</beans>
5.运行
package com.example.controller;
import com.example.bean.domain.User;
import com.example.bean.factory.Animal;
import com.example.test.aop.service.OrderService;
import com.example.test.aop.service.UserService;
import com.example.test.tx.service.UserInfoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author xiang.li
* create date 2018/10/23
* description
*/
public class SpringTest {
public static void main(String[] args) {
ApplicationContext beans=new ClassPathXmlApplicationContext("springCustomTag.xml");
User user=(User) beans.getBean("testUser");
System.out.println("username:"+user.getUserName()+":"+"email:"+user.getEmail());
}
}