Spring 自定义标签配置

前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等。那么这些Spring标签是如何自定义配置的?学习Spring标签的自定义配置为以后实现分布式服务框架做技术储备。

技术分析:Spring的标签配置是通过XML来实现的,通过XSD(xml Schema Definition)来定义元素,属性,数据类型等。

Spring自定义标签解析

1.Spring启动时通过扫描根目录下的META-INF文件下的spring.handlers和spring.schemas文件找到处理类所在的位置以及schemas的路径。

       spring.handlers                             

http\://jewel.com/schema/jewel=com.jewel.spring.tag.handler.JewelNamespaceHandler

  spring.schemas

http\://jewel.com/schema/jewel.xsd=META-INF/jewel.xsd

 2.实现命名空间的处理提供类(NamespaceHandlerSupport):这个类和spring.handlers中的路径对应

public class JewelNamespaceHandler extends NamespaceHandlerSupport {

    public void init() {
        registerBeanDefinitionParser("application", new AppBeanDefinitionParser(AppBeanCnf.class));
    }

}

3.实现解析类(AppBeanDefinitionParser)负责对标签的解析

public class AppBeanDefinitionParser implements BeanDefinitionParser {

	private Class<?> clssze;

	public AppBeanDefinitionParser(Class<?> cls) {
		this.clssze = cls;
	}

	public BeanDefinition parse(Element element, ParserContext parserContext) {
		//其中Element类负责获取标签信息
          //ParserContext是Spring上下文可以将bean注册到Spring中在这里负责注入工作 } }

 4.XSD文件配置

jewel.xsd文件:改文件和spring.schemas中对应位置

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema 
    xmlns="http://jewel.com/schema/jewel" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:beans="http://www.springframework.org/schema/beans"
    targetNamespace="http://jewel.com/schema/jewel" 
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans" />

    <xsd:element name="application">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="beans:identifiedType">
                    <xsd:attribute name="name" type="xsd:string" />
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
    
</xsd:schema>   

5.在标签配置(beans.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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p" 				xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context" 	xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
	xmlns:tool="http://www.springframework.org/schema/tool" 		xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jewel="http://jewel.com/schema/jewel"-------------命名空间的指定
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://jewel.com/schema/jewel http://jewel.com/schema/jewel.xsd"----------路径指定>
<!-- 注解支持 -->
<jewel:application name="demo-app"></jewel:application>-------------------标签配置
</beans>

  

总结:通过以上步骤我们就可以自定义application标签并且可以通过Sping解析。

 

 

         

 

    

转载于:https://www.cnblogs.com/maybo/p/5599701.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值