{"id":12076,"date":"2023-11-28T22:42:45","date_gmt":"2023-11-28T14:42:45","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12076"},"modified":"2023-12-11T15:21:26","modified_gmt":"2023-12-11T07:21:26","slug":"spring-zhong-ru-he-ji-yu-extensible-xml-authoring","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-ru-he-ji-yu-extensible-xml-authoring\/","title":{"rendered":"Spring \u4e2d\u5982\u4f55\u57fa\u4e8e Extensible XML authoring \u6269\u5c55 SpringXML \u5143\u7d20\uff1f"},"content":{"rendered":"<p>\u5728Spring\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528&quot;Extensible XML authoring&quot;\uff08\u53ef\u6269\u5c55\u7684XML\u4f5c\u8005ing\uff09\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u6269\u5c55Spring XML\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5143\u7d20\uff0c\u4ee5\u5f15\u5165\u81ea\u5b9a\u4e49\u7684\u914d\u7f6e\u5143\u7d20\u3002\u8fd9\u6837\u53ef\u4ee5\u5b9e\u73b0\u5bf9Spring\u7684\u81ea\u5b9a\u4e49\u6269\u5c55\uff0c\u9002\u5e94\u7279\u5b9a\u4e1a\u52a1\u9700\u6c42\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u57fa\u4e8eExtensible XML authoring\u6269\u5c55Spring XML\u5143\u7d20\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548cXSD\uff08XML Schema Definition\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u9700\u8981\u5b9a\u4e49\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u548c\u76f8\u5e94\u7684XML Schema\uff0c\u4ee5\u63cf\u8ff0\u4f60\u7684\u6269\u5c55\u5143\u7d20\u7684\u7ed3\u6784\u3002XSD\u6587\u4ef6\u5b9a\u4e49\u4e86\u5143\u7d20\u7684\u5c5e\u6027\u3001\u5b50\u5143\u7d20\u7b49\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xsd\">&lt;!-- my-extension.xsd --&gt;\n&lt;xs:schema xmlns:xs=&quot;http:\/\/www.w3.org\/2001\/XMLSchema&quot;\n           targetNamespace=&quot;http:\/\/www.example.com\/schema\/my-extension&quot;\n           xmlns=&quot;http:\/\/www.example.com\/schema\/my-extension&quot;\n           elementFormDefault=&quot;qualified&quot;&gt;\n\n    &lt;xs:element name=&quot;customElement&quot;&gt;\n        &lt;xs:complexType&gt;\n            &lt;xs:attribute name=&quot;attribute1&quot; type=&quot;xs:string&quot; \/&gt;\n            &lt;!-- Define more attributes or child elements as needed --&gt;\n        &lt;\/xs:complexType&gt;\n    &lt;\/xs:element&gt;\n\n&lt;\/xs:schema&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6ce8\u518cXSD\u6587\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728Spring XML\u914d\u7f6e\u6587\u4ef6\u4e2d\u6ce8\u518c\u81ea\u5b9a\u4e49\u7684XSD\u6587\u4ef6\uff0c\u544a\u8bc9Spring\u6846\u67b6\u5982\u4f55\u89e3\u6790\u548c\u5904\u7406\u8fd9\u4e2a\u6269\u5c55\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;!-- applicationContext.xml --&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:myext=&quot;http:\/\/www.example.com\/schema\/my-extension&quot;\n       xsi:schemaLocation=&quot;\n           http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n           http:\/\/www.example.com\/schema\/my-extension\n           classpath:my-extension.xsd&quot;&gt;\n\n    &lt;!-- Use custom element --&gt;\n    &lt;myext:customElement attribute1=&quot;value1&quot; \/&gt;\n\n&lt;\/beans&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u7f16\u5199NamespaceHandler\u548cBeanDefinitionParser\uff1a<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2aNamespaceHandler\u548c\u4e00\u4e2aBeanDefinitionParser\uff0c\u5b83\u4eec\u8d1f\u8d23\u89e3\u6790\u548c\u5904\u7406\u81ea\u5b9a\u4e49\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">\/\/ CustomNamespaceHandler.java\npublic class CustomNamespaceHandler extends NamespaceHandlerSupport {\n\n    @Override\n    public void init() {\n        registerBeanDefinitionParser(&quot;customElement&quot;, new CustomElementBeanDefinitionParser());\n    }\n}\n\n\/\/ CustomElementBeanDefinitionParser.java\npublic class CustomElementBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {\n\n    @Override\n    protected Class&lt;?&gt; getBeanClass(Element element) {\n        return CustomElement.class;\n    }\n\n    @Override\n    protected void doParse(Element element, BeanDefinitionBuilder builder) {\n        \/\/ Parse and configure the custom element attributes\n        String attribute1 = element.getAttribute(&quot;attribute1&quot;);\n        builder.addPropertyValue(&quot;attribute1&quot;, attribute1);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6ce8\u518cNamespaceHandler\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728Spring XML\u914d\u7f6e\u6587\u4ef6\u4e2d\u6ce8\u518c\u81ea\u5b9a\u4e49\u7684NamespaceHandler\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;!-- applicationContext.xml --&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:myext=&quot;http:\/\/www.example.com\/schema\/my-extension&quot;\n       xsi:schemaLocation=&quot;\n           http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n           http:\/\/www.example.com\/schema\/my-extension\n           classpath:my-extension.xsd&quot;&gt;\n\n    &lt;myext:customElement attribute1=&quot;value1&quot; \/&gt;\n\n&lt;\/beans&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u4f7f\u7528\u6269\u5c55\u7684\u5143\u7d20\uff1a<\/strong><\/p>\n<ul>\n<li>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728Spring\u914d\u7f6e\u6587\u4ef6\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u6269\u5c55\u5143\u7d20\u4e86\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;!-- applicationContext.xml --&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:myext=&quot;http:\/\/www.example.com\/schema\/my-extension&quot;\n       xsi:schemaLocation=&quot;\n           http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n           http:\/\/www.example.com\/schema\/my-extension\n           classpath:my-extension.xsd&quot;&gt;\n\n    &lt;myext:customElement attribute1=&quot;value1&quot; \/&gt;\n\n&lt;\/beans&gt;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u5c31\u53ef\u4ee5\u5b9e\u73b0\u57fa\u4e8eExtensible XML authoring\u7684Spring XML\u5143\u7d20\u6269\u5c55\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u4f60\u5728Spring\u914d\u7f6e\u6587\u4ef6\u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u7684XML\u5143\u7d20\uff0c\u4ee5\u5b9e\u73b0\u5bf9Spring\u6846\u67b6\u7684\u5b9a\u5236\u548c\u6269\u5c55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528&quot;Extensible XML authoring&quot;\uff08\u53ef\u6269\u5c55\u7684X [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-12076","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=12076"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12076\/revisions"}],"predecessor-version":[{"id":47848,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12076\/revisions\/47848"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12076"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}