zoukankan      html  css  js  c++  java
  • 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解析。

             

        

  • 相关阅读:
    SQL Server 重新组织生成索引
    atitit.软件设计模式大的总结attialx总结
    Linux防火墙限制指定port仅仅能由指定IP訪问
    android TextView里边实现图文混配效果
    Codeforces Round #270
    HTML5中x-webkit-speech语音输入功能
    oracle11g中SQL优化(SQL TUNING)新特性之SQL Plan Management(SPM)
    Android-HttpURLConnection自己主动管理cookie
    iOS UI01_UIView
    Okio简化处理I/O操作原理
  • 原文地址:https://www.cnblogs.com/maybo/p/5599701.html
Copyright © 2011-2022 走看看