当前位置: 首页 > news >正文

西安建站价格表建设电商网站的总结

西安建站价格表,建设电商网站的总结,网站推广公司傻大白,电商购物网站模板下载上篇文章和小伙伴们说了 Spring 源码中 XML 文件的解析流程#xff0c;本来可以继续往下走看加载核心类了#xff0c;但是松哥还是希望能够慢一点#xff0c;既然要学就学懂#xff0c;在 XML 文件解析的过程中还涉及到一些其他的类和概念#xff0c;因此我就先用几篇文章…上篇文章和小伙伴们说了 Spring 源码中 XML 文件的解析流程本来可以继续往下走看加载核心类了但是松哥还是希望能够慢一点既然要学就学懂在 XML 文件解析的过程中还涉及到一些其他的类和概念因此我就先用几篇文章介绍一下这些涉及到的概念或者类然后我们再继续往下看。本文要和大家介绍的是上篇文章中涉及到的 EntityResolver 类看看这个类到底是干嘛用的。本文是 Spring 源码系列第四篇阅读前面文章有助于更好理解本文Spring 源码解读计划Spring 源码第一篇开整配置文件是怎么加载的Spring 源码第二弹XML 文件解析流程先来回顾下在 EntityResolver 这个类在上篇文章哪里出现了。我们在讲到 doLoadDocument 方法时在该方法中调用 loadDocument 方法时传递的第二个参数就是一个 EntityResolver 实例当时我们说这个是用来处理文件的验证方式的但是到底是怎么处理的今天我们就来看下。1.XML 验证模式要了解 EntityResolver就得先来看看 XML 文件验证模式。现在我们大多数情况下可能都是使用 JSON 传递数据XML 使用较少可能有的小伙伴对 XML 文件的一些规则还不太熟悉我这里稍微说一下。XML 是指可扩展标记语言(eXtensible Markup Language)它是一种标记语言类似 HTMLXML 标签没有被预定义需要用户自行定义标签也就是 XML 文件中的节点都是用户自定义的。XML 文件从设计之初就是为了传输数据而非显示数据。一般来说一个 XML 文件由六个部分组成文档生命元素属性注释CDATA 区处理指令虽然说 XML 文件本身是没有预定义 XML 标签但是当 XML 文件作为框架的配置时对于 XML 标签还是要有一定的约束否则每个人都按照自己的喜好定义 XML 标签框架就没法读取这样的 XML 文件了。在 XML 技术中开发者可以通过一个文档来约束一个 XML 的文档中的标签这个文档称之为约束。遵循 XML 语法的 XML 我们称之为格式良好的 XML而遵循 XML 约束的 XML 我们称之为有效的 XML。XML 约束文档主要定义了在 XML 中允许出现的元素名称、属性及元素出现的顺序等等。要想约束 XML 标签有两种方式DTDSchemaDTD(Document Type Definition)全称为文档类型定义一个 DTD 约束文件我们既可以定义在 XML 文件内部也可以定义一个本地文件也可以引用一个网络上的公共的 DTD。XML Schema 也是一种用于定义和描述 XML 文档结构与内容的模式语言相比于 DTDSchema 对于名称空间的支持更加友好同时也支持更多的数据类型而且它的约束能力也比较强大另外还有非常重要的一点是Schema 文档本身也是 XML 文档而不是像 DTD 一样使用自成一体的语法。所以Schema 目前在 XML 约束这块更具备优势也在逐渐替代 DTD。大家在日常开发中这两种约束可能都见过但是有的人可能没注意。我给大家简单举一个例子。早期的 Spring 配置头部是这样的(Spring2.x)这就是 DTD 约束?xml  version1.0 encodingUTF-8?  beans PUBLIC -//SPRING//DTD BEAN 2.0//EN  http://www.springframework.org/dtd/spring-beans-2.0.dtd        现在大家看到的 Spring 配置头部一般都是这样这就是 Schema 约束?xml  version1.0 encodingUTF-8?schema 约束对命名空间有着很好的支持命名空间可以防止命名冲突schema 中的名称空间和约束文件都是成对出现的。有了约束XML 文件中该写什么不该写什么就固定下来了这样框架才能成功解析出 XML 文件。但是大家同时也发现了一个新的问题无论是 DTD 还是 Schema 约束给出的约束文件地址都是一个在线地址这就意味着项目启动时必须能够访问到该在线地址才能加载到约束文件如果访问在线约束文件失败那么项目启动也会失败。为了解决这个问题框架一般都是将约束文件放在本地的在本地哪里呢实际上就在你下载的 jar 包里。以 spring-beans 为例在下载的 jar 包里有如下两个文件spring.handlers 文件内容如下http://www.springframework.org/schema/corg.springframework.beans.factory.xml.SimpleConstructorNamespaceHandlerhttp://www.springframework.org/schema/porg.springframework.beans.factory.xml.SimplePropertyNamespaceHandlerhttp://www.springframework.org/schema/utilorg.springframework.beans.factory.xml.UtilNamespaceHandler这其实一个映射配置每一个名称空间对应的处理类在这里进行配置。spring.schemas 文件内容如下(部分)http://www.springframework.org/schema/beans/spring-beans-2.0.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdorg/springframework/beans/factory/xml/spring-beans.xsdhttp://www.springframework.org/schema/beans/spring-beans.xsdorg/springframework/beans/factory/xml/spring-beans.xsd可以看到各种版本以及没有版本号的约束文件都对应了同一个文件就是 org/springframework/beans/factory/xml/spring-beans.xsd打开这个文件目录我们就可以看到约束文件所以我们虽然在 Spring 的 XML 配置中看到的约束文件是一个在线地址实际上约束文件是从本地 jar 中读取的。2.两种解析器EntityResolver 就是用来处理 XML 验证的。我们先来看下 EntityResolver 接口的定义public interface EntityResolver {    public abstract InputSource resolveEntity (String publicId,                                               String systemId)        throws SAXException, IOException;}接口中就只有一个方法就是加载约束文件。在 Spring 中EntityResolver 的实现类是 DelegatingEntityResolverpublic class DelegatingEntityResolver implements EntityResolver { public static final String DTD_SUFFIX  .dtd; public static final String XSD_SUFFIX  .xsd; private final EntityResolver dtdResolver; private final EntityResolver schemaResolver; public DelegatingEntityResolver(Nullable ClassLoader classLoader) {  this.dtdResolver  new BeansDtdResolver();  this.schemaResolver  new PluggableSchemaResolver(classLoader); } public DelegatingEntityResolver(EntityResolver dtdResolver, EntityResolver schemaResolver) {  this.dtdResolver  dtdResolver;  this.schemaResolver  schemaResolver; } Override Nullable public InputSource resolveEntity(Nullable String publicId, Nullable String systemId)   throws SAXException, IOException {  if (systemId ! null) {   if (systemId.endsWith(DTD_SUFFIX)) {    return this.dtdResolver.resolveEntity(publicId, systemId);   }   else if (systemId.endsWith(XSD_SUFFIX)) {    return this.schemaResolver.resolveEntity(publicId, systemId);   }  }  return null; } Override public String toString() {  return EntityResolver delegating   XSD_SUFFIX   to   this.schemaResolver      and   DTD_SUFFIX   to   this.dtdResolver; }}在 DelegatingEntityResolver 类中首先通过两种不同的后缀来区分不同的约束。然后定义了 dtdResolver 和 schemaResolver 两个不同的变量对应的类型分别是 BeansDtdResolver 和 PluggableSchemaResolver也就是 dtd 和 schema 的约束验证分别由这两个类来处理。在 resolveEntity 方法中根据解析出来不同的后缀分别交由不同的 EntityResolver 来处理。resolveEntity 解析中有两个参数如果是 dtd 解析的话publicId 是有值的如果是 schema 解析publicId 为 null而 systemId 则始终指向具体的约束文件。由于现在大部分都是 schema 约束所以这里我们就来重点看下 PluggableSchemaResolver 类的实现public class PluggableSchemaResolver implements EntityResolver { public static final String DEFAULT_SCHEMA_MAPPINGS_LOCATION  META-INF/spring.schemas; private static final Log logger  LogFactory.getLog(PluggableSchemaResolver.class); Nullable private final ClassLoader classLoader; private final String schemaMappingsLocation; Nullable private volatile Map schemaMappings; public PluggableSchemaResolver(Nullable ClassLoader classLoader) {  this.classLoader  classLoader;  this.schemaMappingsLocation  DEFAULT_SCHEMA_MAPPINGS_LOCATION; } public PluggableSchemaResolver(Nullable ClassLoader classLoader, String schemaMappingsLocation) {  Assert.hasText(schemaMappingsLocation, schemaMappingsLocation must not be empty);  this.classLoader  classLoader;  this.schemaMappingsLocation  schemaMappingsLocation; } Override Nullable public InputSource resolveEntity(Nullable String publicId, Nullable String systemId) throws IOException {  if (logger.isTraceEnabled()) {   logger.trace(Trying to resolve XML entity with public id [  publicId      ] and system id [  systemId  ]);  }  if (systemId ! null) {   String resourceLocation  getSchemaMappings().get(systemId);   if (resourceLocation  null  systemId.startsWith(https:)) {    resourceLocation  getSchemaMappings().get(http:  systemId.substring(6));   }   if (resourceLocation ! null) {    Resource resource  new ClassPathResource(resourceLocation, this.classLoader);    try {     InputSource source  new InputSource(resource.getInputStream());     source.setPublicId(publicId);     source.setSystemId(systemId);     if (logger.isTraceEnabled()) {      logger.trace(Found XML schema [  systemId  ] in classpath:   resourceLocation);     }     return source;    }    catch (FileNotFoundException ex) {     if (logger.isDebugEnabled()) {      logger.debug(Could not find XML schema [  systemId  ]:   resource, ex);     }    }   }  }  return null; } private Map getSchemaMappings() {  Map schemaMappings  this.schemaMappings;  if (schemaMappings  null) {   synchronized (this) {    schemaMappings  this.schemaMappings;    if (schemaMappings  null) {     try {      Properties mappings         PropertiesLoaderUtils.loadAllProperties(this.schemaMappingsLocation, this.classLoader);      schemaMappings  new ConcurrentHashMap(mappings.size());      CollectionUtils.mergePropertiesIntoMap(mappings, schemaMappings);      this.schemaMappings  schemaMappings;     }     catch (IOException ex) {      throw new IllegalStateException(        Unable to load schema mappings from location [  this.schemaMappingsLocation  ], ex);     }    }   }  }  return schemaMappings; } Override public String toString() {  return EntityResolver using schema mappings   getSchemaMappings(); }}在这个类中一上来先通过 DEFAULT_SCHEMA_MAPPINGS_LOCATION 变量定义了 spring.schemas 文件的位置。getSchemaMappings 方法则是将 spring.schemas 文件中的内容读取成一个 Map 加载进来。在 resolveEntity 方法中根据 systemId 找到文件路径systemId 是 http://www.springframework.org/schema/beans/spring-beans.xsd 格式文件路径则是 org/springframework/beans/factory/xml/spring-beans.xsd如果第一次没有加载到就把用户的 https: 替换成 http: 再去加载。有了文件路径接下来调用 ClassPathResource 去获取一个 Resource 对象这块可以参考本系列第二篇这里我就不再赘述。最后构造一个 InputSource 返回即可。在上篇文章中我们获取 EntityResolver 是通过 getEntityResolver 方法来获取的protected EntityResolver getEntityResolver() { if (this.entityResolver  null) {  // Determine default EntityResolver to use.  ResourceLoader resourceLoader  getResourceLoader();  if (resourceLoader ! null) {   this.entityResolver  new ResourceEntityResolver(resourceLoader);  }  else {   this.entityResolver  new DelegatingEntityResolver(getBeanClassLoader());  } } return this.entityResolver;}这里最终返回的是 ResourceEntityResolverResourceEntityResolver 继承自 DelegatingEntityResolver当调用 resolveEntity 方法时也是先调用父类的该方法进行处理如果父类方法处理成功了就直接返回父类方法给出的结果如果父类方法处理失败了则在 ResourceEntityResolver 中通过资源的相对路径再次尝试加载。3.小结好啦经过上面的介绍相信大家对于 XMl 约束和 EntityResolver 都有一定的了解啦。后记本文刚写完微信群里就有小伙伴问了一个一模一样的问题松哥不禁感叹源码并非离我们很远的东西阅读源码可以有效解决我们日常开发中一些实实在在的问题如果觉得有收获记得点个在看鼓励下松哥哦搜索微信公众号【江南一点雨】回复 888 获取超 17k star 开源项目学习文档
http://www.huolong8.cn/news/134308/

相关文章:

  • 做网站的前途个人可以做导航网站吗
  • 找人做个网站建设制作报价方案wordpress换域名主题
  • 英语培训学校网站怎么做cms系统表单
  • 京东的网站是哪家公司做永清县建设局网站
  • h5和手机网站wordpress 自定义链接
  • 网站支付功能怎么做设计网站都有哪些
  • 电力建设期刊网站经常维护吗福州如何做百度的网站
  • 哪个网站的ps元素好网站建设需要具备
  • 网站建设多少钱 知乎网站制作咨询
  • 怎么用frontpage做网站什么网站做装修的
  • 网站seo外链平台紧固件做网站有效果吗
  • 介绍一学一做视频网站吗商城购物网站开发背景
  • 找专业公司做网站蒙阴县建设局网站
  • 网站设计素材网站合肥市城乡城乡建设局网站
  • 富阳网站制作做网站语言排名2018
  • 南阳网站建设域名公司网站集约化建设试点
  • 网站开发页面设计过程便捷的邢台做网站
  • 怎么开网页游戏平台seo建站教学
  • wordpress删除小工具英文谷歌seo
  • 网站建设托管产品图建筑设计经典案例分析
  • php网站开发环境配置湛江房产信息网
  • 网站论坛制作小型网站开发成本
  • 怎么建商城网站深圳网站seo服务
  • 西华县住房和城乡建设局网站响应式网站案例
  • 织梦系统网站地图模板下载网页微博怎么发文章
  • 保定专业网站建设北京学校网站建设
  • 淡蓝黑色大气企业网站模板品牌网站建设信息
  • asp网站如何发布wordpress 4.1分页
  • 明星设计网站风格说明百度app安卓版下载
  • wordpress建博客网站江西省城乡建设厅网站证件查询