先创建Spring.handlers 和 Spring.schemas文件在META-INF目录下
spring.handlers
文件内容
代表当前命名空间对应的处理类
http\://www.xxx.org/schema/custom=first.CustomHandler
spring.schemas
文件内容,代表当不能联网找到xsd文件时,去找本地META-INF/xx/custom.xsd文件
http\://www.xxx.org/schema/custom.xsd=xx/custom.xsd
Handler处理类
package first;/*
* @ClassName Handler
* @Desc TODO
* @Author 19637
* @Date 2022/12/22 16:51
* @Version 1.0
*/
import org.springframework.beans.factory.xml.NamespaceHandler;
import org.springframework.context.config.ContextNamespaceHandler;
//随意继承一个NameSpaceHandler
public class CustomHandler extends ContextNamespaceHandler {
@Override
public void init() {
//注册解析器,代表xsd文件中每个元素对应的解析器
registerBeanDefinitionParser("person",new CustomParse());
}
}
CustomParse()解析器
package first;/*
* @ClassName CustomParse
* @Desc TODO
* @Author 19637
* @Date 2022/12/22 16:58
* @Version 1.0
*/
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
public class CustomParse implements BeanDefinitionParser {
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
AbstractBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(Person.class);
String name = element.getAttribute("name");
MutablePropertyValues propertyValues = new MutablePropertyValues();
if (StringUtils.hasText(name)) {
propertyValues.addPropertyValue("name", name);
}
String age = element.getAttribute("age");
if (StringUtils.hasText(age)) {
propertyValues.addPropertyValue("age", Integer.parseInt(age));
}
beanDefinition.setPropertyValues(propertyValues);
System.out.println();
//注册Bean定义信息
parserContext.getRegistry().registerBeanDefinition("person", beanDefinition);
return null;
}
}
对应的custom.xsd
文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://www.xxx.org/schema/custom"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xxx.org/schema/custom">
<xsd:element name="person">
<xsd:complexType>
<xsd:attribute name="age" type="xsd:integer"/>
<xsd:attribute name="name" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
使用自定义的标签
<?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:custom="http://www.xxx.org/schema/custom"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.xxx.org/schema/custom
http://www.xxx.org/schema/custom.xsd
"
>
<!--使用自定义-->
<custom:person name="123" age="12"/>
</beans>
获取Bean看看
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("spring-bean.xml");
Object person = context.getBean("person");
System.out.println(person);