programming-jiexi.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 2.0.15">
  8. <meta name="author" content="pxzxj, pudge.zxj@gmail.com, 2023/02/23">
  9. <title>编程中的解析(Parse vs Resolve vs Analyze)</title>
  10. <link rel="stylesheet" href="css/site.css">
  11. <link href="css/custom.css" rel="stylesheet">
  12. <script src="js/setup.js"></script><script defer src="js/site.js"></script>
  13. </head>
  14. <body class="article toc2 toc-left"><div id="banner-container" class="container" role="banner">
  15. <div id="banner" class="contained" role="banner">
  16. <div id="switch-theme">
  17. <input type="checkbox" id="switch-theme-checkbox" />
  18. <label for="switch-theme-checkbox">Dark Theme</label>
  19. </div>
  20. </div>
  21. </div>
  22. <div id="tocbar-container" class="container" role="navigation">
  23. <div id="tocbar" class="contained" role="navigation">
  24. <button id="toggle-toc"></button>
  25. </div>
  26. </div>
  27. <div id="main-container" class="container">
  28. <div id="main" class="contained">
  29. <div id="doc" class="doc">
  30. <div id="header">
  31. <h1>编程中的解析(Parse vs Resolve vs Analyze)</h1>
  32. <div class="details">
  33. <span id="author" class="author">pxzxj</span><br>
  34. <span id="author2" class="author">pudge.zxj@gmail.com</span><br>
  35. <span id="author3" class="author">2023/02/23</span><br>
  36. </div>
  37. <div id="toc" class="toc2">
  38. <div id="toctitle">Table of Contents</div>
  39. <span id="back-to-index"><a href="index.html">Back to index</a></span><ul class="sectlevel1">
  40. <li><a href="#_parse">1. Parse</a></li>
  41. <li><a href="#_resolve">2. Resolve</a></li>
  42. <li><a href="#_analyze">3. Analyze</a></li>
  43. </ul>
  44. </div>
  45. </div>
  46. <div id="content">
  47. <div id="preamble">
  48. <div class="sectionbody">
  49. <div class="paragraph">
  50. <p><code>解析</code> 是软件开发中的一个高频词汇,但在英文中表达 <code>解析</code> 语义的单词却很多,本文结合Spring Framework源码对比常用的三个表达解析的单词深层的不同以帮助开发者做出正确选择。</p>
  51. </div>
  52. <div class="imageblock">
  53. <div class="content">
  54. <img src="images/jiexi.png" alt="jiexi">
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. <div class="sect1">
  60. <h2 id="_parse"><a class="anchor" href="#_parse"></a>1. Parse</h2>
  61. <div class="sectionbody">
  62. <div class="quoteblock">
  63. <blockquote>
  64. <div class="paragraph">
  65. <p>to separate a sentence into grammatical parts, such as subject, verb, etc.</p>
  66. </div>
  67. </blockquote>
  68. <div class="attribution">
  69. &#8212; dictionary.cambridge.org
  70. </div>
  71. </div>
  72. <div class="paragraph">
  73. <p>Spring Framework中 <code>Parse</code> 相关的有如下接口</p>
  74. </div>
  75. <div class="listingblock">
  76. <div class="title">org.springframework.format.Parser</div>
  77. <div class="content">
  78. <pre class="highlight"><code class="language-java" data-lang="java">@FunctionalInterface
  79. public interface Parser&lt;T&gt; {
  80. /**
  81. * 将文本根据不同语系解析为T的实例
  82. */
  83. T parse(String text, Locale locale) throws ParseException;
  84. }
  85. </code></pre>
  86. </div>
  87. </div>
  88. <div class="listingblock">
  89. <div class="title">org.springframework.expression.ExpressionParser</div>
  90. <div class="content">
  91. <pre class="highlight"><code class="language-java" data-lang="java">public interface ExpressionParser {
  92. /**
  93. * 将表达式解析为可执行的Expression对象
  94. */
  95. Expression parseExpression(String expressionString) throws ParseException;
  96. }
  97. </code></pre>
  98. </div>
  99. </div>
  100. <div class="listingblock">
  101. <div class="title">org.springframework.beans.factory.xml.BeanDefinitionParser</div>
  102. <div class="content">
  103. <pre class="highlight"><code class="language-java" data-lang="java">public interface BeanDefinitionParser {
  104. /**
  105. * 将Xml元素解析为BeanDefinition对象
  106. */
  107. @Nullable
  108. BeanDefinition parse(Element element, ParserContext parserContext);
  109. }
  110. </code></pre>
  111. </div>
  112. </div>
  113. <div class="paragraph">
  114. <p>综合 <code>Parse</code> 的定义与Spring Framework中的实践可以将它的使用场景归纳为:<code>Parse</code> 用于内容已知但需要对它的格式、表现形式或存储方式做一定转换并且转换过程中可能需要对内容按照特定语法做拆分的场景,例如Xml解析、Json解析、Excel解析都应该使用 <code>Parse</code> 表示</p>
  115. </div>
  116. </div>
  117. </div>
  118. <div class="sect1">
  119. <h2 id="_resolve"><a class="anchor" href="#_resolve"></a>2. Resolve</h2>
  120. <div class="sectionbody">
  121. <div class="quoteblock">
  122. <blockquote>
  123. <div class="paragraph">
  124. <p>to solve or end a problem or difficulty</p>
  125. </div>
  126. </blockquote>
  127. <div class="attribution">
  128. &#8212; dictionary.cambridge.org
  129. </div>
  130. </div>
  131. <div class="paragraph">
  132. <p>Spring Framework中 <code>Resolve</code> 相关的有如下接口</p>
  133. </div>
  134. <div class="listingblock">
  135. <div class="title">org.springframework.expression.BeanResolver</div>
  136. <div class="content">
  137. <pre class="highlight"><code class="language-java" data-lang="java">public interface BeanResolver {
  138. /**
  139. * 根据Bean的名称查找并返回对应的Bean实例
  140. */
  141. Object resolve(EvaluationContext context, String beanName) throws AccessException;
  142. }
  143. </code></pre>
  144. </div>
  145. </div>
  146. <div class="listingblock">
  147. <div class="title">org.springframework.web.servlet.LocaleResolver</div>
  148. <div class="content">
  149. <pre class="highlight"><code class="language-java" data-lang="java">public interface LocaleResolver {
  150. /**
  151. * 从请求中解析Locale
  152. */
  153. Locale resolveLocale(HttpServletRequest request);
  154. }
  155. </code></pre>
  156. </div>
  157. </div>
  158. <div class="listingblock">
  159. <div class="title">org.springframework.web.servlet.HandlerExceptionResolver</div>
  160. <div class="content">
  161. <pre class="highlight"><code class="language-java" data-lang="java">public interface HandlerExceptionResolver {
  162. /**
  163. * SpringMVC执行过程中将异常解析为错误页面
  164. */
  165. @Nullable
  166. ModelAndView resolveException(
  167. HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex);
  168. }
  169. </code></pre>
  170. </div>
  171. </div>
  172. <div class="paragraph">
  173. <p>综合 <code>Resolve</code> 的定义与Spring Framework中的实践可以将它的使用场景归纳为:需要对原始内容做一定的业务处理和计算后生成与原始内容相关但完全不同的目标内容的场景,例如DNS解析需要访问DNS服务器查询域名对应的IP地址,域名与IP是相关的但它们的内容却完全不同,因此需要使用 <code>Resolve</code> 表示,甚至Linux的DNS服务器配置文件名也是 <code>resolv.conf</code></p>
  174. </div>
  175. <div class="admonitionblock tip">
  176. <table>
  177. <tr>
  178. <td class="icon">
  179. <i class="fa icon-tip" title="Tip"></i>
  180. </td>
  181. <td class="content">
  182. <code>Resolve</code> 虽然可以翻译为解析,但它的重点在于能够以某种方式使用原始内容解决问题,这一点在它的英文释义中也有体现
  183. </td>
  184. </tr>
  185. </table>
  186. </div>
  187. </div>
  188. </div>
  189. <div class="sect1">
  190. <h2 id="_analyze"><a class="anchor" href="#_analyze"></a>3. Analyze</h2>
  191. <div class="sectionbody">
  192. <div class="quoteblock">
  193. <blockquote>
  194. <div class="paragraph">
  195. <p>to study or examine something in detail, in order to discover more about it</p>
  196. </div>
  197. </blockquote>
  198. <div class="attribution">
  199. &#8212; dictionary.cambridge.org
  200. </div>
  201. </div>
  202. <div class="paragraph">
  203. <p>Spring Boot中 <code>Analyze</code> 相关的有如下接口</p>
  204. </div>
  205. <div class="listingblock">
  206. <div class="title">org.springframework.boot.diagnostics.FailureAnalyzer</div>
  207. <div class="content">
  208. <pre class="highlight"><code class="language-java" data-lang="java">@FunctionalInterface
  209. public interface FailureAnalyzer {
  210. /**
  211. * Returns an analysis of the given {@code failure}, or {@code null} if no analysis
  212. * was possible.
  213. * @param failure the failure
  214. * @return the analysis or {@code null}
  215. */
  216. FailureAnalysis analyze(Throwable failure);
  217. }
  218. </code></pre>
  219. </div>
  220. </div>
  221. <div class="paragraph">
  222. <p>综合 <code>Analyze</code> 的定义与Spring Boot中的实践可以将它的使用场景归纳为:通过对原始内容做深层研究和剖析生成更有价值或更直观的目标内容的场景,例如上面的 <code>FailureAnalyzer</code> 通过分析异常返回失败原因</p>
  223. </div>
  224. <div class="admonitionblock tip">
  225. <table>
  226. <tr>
  227. <td class="icon">
  228. <i class="fa icon-tip" title="Tip"></i>
  229. </td>
  230. <td class="content">
  231. <code>Resolve</code> 和 <code>Analyze</code> 的主要区别在于 <code>Analyze</code> 强调对原始内容本身的深层分析产生结果,而 <code>Resolve</code> 则是使用原始内容进行业务逻辑处理后产生结果
  232. </td>
  233. </tr>
  234. </table>
  235. </div>
  236. </div>
  237. </div>
  238. </div>
  239. <div id="footer">
  240. <div id="footer-text">
  241. Last updated 2024-03-18 05:44:42 UTC
  242. </div>
  243. </div>
  244. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
  245. <script>
  246. if (!hljs.initHighlighting.called) {
  247. hljs.initHighlighting.called = true
  248. ;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
  249. }
  250. </script>
  251. <script src="https://utteranc.es/client.js"
  252. repo="pxzxj/articles"
  253. issue-term="title"
  254. label="utteranc"
  255. theme="github-light"
  256. crossorigin="anonymous"
  257. async>
  258. </script>
  259. </div>
  260. </div>
  261. </div>
  262. </body>
  263. </html>