{"id":1298,"date":"2015-09-03T09:41:38","date_gmt":"2015-09-03T01:41:38","guid":{"rendered":"http:\/\/coderbee.net\/?p=1298"},"modified":"2015-09-03T09:41:59","modified_gmt":"2015-09-03T01:41:59","slug":"spring-mvc-%e4%b8%8e%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20150903\/1298","title":{"rendered":"Spring MVC \u4e0e\u7ebf\u7a0b"},"content":{"rendered":"<p>\u6700\u8fd1\u53c8\u88ab\u4e00\u4e2a\u8fde\u73af\u5751\u5751\u60e8\u4e86\uff0c\u6211\u8e29\u7684\u5751\u662f\u4e0e Spring MVC \u7684\u7ebf\u7a0b\u6a21\u578b\u6709\u5173\u7684\u3002<\/p>\n<p>\u6211\u4eec\u7684\u7cfb\u7edf\u73b0\u5728\u6709\u4e24\u4e2a war \u5305\uff0c\u5206\u522b\u90e8\u7f72\u5728\u4e0d\u540c\u7684 JBoss \u4e0a\uff0c\u7b2c\u4e00\u4e2a\u662f\u4e92\u8054\u7f51\u53ef\u8bbf\u95ee\u7684\uff0c\u53eb front \u5427\uff0c\u7b2c\u4e8c\u4e2a\u662f\u5728\u9632\u706b\u5899\u4e4b\u540e\u7684\uff0c\u53ea\u80fd\u901a\u8fc7 front \u6765\u8bbf\u95ee\uff0c\u53eb backend\uff0c\u662f\u53ef\u4ee5\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u3002<\/p>\n<p>front \u901a\u8fc7 Hessian \u8c03\u7528 backend \u7684\u670d\u52a1\u3002<\/p>\n<p>\u73b0\u5728\u6709\u4e2a\u9700\u6c42\u8981\u6c42 backend \u8bb0\u5f55\u4e92\u8054\u7f51\u7528\u6237\u7684 IP \u548c\u5176\u4ed6\u4e00\u4e9b\u53c2\u6570\uff0c\u63a5\u53e3\u592a\u591a\uff0c\u4e0d\u53ef\u80fd\u6bcf\u4e2a\u65b9\u6cd5\u518d\u6dfb\u52a0\u53c2\u6570\uff0c\u7531\u4e8e Hessian \u4e5f\u8fd0\u884c\u662f\u5728 HTTP \u4e0a\u7684\uff0c\u6240\u4ee5\u6211\u60f3\u5728 Hessian \u7684 HTTP \u8bf7\u6c42\u5934\u91cc\u628a\u8fd9\u4e9b\u9700\u8981\u7684\u53c2\u6570\u4f20\u8fc7\u53bb\u3002<\/p>\n<p>\u6839\u636e debug\uff0cHessian \u91c7\u7528\u7684\u662f JDK \u7684 <code>HessianURLConnectionFactory<\/code> \u6765\u521b\u5efa\u8fde\u63a5\uff0c\u5904\u7406 HTTP \u8bf7\u6c42\uff0c\u6240\u4ee5\u6211\u5c31\u6269\u5c55\u4e86\u5b83\u7684\u65b9\u6cd5 <code>public HessianConnection open(URL url) throws IOException<\/code> \uff0c\u5728\u91cc\u9762\u628a\u53c2\u6570\u653e\u5230\u8bf7\u6c42\u5934\u91cc\u3002\u7531\u4e8e\u8981\u53d6\u7684\u662f\u5ba2\u6237\u7aef\u5b9e\u9645 IP \u7b49\u4fe1\u606f\uff0c\u6240\u4ee5\u8fd8\u9700\u8981\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42 <code>HttpServletRequest<\/code> \uff0c\u521a\u597d Spring MVC \u91cc\u9762\u6709\u4e2a <code>RequestContextHolder<\/code> \u5de5\u5177\uff0c\u6301\u6709\u4e86  <code>HttpServletRequest<\/code> \u3002\u7136\u540e\u5c31\u6709\u4e86\u4e0b\u9762\u8fd9\u884c\u4ee3\u7801\uff1a<code>HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();<\/code> \u3002\u5751\u5c31\u6316\u6210\u4e86\u3002<\/p>\n<p>\u70b9\u5f00 <code>RequestContextHolder.getRequestAttributes()<\/code> \u65b9\u6cd5\u6765\u770b\u770b\uff1a<\/p>\n<pre><code class=\"java\">public static RequestAttributes getRequestAttributes() {\n     RequestAttributes attributes = requestAttributesHolder.get();\n     if (attributes == null) {\n          attributes = inheritableRequestAttributesHolder.get();\n     }\n     return attributes;\n}\n<\/code><\/pre>\n<p><code>requestAttributesHolder<\/code> \u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code class=\"java\">private static final ThreadLocal&lt;RequestAttributes&gt; requestAttributesHolder =\n          new NamedThreadLocal&lt;RequestAttributes&gt;(\"Request attributes\");\n<\/code><\/pre>\n<p><code>NamedThreadLocal<\/code>  \u662f\u8fd9\u6837\u5b9a\u4e49\u7684\uff1a <code>public class NamedThreadLocal&lt;T&gt; extends ThreadLocal&lt;T&gt; {<\/code> \u3002<br \/>\n\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e2a\u8bf7\u6c42\u662f\u8ddf\u7ebf\u7a0b\u7ed1\u5b9a\u7684\uff0c\u5728\u522b\u7684\u7ebf\u7a0b\u662f\u83b7\u53d6\u4e0d\u5230\u7684\u3002<\/p>\n<p>\u5176\u4ed6\u7684\u5751\u662f\u8fd9\u6837\u7684\uff1a<br \/>\n\u5728 front \u6709\u4e2a\u5730\u65b9\u7528\u4e86\u5f02\u6b65\u53bb\u8c03\u7528 backend\uff0c\u5176\u5b9e\u662f\u6839\u672c\u6ca1\u5fc5\u8981\u7528\u5f02\u6b65\u7684\uff08\u56e0\u4e3a\u5f53\u524d\u7ebf\u7a0b\u53d1\u51fa\u8bf7\u6c42\u540e\u662f\u5728\u7b49\u5f85\u54cd\u5e94\u7684\uff09\uff1b\u800c\u8fd9\u4e2a\u5f02\u6b65\u8c03\u7528\u5728\u6d4b\u8bd5\u73af\u5883\u65f6\u6ca1\u6709\u542f\u7528\uff0c\u88ab\u5f00\u5173\u5207\u53bb\u8d70\u53e6\u4e00\u4e2a\u540c\u6b65\u7684\u5206\u652f\uff0c\u800c\u751f\u4ea7\u73af\u5883\u786e\u662f\u5fc5\u987b\u8d70\u8fd9\u4e2a\u5f02\u6b65\u5206\u652f\u7684\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u53c8\u88ab\u4e00\u4e2a\u8fde\u73af\u5751\u5751\u60e8\u4e86\uff0c\u6211\u8e29\u7684\u5751\u662f\u4e0e Spring MVC \u7684\u7ebf\u7a0b\u6a21\u578b\u6709\u5173\u7684\u3002 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20150903\/1298\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[238,239],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1298"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1298"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1298\/revisions"}],"predecessor-version":[{"id":1300,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1298\/revisions\/1300"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}