聊聊Spring的XML Schema扩展机制的使用方式

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在当前Java生态,Spring算的上是最核心的框架,所有的开发组件想要得到大范围更便捷的使用,都要和Spring进行整合,比如我们熟知的Mybatis、Dubbo等,以及内部封装的各类组件包括Redis、MQ、配置中心等。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有了整合这一步,我们只需引入相应的jar,比如mybatis-spring,然后进行简单的配置后即可在Spring工程中使用Mybatis的功能,也正是由于这样的便捷性,导致很多时候我们没有对其进行深究。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所谓整合,即在Spring的框架下进行扩展,让框架能无缝的与Spring工程配合使用。Spring设计了良好的扩展的机制,本文将对Spring的扩展方法及原理进行简单介绍。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"XML Schema扩展","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"打开","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"mybatis-spring","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"dubbo","attrs":{}}],"attrs":{}},{"type":"text","text":"的源码会发现在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"META-INF","attrs":{}}],"attrs":{}},{"type":"text","text":"目录下有两个文件(如下图所示),","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"spring.handlers","attrs":{}}],"attrs":{}},{"type":"text","text":"与","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"spring.schemas","attrs":{}}],"attrs":{}},{"type":"text","text":",这两个文件就是XML Schema扩展的关键入口点。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c7/c79889d74de2b91e1229c65943b5a04e.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"XSD","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"XSD,XML Schema Definition,XML定义。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"XML Schema定义XML文档的结构,XML Schema语言也称为XML定义,即XSD。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"XSD的语法及使用比较简单,可参考https://www.w3school.com.cn/schema/index.asp进行了解熟悉。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"简单的说,XSD用于制定xml文件规范,包括xml中的元素(简单元素、复杂元素)、属性、以及属性类型及约束等。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Spring XML Schema扩展的第一步就是要定义一个xsd文件,比如spring-beans对应xsd文件为http://www.springframework.org/schema/beans/spring-beans.xsd,如下图:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a5/a584657f56930260a6140e316beb82f4.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ef/ef0f1ad744f35bdb015fc521d118521d.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"为了简单介绍Spring XML Schema扩展实现,下面将一个简单例子(模拟一个简单的分布式id生成器,不会实现具体功能)进行说明,xsd定义如下(文件命名为DistributedId.xsd,在META-INF目录下):","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n\n\n\n \n \n \n \n \n \n \n \n\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上述xsd文件里定义了一个复杂元素distributed-id,包含属性id,bizCode,length,形如:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:xmlns,即为xml namespace,xml命名空间,后面跟的http链接地址可以不存在,因为xsd会放在当前工程的META-INF下。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"配置spring.handlers和spring.schemas","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如下两张图所示,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"spring.schemas","attrs":{}}],"attrs":{}},{"type":"text","text":"文件中用于说明xsd的文件路径,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"spring.schemas","attrs":{}}],"attrs":{}},{"type":"text","text":"文件用于说明解析此类xsd定义的标签的处理类,下面会对处理类进行详细说明。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/33/33fc52ef2c2159bb42946a6c2c91b129.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6f/6fa35524c8fd12e15123da79fa84e57a.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"NameSpaceHandler与BeanDefinitionParser","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"定义类DistributedIdNamespaceHandler继承NamespaceHandlerSupport,init方法用于注册BeanDefinition解析器,也就是解析xml中对应标签为Spring Bean。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class DistributedIdNamespaceHandler extends NamespaceHandlerSupport {\n\n @Override\n public void init() {\n registerBeanDefinitionParser(\"distributed-id\", new DistributedIdParser());\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同时要创建BeanDefinitionParser","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class DistributedIdParser implements BeanDefinitionParser {\n\n\n @Override\n public BeanDefinition parse(Element element, ParserContext parserContext) {\n // 解析xml内的标签\n String bizCode = element.getAttribute(\"bizCode\");\n int length = Integer.valueOf(element.getAttribute(\"length\"));\n String id = element.getAttribute(\"id\");\n \n // 创建DistributedIdFactoryBean bean\n BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();\n builder.getRawBeanDefinition().setBeanClass(DistributedIdFactoryBean.class);\n builder.setScope(BeanDefinition.SCOPE_SINGLETON);\n\n builder.addPropertyValue(\"bizCode\", bizCode);\n builder.addPropertyValue(\"length\", length);\n\n BeanDefinition beanDefinition = builder.getBeanDefinition();\n\n parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);\n\n return beanDefinition;\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中DistributedIdFactoryBean实现FactoryBean接口用于创建DistributedIdComponent Bean,如下","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class DistributedIdFactoryBean implements InitializingBean, FactoryBean {\n\n private String bizCode;\n private int length;\n\n private DistributedIdComponent distributedIdComponent;\n\n @Override\n public DistributedIdComponent getObject() throws Exception {\n return distributedIdComponent;\n }\n\n @Override\n public Class> getObjectType() {\n return DistributedIdComponent.class;\n }\n\n @Override\n public boolean isSingleton() {\n return false;\n }\n\n @Override\n public void afterPropertiesSet() throws Exception {\n distributedIdComponent = new DistributedIdComponent(bizCode, length);\n }\n\n public void setBizCode(String bizCode) {\n this.bizCode = bizCode;\n }\n\n public void setLength(int length) {\n this.length = length;\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目标Bean DistributedIdComponent如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class DistributedIdComponent {\n private String bizCode;\n private int length;\n\n public DistributedIdComponent() {\n\n }\n\n public DistributedIdComponent(String bizCode, int length) {\n this.bizCode = bizCode;\n this.length = length;\n }\n\n public String generateId() {\n System.out.println(\"mock generate id\");\n return String.valueOf(System.currentTimeMillis()).substring(0, length);\n }\n\n public String getBizCode() {\n return bizCode;\n }\n\n public void setBizCode(String bizCode) {\n this.bizCode = bizCode;\n }\n\n public int getLength() {\n return length;\n }\n\n public void setLength(int length) {\n this.length = length;\n }\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"使用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"spring配置文件,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"spring-service.xml","attrs":{}}],"attrs":{}},{"type":"text","text":"中配置distributed-id标签以及对应的属性值,如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"xml"},"content":[{"type":"text","text":"\n\n\n\n \n\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"运行容器验证:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class App {\n public static void main(String[] args) {\n ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(\"spring-service.xml\");\n DistributedIdComponent bean = context.getBean(DistributedIdComponent.class);\n\n String id = bean.generateId();\n\n System.out.println(\"id:\" + id);\n }\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"总结","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文主要介绍了Spring XML Schema扩展机制的使用方法,大致步骤为定义XSD文件、配置spring.schemas、编码实现NameSpaceHanlder和BeanDefinitionParser实现类、配置spring.handlers。但未说明具体的实现原理,后续会有一篇文章详细介绍Spring源码是怎么实现扩展的,以及介绍为什么使用FactoryBean来创建具体的Bean等问题。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章