码迷,mamicode.com
首页 > 编程语言 > 详细

nacos-spring-project 的 xml 配置

时间:2020-03-14 16:28:34      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:target   ann   lib   gif   inf   实现   onclick   ebe   override   

nacos-spring-context 作为一座桥连接 spring 项目和 nacos-client

在 spring 项目中使用自定义的 xml 标签

1. 编写 xsd 文件

技术图片
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://nacos.io/schema/nacos"
            targetNamespace="http://nacos.io/schema/nacos">

    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>

    <xsd:annotation>
        <xsd:documentation>
            <![CDATA[ Nacos Framework Schema ]]>
        </xsd:documentation>
    </xsd:annotation>

    <xsd:element name="annotation-driven">
        <xsd:annotation>
            <xsd:documentation>
                <![CDATA[
                Activates various Nacos Spring‘s annotations :
                @NacosInjected
                @NacosConfigListener
                @NacosPropertySource
                @NacosPropertySources
                @NacosConfigProperties / @NacosProperty / @NacosIgnore
            ]]></xsd:documentation>
        </xsd:annotation>
    </xsd:element>

    <xsd:element name="global-properties">
        <xsd:annotation>
            <xsd:documentation>
                <![CDATA[
                Register Nacos global Properties whose values are configured from attributes.
                ]]>
            </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:attribute name="endpoint" default="${nacos.endpoint:}"/>
            <xsd:attribute name="namespace" default="${nacos.endpoint:}"/>
            <xsd:attribute name="access-key" default="${nacos.access-key:}"/>
            <xsd:attribute name="secret-key" default="${nacos.secret-key:}"/>
            <xsd:attribute name="server-addr" default="${nacos.server-addr:}"/>
            <xsd:attribute name="context-path" default="${nacos.context-path:}"/>
            <xsd:attribute name="cluster-name" default="${nacos.cluster-name:}"/>
            <xsd:attribute name="encode" default="${nacos.encode:UTF-8}"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="property-source">
        <xsd:annotation>
            <xsd:documentation>
                <![CDATA[
                Add Nacos property source
                ]]>
            </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:attribute name="name" default=""/>
            <xsd:attribute name="group-id" default="DEFAULT_GROUP"/>
            <xsd:attribute name="data-id" use="required"/>
            <xsd:attribute name="data-ids"/>
            <xsd:attribute name="auto-refreshed" default="false"/>
            <xsd:attribute name="first" default="false"/>
            <xsd:attribute name="before" default=""/>
            <xsd:attribute name="after" default=""/>
            <xsd:attribute name="type" default="properties" />
        </xsd:complexType>
    </xsd:element>

</xsd:schema>
View Code

 

2. 实现 NamespaceHandler 和 BeanDefinitionParser

public class NacosNamespaceHandler extends NamespaceHandlerSupport {

    @Override
    public void init() {
        registerBeanDefinitionParser("annotation-driven", new NacosAnnotationDrivenBeanDefinitionParser());
        registerBeanDefinitionParser("global-properties", new GlobalNacosPropertiesBeanDefinitionParser());
        registerBeanDefinitionParser("property-source", new NacosPropertySourceBeanDefinitionParser());
    }
}

 

3. 编写 spring.handlers 和 spring.schemas 文件

http\://nacos.io/schema/nacos=com.alibaba.nacos.spring.context.config.xml.NacosNamespaceHandler
http\://nacos.io/schema/nacos.xsd=META-INF/schemas/nacos.xsd

 

nacos-spring-project 的 xml 配置

标签:target   ann   lib   gif   inf   实现   onclick   ebe   override   

原文地址:https://www.cnblogs.com/allenwas3/p/11394092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!