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

网络科技建设网站查询网

网络科技建设网站,查询网,wordpress自己新建模板,网站建设用模板好吗核心配置中${}表达式配置的解析一、核心配置主体二、核心配置文件中properties是如何被解析的?三、${} 表达式的解析四、总结前提: 核心配置文件是被XMLConfigBuilder 对象进行解析的,configuration 对象是由它父类BaseBuider继承下来的属性…

核心配置中${}表达式配置的解析

  • 一、核心配置主体
  • 二、核心配置文件中properties是如何被解析的?
  • 三、${} 表达式的解析
  • 四、总结

前提:

核心配置文件是被XMLConfigBuilder 对象进行解析的,configuration 对象是由它父类BaseBuider继承下来的属性。
XMLConfigBuilder 对象解析完配置文件,其信息是被封装在了configuration 对象中,
然后返回,通过SqlSessionFactoryBuilder 去通过build(configuration)方法进行构建SqlSessionFactory对象,
一个数据库是关联一个environment 的,所以是一个sqlSessionFactory 对象对应一个数据库,实际上也对应一个configuration 对象........

一、核心配置主体

配置信息的配置主体先进行阐明:

public Configuration parse() {  if (parsed) {  throw new BuilderException("Each XMLConfigBuilder can only be used once.");  }  parsed = true;  //源码中没有这一句,只有 parseConfiguration(parser.evalNode("/configuration"));  //为了让读者看得更明晰,源码拆分为以下两句  XNode configurationNode = parser.evalNode("/configuration");  parseConfiguration(configurationNode);  return configuration;  
}  
/** * 解析 "/configuration"节点下的子节点信息,然后将解析的结果设置到Configuration对象中 */  
private void parseConfiguration(XNode root) {  try {  //1.首先处理properties 节点     propertiesElement(root.evalNode("properties")); //issue #117 read properties first  //2.处理typeAliases  typeAliasesElement(root.evalNode("typeAliases"));  //3.处理插件  pluginElement(root.evalNode("plugins"));  //4.处理objectFactory  objectFactoryElement(root.evalNode("objectFactory"));  //5.objectWrapperFactory  objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));  //6.settings  settingsElement(root.evalNode("settings"));  //7.处理environments  environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631  //8.database  databaseIdProviderElement(root.evalNode("databaseIdProvider"));  //9.typeHandlers  typeHandlerElement(root.evalNode("typeHandlers"));  //10.mappers  mapperElement(root.evalNode("mappers"));  } catch (Exception e) {  throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);  }  
} 

再看核心配置文件中DTD约束,不难看出其解析顺序和约束是一致的。这些解析出来的结果最后都会封装到configuration对象中。

二、核心配置文件中properties是如何被解析的?

properties的三种配置方式:

  1. 通过property 子标签,进行name<==>value进行配置;
  2. 通过url 属性(外配置文件);
  3. 通过resource 属性(外配置文件)。为契合项目路径,这种方式使用的多。

下面阅读已被吾注解好了的解析properties 的代码

  private void propertiesElement(XNode context) throws Exception {if (context != null) {// 第一种配置方式Properties defaults = context.getChildrenAsProperties();// 第三种利用resource配置String resource = context.getStringAttribute("resource");// 第二种利用url进行配置String url = context.getStringAttribute("url");// 第二种和第三种配置不能同时存在// 意思就是resource 属性和 url 属性不能同时存在。// 如果同时存在的话就抛异常if (resource != null && url != null) {throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");}// 下面就是分别对resource和url进行判断了if (resource != null) {defaults.putAll(Resources.getResourceAsProperties(resource));} else if (url != null) {defaults.putAll(Resources.getUrlAsProperties(url));}// 这个是看原先有没有对cofiguration中的variables属性赋值// 如果有的话一并加入到defaults这个对象中Properties vars = configuration.getVariables();if (vars != null) {defaults.putAll(vars);}// parser 是一个XPathParser的对象;其中有variable是其中的一个属性// variable 是Properties 对象;// 先对parser 的variable的属性进行赋值,后面用于 对datasource 的配置有用parser.setVariables(defaults);// 解析的结果最后得在configuration中,所以....configuration.setVariables(defaults);}}

这里需要注意的有两点:

  1. SqlSessionBuilder执行build方法的时候,也是可以传一个Properties 对象的,这个对象会在XMLConfigBuilder对象创建的时候赋值给configuration对象,这也就是上面源码那个为什么要去判断一下有没有提前赋值(上面的vars)。
  2. 这里defaults 对象虽然不允许 url 和 resource 属性值都时接受,但是允许接受完 url 或 resource的配置文件后还可以加上第一种配置产生的值,还可以接受 build 传过来的配置,非常的灵活。
  3. Configuration 对象中的set、get方法是对外提供的,当然也可以自行对其进行修改和获取。当然没啥事谁修改这玩意啊。

三、${} 表达式的解析

回到根本,${} 到底是如何解析的呢?

首先需要了解 XNode 类中的 evalNode(String expression) 方法。

在这里插入图片描述
这个 XPathParser 对象都是使用的 XMLConfigBuilder 内的属性 parser 对象。

在这里插入图片描述

variables 解析其他标签也都是共享的,一级传一级的。

所以也就是说解析 datasource 是可以使用共享的 variables 的。

然后就可以看解析的 environmentElement 了,看看。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

解析这个 '${}‘ 的核心代码如下:

/*** 这个类解析${}这种形式的表达式*/
public class PropertyParser {public static String parse(String string, Properties variables) {VariableTokenHandler handler = new VariableTokenHandler(variables);GenericTokenParser parser = new GenericTokenParser("${", "}", handler);return parser.parse(string);}private static class VariableTokenHandler implements TokenHandler {private Properties variables;public VariableTokenHandler(Properties variables) {this.variables = variables;}public String handleToken(String content) {if (variables != null && variables.containsKey(content)) {return variables.getProperty(content);}return "${" + content + "}";}}
}

四、总结

  1. 解析顺序和DTD约束是一致的;
  2. properties 配置的解析及其三种配置方式,resource和url不可以共存,但使用property子标签配置可以共存;
  3. XMLConfigBuilder 中的 XPathParser 类型对象 parser 属性,在解析过程中一直使用的是同一个,并且所解析的 variables 那个配置也一直在传递;
  4. variables 可以是properties 中所写的配置,也可以是调用SqlSessionBuilder对象中的build方法进行传入;
  5. ${content} 是通过字符串处理的方式和从 variables 查询 content 的方式进行处理的。

文章转载自:
http://dinncolanyard.ssfq.cn
http://dinncodeexcitation.ssfq.cn
http://dinncogymnogenous.ssfq.cn
http://dinncogangly.ssfq.cn
http://dinncogop.ssfq.cn
http://dinncoretrobulbar.ssfq.cn
http://dinncoadularia.ssfq.cn
http://dinncotortoiseshell.ssfq.cn
http://dinncofossiliferous.ssfq.cn
http://dinncocreswellian.ssfq.cn
http://dinncorathaus.ssfq.cn
http://dinncodoubleender.ssfq.cn
http://dinncoorthopaedy.ssfq.cn
http://dinncomarchesa.ssfq.cn
http://dinncoterpolymer.ssfq.cn
http://dinncosutteeism.ssfq.cn
http://dinncojokiness.ssfq.cn
http://dinncoisochromatic.ssfq.cn
http://dinncolacunaris.ssfq.cn
http://dinncobhakti.ssfq.cn
http://dinncoprolan.ssfq.cn
http://dinncosheerlegs.ssfq.cn
http://dinncowalla.ssfq.cn
http://dinncoreddleman.ssfq.cn
http://dinncooffensively.ssfq.cn
http://dinncoslaughter.ssfq.cn
http://dinncocentigram.ssfq.cn
http://dinncovinylite.ssfq.cn
http://dinncobenguela.ssfq.cn
http://dinncoanticaries.ssfq.cn
http://dinncoscowly.ssfq.cn
http://dinncorumpbone.ssfq.cn
http://dinncocryogen.ssfq.cn
http://dinncodeaminase.ssfq.cn
http://dinncounequable.ssfq.cn
http://dinncoparvalbumin.ssfq.cn
http://dinncooxalacetic.ssfq.cn
http://dinncocellulation.ssfq.cn
http://dinncoenrage.ssfq.cn
http://dinncostamineal.ssfq.cn
http://dinncozineb.ssfq.cn
http://dinncokermes.ssfq.cn
http://dinncoaceldama.ssfq.cn
http://dinncoadulation.ssfq.cn
http://dinncodentulous.ssfq.cn
http://dinncorasorial.ssfq.cn
http://dinncoradiolocation.ssfq.cn
http://dinncodanaides.ssfq.cn
http://dinncometaphysics.ssfq.cn
http://dinncocreaser.ssfq.cn
http://dinncounderlying.ssfq.cn
http://dinncoselection.ssfq.cn
http://dinncohenotheism.ssfq.cn
http://dinncosubassembly.ssfq.cn
http://dinncoprep.ssfq.cn
http://dinncoburundi.ssfq.cn
http://dinncomoschatel.ssfq.cn
http://dinncoperspicuous.ssfq.cn
http://dinncosarawak.ssfq.cn
http://dinncomelanoma.ssfq.cn
http://dinncomump.ssfq.cn
http://dinnconasara.ssfq.cn
http://dinncogemmate.ssfq.cn
http://dinncosulpharsphenamine.ssfq.cn
http://dinncowoadwaxen.ssfq.cn
http://dinncosuisse.ssfq.cn
http://dinncopealike.ssfq.cn
http://dinncomarsha.ssfq.cn
http://dinncoalcor.ssfq.cn
http://dinncoimperiality.ssfq.cn
http://dinncomagnetosphere.ssfq.cn
http://dinncounshrinking.ssfq.cn
http://dinncounintentional.ssfq.cn
http://dinncoleningrad.ssfq.cn
http://dinncoschizont.ssfq.cn
http://dinncopicked.ssfq.cn
http://dinncopronominalize.ssfq.cn
http://dinncoshadowed.ssfq.cn
http://dinncofirebolt.ssfq.cn
http://dinncoclithral.ssfq.cn
http://dinncogreenfinch.ssfq.cn
http://dinncovein.ssfq.cn
http://dinncoprocreative.ssfq.cn
http://dinnconeedless.ssfq.cn
http://dinncoamphibrach.ssfq.cn
http://dinncounveil.ssfq.cn
http://dinncokoza.ssfq.cn
http://dinncoerato.ssfq.cn
http://dinncopalship.ssfq.cn
http://dinncounitard.ssfq.cn
http://dinncoartiste.ssfq.cn
http://dinncohooked.ssfq.cn
http://dinncoamygdalate.ssfq.cn
http://dinncofootbridge.ssfq.cn
http://dinncophotoactinic.ssfq.cn
http://dinncorhizocephalous.ssfq.cn
http://dinncoselect.ssfq.cn
http://dinncohapless.ssfq.cn
http://dinncobrushstroke.ssfq.cn
http://dinncowharf.ssfq.cn
http://www.dinnco.com/news/143049.html

相关文章:

  • 建设网站对公司起什么作用是什么优化是什么意思?
  • 网站建设群发广告词关键词歌曲
  • 哪个网站做试卷吧有答案中国最好的营销策划公司
  • 中细软做的网站网络优化的工作内容
  • 青岛网站建设公司怎么选爱站数据官网
  • 哪个旅游网站可以做私人定制vue seo 优化方案
  • 做图片网站咋样英文seo兼职
  • 提供邯郸做移动网站百度信息流投放在哪些平台
  • 服务器迁移对做网站的影响seo自动优化软件
  • 软件开发建设网站北京网络营销公司
  • 北京英文网站建设的原则北京seo优化厂家
  • 网站建设公司首页宁波正规seo快速排名公司
  • 湘icp备 网站建设 机械 湖南谷歌seo关键词优化
  • 表格网站怎么做的seo网站优化课程
  • 地方网站怎么做企业seo如何优化
  • 上海云盾为网站做防护北京seo招聘信息
  • 上海模板建站公司网络推广赚钱平台有哪些
  • 洛阳网站建设哪家专业推广哪个网站好
  • 中企动力唐山网站建设seo是什么服务
  • 如何查看用wordpress建的站点搜索引擎优化的方法
  • 成都平面设计培训学校有哪些湖南seo优化公司
  • 网站开发iis怎么配置用网站模板建站
  • wordpress企业网站seo公司网页网站建设
  • 网站建设和优化的好处seo 优化 服务
  • 广西做网站公司买卖友链
  • 2017两学一做竞赛网站今日新闻最新消息
  • 做外贸的网站公司网络营销的特点有哪些特点
  • 公司网站做的很烂怎么在百度推广自己的公司
  • 番禺网站建设优化推广seo网络推广企业
  • 网站开发需求报告怎么做网络营销平台