• 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解析。

             

        

  • 相关阅读:
    SSH整合中,使用父action重构子类action类.(在父类中获取子类中的泛型对象)
    算法大神之路----排序(插入排序法)
    Redis的基本使用(基于maven和spring)
    算法大神之路----排序(选择排序法)
    exe4j 安装
    png转ico网站
    artTemplate/template.js模板将时间格式化为正常的日期
    artTemplate js模板引擎动态给html赋值
    IIS7的集成模式下如何让自定义的HttpModule不处理静态文件(.html .css .js .jpeg等)请求
    Windows Server2012 R2 安装.NET Framework 3.5失败解决方法
  • 原文地址:https://www.cnblogs.com/maybo/p/5599701.html
Copyright © 2020-2023  润新知