Spring的XML文件自定义标签

先创建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);

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值