{"id":10006896,"date":"2022-11-22T17:28:51","date_gmt":"2022-11-22T09:28:51","guid":{"rendered":"https:\/\/ityet.com\/?p=10006896"},"modified":"2022-11-22T17:28:51","modified_gmt":"2022-11-22T09:28:51","slug":"%e5%9f%ba%e4%ba%8espring-aop%e7%9a%84%e8%87%aa%e5%ae%9a%e4%b9%89%e5%88%86%e7%89%87%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006896","title":{"rendered":"\u57fa\u4e8eSpring-AOP\u7684\u81ea\u5b9a\u4e49\u5206\u7247\u5de5\u5177"},"content":{"rendered":"<p><strong>\u4f5c\u8005\uff1a\u9648\u660c\u6d69<\/strong><\/p>\n<h3>1 \u80cc\u666f<\/h3>\n<p>\u968f\u7740\u6570\u636e\u91cf\u7684\u589e\u957f\uff0c\u53d1\u73b0\u7cfb\u7edf\u5728\u4e0e\u5176\u4ed6\u7cfb\u7edf\u4ea4\u4e92\u65f6\uff0c\u6279\u91cf\u63a5\u53e3\u4f1a\u51fa\u73b0\u8d85\u65f6\u73b0\u8c61\uff0c\u53d1\u73b0\u539f\u6279\u91cf\u63a5\u53e3\u5728\u5b9e\u73b0\u65f6\uff0c\u6ca1\u6709\u505a\u5206\u7247\u5904\u7406\uff0c\u5f53\u6570\u636e\u8fc7\u5927\u65f6\u6216\u8d85\u8fc7\u5176\u4ed6\u7cfb\u7edf\u9608\u503c\u65f6\uff0c\u5c31\u4f1a\u51fa\u73b0\u9519\u8bef\u3002\u7531\u4e8e\u4e0e\u5176\u4ed6\u7cfb\u7edf\u4ea4\u4e92\u6bd4\u8f83\u591a\uff0c\u4e00\u4e2a\u4e00\u4e2a\u63a5\u53e3\u505a\u5206\u7247\u4f18\u5316\uff0c\u6539\u52a8\u91cf\u8f83\u5927\uff0c\u6240\u4ee5\u8003\u8651\u901a\u8fc7AOP\u89e3\u51b3\u6b64\u95ee\u9898\u3002<\/p>\n<h3>2 Spring-AOP<\/h3>\n<p>AOP \uff08Aspect Orient Programming\uff09,\u76f4\u8bd1\u8fc7\u6765\u5c31\u662f \u9762\u5411\u5207\u9762\u7f16\u7a0b\u3002AOP \u662f\u4e00\u79cd\u7f16\u7a0b\u601d\u60f3\uff0c\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u7684\u4e00\u79cd\u8865\u5145\u3002\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u5c06\u7a0b\u5e8f\u62bd\u8c61\u6210\u5404\u4e2a\u5c42\u6b21\u7684\u5bf9\u8c61\uff0c\u800c\u9762\u5411\u5207\u9762\u7f16\u7a0b\u662f\u5c06\u7a0b\u5e8f\u62bd\u8c61\u6210\u5404\u4e2a\u5207\u9762\u3002<\/p>\n<p>Spring \u4e2d\u7684 AOP \u662f\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u7684\u3002 Spring AOP \u4e0d\u80fd\u62e6\u622a\u5bf9\u5bf9\u8c61\u5b57\u6bb5\u7684\u4fee\u6539\uff0c\u4e5f\u4e0d\u652f\u6301\u6784\u9020\u5668\u8fde\u63a5\u70b9,\u6211\u4eec\u65e0\u6cd5\u5728 Bean \u521b\u5efa\u65f6\u5e94\u7528\u901a\u77e5\u3002<\/p>\n<h3>3 \u529f\u80fd\u5b9e\u73b0<\/h3>\n<p>\u81ea\u5b9a\u4e49\u5206\u7247\u5904\u7406\u5206\u4e09\u4e2a\u90e8\u5206\uff1a\u81ea\u5b9a\u4e49\u6ce8\u89e3\uff08MethodPartAndRetryer\uff09\u3001\u91cd\u8bd5\u5668\uff08RetryUtil\uff09\u3001\u5207\u9762\u5b9e\u73b0(RetryAspectAop)\u3002<\/p>\n<h4>3.1 MethodPartAndRetryer<\/h4>\n<p>\u6e90\u7801<\/p>\n<pre><code>\n@Target(ElementType.METHOD)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\npublic @interface MethodPartAndRetryer {\n\/**\n* \u5931\u8d25\u91cd\u8bd5\u6b21\u6570\n* @return\n*\/\nint times() default 3;\n\/**\n* \u5931\u8d25\u95f4\u9694\u6267\u884c\u65f6\u95f4 300\u6beb\u79d2\n* @return\n*\/\nlong waitTime() default 300L;\n\/**\n* \u5206\u7247\u5927\u5c0f\n* @return\n*\/\nint parts() default 200;\n}\n<\/code><\/pre>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=U%2B42pVDwY%2FeE4AHOLIkNGw%3D%3D.kybXq%2BtZhDRWJp3erPeLRdK%2Bue7zzcGmhzhfru5WyEA%3D\" rel=\"nofollow\">@interface<\/a>\u8bf4\u660e\u8fd9\u4e2a\u7c7b\u662f\u4e2a\u6ce8\u89e3\u3002  <br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=N0wTW1VZK0OrhRjlzIEUjg%3D%3D.ayQQ16zPMZFaAMMiI67f%2B9Ld6TF%2Fhklp%2B%2B0DKX7BNpA%3D\" rel=\"nofollow\">@Target<\/a>\u662f\u8fd9\u4e2a\u6ce8\u89e3\u7684\u4f5c\u7528\u57df<\/p>\n<pre><code>\u00a0\npublic enum ElementType {\n\/** \u7c7b\u3001\u63a5\u53e3(\u5305\u62ec\u6ce8\u91ca\u7c7b\u578b)\u6216\u679a\u4e3e\u58f0\u660e *\/\nTYPE,\n\/** \u5b57\u6bb5\u58f0\u660e(\u5305\u62ec\u679a\u4e3e\u5e38\u91cf) *\/\nFIELD,\n\/** \u65b9\u6cd5\u58f0\u660e *\/\nMETHOD,\n\/** \u6b63\u5f0f\u7684\u53c2\u6570\u58f0\u660e *\/\nPARAMETER,\n\/** \u6784\u9020\u51fd\u6570\u58f0\u660e *\/\nCONSTRUCTOR,\n\/** \u5c40\u90e8\u53d8\u91cf\u58f0\u660e *\/\nLOCAL_VARIABLE,\n\/** \u6ce8\u91ca\u7c7b\u578b\u58f0\u660e*\/\nANNOTATION_TYPE,\n\/** \u7a0b\u5e8f\u5305\u58f0\u660e *\/\nPACKAGE,\n\/**\u7c7b\u578b\u53c2\u6570\u58f0\u660e*\/\nTYPE_PARAMETER,\n\/**\u7c7b\u578b\u7684\u4f7f\u7528*\/\nTYPE_USE\n}\n<\/code><\/pre>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=qDWxlOKlXMBNNs%2BmHMPKdw%3D%3D.fK0jBDL%2BrhSW9I0qV9XmSG2yX9czE5IaklJRR%2B9e6cM%3D\" rel=\"nofollow\">@Retention<\/a>\u6ce8\u89e3\u7684\u751f\u547d\u5468\u671f<\/p>\n<pre><code>public enum RetentionPolicy {\n\/** \u7f16\u8bd1\u5668\u5904\u7406\u5b8c\u540e\u4e0d\u5b58\u50a8\u5728class\u4e2d*\/\nSOURCE,\n\/**\u6ce8\u91ca\u5c06\u88ab\u7f16\u8bd1\u5668\u8bb0\u5f55\u5728\u7c7b\u6587\u4ef6\u4e2d\uff0c\u4f46\u4e0d\u9700\u8981\u5728\u8fd0\u884c\u65f6\u88abVM\u4fdd\u7559\u3002 \u8fd9\u662f\u9ed8\u8ba4\u503c*\/\nCLASS,\n\/**\u7f16\u8bd1\u5668\u5b58\u50a8\u5728class\u4e2d\uff0c\u53ef\u4ee5\u7531\u865a\u62df\u673a\u8bfb\u53d6*\/\nRUNTIME\n}\n<\/code><\/pre>\n<ul>\n<li>times()\uff1a\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u65f6\uff0c\u91cd\u8bd5\u7684\u6b21\u6570\u3002<\/li>\n<li>waitTime():\u63a5\u53e3\u8c03\u7528\u5931\u8d25\u662f\uff0c\u95f4\u9694\u591a\u957f\u65f6\u95f4\u518d\u6b21\u8c03\u7528\u3002<\/li>\n<li>int parts()\uff1a\u8fdb\u884c\u5206\u7247\u65f6\uff0c\u6bcf\u4e2a\u5206\u7247\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p>3.2 RetryUtil<\/p>\n<p>\u6e90\u7801<\/p>\n<pre><code>public class RetryUtil&lt;V&gt; {\n\npublic Retryer&lt;V&gt; getDefaultRetryer(int times,long waitTime) {\nRetryer&lt;V&gt; retryer = RetryerBuilder.&lt;V&gt;newBuilder()\n.retryIfException()\n.retryIfRuntimeException()\n.retryIfExceptionOfType(Exception.class)\n.withWaitStrategy(WaitStrategies.fixedWait(waitTime, TimeUnit.MILLISECONDS))\n.withStopStrategy(StopStrategies.stopAfterAttempt(times))\n.build();\nreturn retryer;\n}\n}\n<\/code><\/pre>\n<p>\u8bf4\u660e<\/p>\n<ul>\n<li>RetryerBuilder\uff1a\u662f\u7528\u4e8e\u914d\u7f6e\u548c\u521b\u5efaRetryer\u7684\u6784\u5efa\u5668\u3002<\/li>\n<li>retryIfException\uff1a\u629b\u51faruntime\u5f02\u5e38\u3001checked\u5f02\u5e38\u65f6\u90fd\u4f1a\u91cd\u8bd5\uff0c\u4f46\u662f\u629b\u51faerror\u4e0d\u4f1a\u91cd\u8bd5\u3002<\/li>\n<li>retryIfRuntimeException\uff1a\u53ea\u4f1a\u5728\u629bruntime\u5f02\u5e38\u7684\u65f6\u5019\u624d\u91cd\u8bd5\uff0cchecked\u5f02\u5e38\u548cerror\u90fd\u4e0d\u91cd\u8bd5\u3002<\/li>\n<li>retryIfExceptionOfType\uff1a\u5141\u8bb8\u6211\u4eec\u53ea\u5728\u53d1\u751f\u7279\u5b9a\u5f02\u5e38\u7684\u65f6\u5019\u624d\u91cd\u8bd5\u3002<\/li>\n<li>withWaitStrategy:\u7b49\u5f85\u7b56\u7565\uff0c\u6bcf\u6b21\u8bf7\u6c42\u95f4\u9694\u3002<\/li>\n<li>withStopStrategy:\u505c\u6b62\u7b56\u7565\uff0c\u91cd\u8bd5\u591a\u5c11\u6b21\u540e\u505c\u6b62\u3002<\/li>\n<\/ul>\n<h4>3.3 RetryAspectAop<\/h4>\n<p>\u6e90\u7801\uff1a<\/p>\n<pre><code>public class RetryAspectAop {\npublic Object around(final ProceedingJoinPoint point) throws Throwable {\nObject result = null;\nfinal Object[] args = point.getArgs();\nboolean isHandler1 = isHandler(args);\nif (isHandler1) {\nString className = point.getSignature().getDeclaringTypeName();\nString methodName = point.getSignature().getName();\nObject firstArg = args[0];\nList&lt;Object&gt; paramList = (List&lt;Object&gt;) firstArg;\n\/\/\u83b7\u53d6\u65b9\u6cd5\u4fe1\u606f\nMethod method = getCurrentMethod(point);\n\/\/\u83b7\u53d6\u6ce8\u89e3\u4fe1\u606f\nMethodPartAndRetryer retryable = AnnotationUtils.getAnnotation(method, MethodPartAndRetryer.class);\n\/\/\u91cd\u8bd5\u673a\u5236\nRetryer&lt;Object&gt; retryer = new RetryUtil&lt;Object&gt;().getDefaultRetryer(retryable.times(),retryable.waitTime());\n\/\/\u5206\u7247\nList&lt;List&lt;Object&gt;&gt; requestList = Lists.partition(paramList, retryable.parts());\nfor (List&lt;Object&gt; partList : requestList) {\nargs[0] = partList;\nObject tempResult = retryer.call(new Callable&lt;Object&gt;() {\n@Override\npublic Object call() throws Exception {\ntry {\nreturn point.proceed(args);\n} catch (Throwable throwable) {\nlog.error(String.format(&quot;\u5206\u7247\u91cd\u8bd5\u62a5\u9519,\u7c7b%s-\u65b9\u6cd5%s&quot;,className,methodName),throwable);\nthrow new RuntimeException(&quot;\u5206\u7247\u91cd\u8bd5\u51fa\u9519&quot;);\n}\n}\n});\nif (null != tempResult) {\nif (tempResult instanceof Boolean) {\nif (!((Boolean) tempResult)) {\nlog.error(String.format(&quot;\u5206\u7247\u6267\u884c\u62a5\u9519\u8fd4\u56de\u7c7b\u578b\u4e0d\u80fd\u8f6c\u5316bolean,\u7c7b%s-\u65b9\u6cd5%s&quot;,className,methodName));\nthrow new RuntimeException(&quot;\u5206\u7247\u6267\u884c\u62a5\u9519!&quot;);\n}\nresult = tempResult;\n} else if (tempResult instanceof List) {\nif(result ==null){\nresult =Lists.newArrayList();\n}\n((List) result).addAll((List) tempResult);\n}else {\nlog.error(String.format(&quot;\u5206\u7247\u6267\u884c\u8fd4\u56de\u7684\u7c7b\u578b\u4e0d\u652f\u6301,\u7c7b%s-\u65b9\u6cd5%s&quot;,className,methodName));\nthrow new RuntimeException(&quot;\u4e0d\u652f\u6301\u8be5\u8fd4\u56de\u7c7b\u578b&quot;);\n}\n} else {\nlog.error(String.format(&quot;\u5206\u7247\u6267\u884c\u8fd4\u56de\u7684\u7ed3\u679c\u4e3a\u7a7a,\u7c7b%s-\u65b9\u6cd5%s&quot;,className,methodName));\nthrow new RuntimeException(&quot;\u8c03\u7528\u7ed3\u679c\u4e3a\u7a7a&quot;);\n}\n}\n} else {\nresult = point.proceed(args);\n}\nreturn result;\n}\nprivate boolean isHandler(Object[] args) {\nboolean isHandler = false;\nif (null != args &amp;&amp; args.length &gt; 0) {\nObject firstArg = args[0];\n\/\/\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662flist \u5e76\u4e14\u6570\u91cf\u5927\u4e8e1\nif (firstArg!=null&amp;&amp;firstArg instanceof List &amp;&amp;((List) firstArg).size()&gt;1) {\nisHandler = true;\n}\n}\nreturn isHandler;\n}\nprivate Method getCurrentMethod(ProceedingJoinPoint point) {\ntry {\nSignature sig = point.getSignature();\nMethodSignature msig = (MethodSignature) sig;\nObject target = point.getTarget();\nreturn target.getClass().getMethod(msig.getName(), msig.getParameterTypes());\n} catch (NoSuchMethodException e) {\nthrow new RuntimeException(e);\n}\n}\n}\n<\/code><\/pre>\n<p>\u8bf4\u660e\uff1a<\/p>\n<p>getCurrentMethod\uff1a\u83b7\u53d6\u65b9\u6cd5\u4fe1\u606f\u5373\u8981\u505a\u5206\u7247\u7684\u6279\u91cf\u8c03\u7528\u7684\u63a5\u53e3\u3002  <br \/>isHandler1\uff1a\u5224\u65ad\u662f\u5426\u8981\u505a\u5206\u7247\u5904\u7406\uff0c\u53ea\u6709\u7b2c\u4e00\u53c2\u6570\u662flist\u5e76\u4e14list \u7684\u503c\u5927\u4e8e1\u65f6\u624d\u505a\u5206\u7247\u5904\u7406\u3002  <br \/>around\uff1a\u5177\u4f53\u5206\u7247\u903b\u8f91\u3002<\/p>\n<ol>\n<li>\u83b7\u53d6\u8981\u5206\u7247\u65b9\u6cd5\u7684\u53c2\u6570\u3002<\/li>\n<li>\u5224\u65ad\u662f\u5426\u8981\u505a\u5206\u7247\u5904\u7406\u3002<\/li>\n<li>\u83b7\u53d6\u65b9\u6cd5\u3002<\/li>\n<li>\u83b7\u53d6\u91cd\u8bd5\u6b21\u6570\u3001\u91cd\u8bd5\u95f4\u9694\u65f6\u95f4\u548c\u5206\u7247\u5927\u5c0f\u3002<\/li>\n<li>\u751f\u6210\u91cd\u8bd5\u5668\u3002<\/li>\n<li>\u6839\u636e\u8bbe\u7f6e\u7684\u5206\u7247\u5927\u5c0f\uff0c\u505a\u5206\u7247\u5904\u7406\u3002<\/li>\n<li>\u8c03\u7528\u6279\u91cf\u63a5\u53e3\u5e76\u5904\u7406\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<h3>4 \u529f\u80fd\u4f7f\u7528<\/h3>\n<h4>4.1 \u914d\u7f6e\u6587\u4ef6<\/h4>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042859430\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>4.2 \u4ee3\u7801\u793a\u4f8b<\/h4>\n<pre><code>@MethodPartAndRetryer(parts=100)\npublic Boolean writeBackOfGoodsSN(List&lt;SerialDTO&gt; listSerial,ObCheckWorker workerData)\n<\/code><\/pre>\n<p>\u53ea\u8981\u5728\u9700\u8981\u505a\u5206\u7247\u7684\u6279\u91cf\u63a5\u53e3\u65b9\u6cd5\u4e0a\uff0c\u52a0\u4e0aMethodPartAndRetryer\u6ce8\u89e3\u5c31\u53ef\u4ee5\uff0c\u91cd\u8bd5\u6b21\u6570\u3001\u91cd\u8bd5\u95f4\u9694\u65f6\u95f4\u548c\u5206\u7247\u5927\u5c0f\u53ef\u4ee5\u5728\u6ce8\u89e3\u65f6\u8bbe\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<h3>5 \u5c0f\u7ed3<\/h3>\n<p>\u901a\u8fc7\u81ea\u5b9a\u4e49\u5206\u7247\u5de5\u5177\uff0c\u53ef\u4ee5\u5feb\u901f\u7684\u5bf9\u8001\u4ee3\u7801\u8fdb\u884c\u5206\u7247\u5904\u7406\uff0c\u800c\u4e14\u589e\u52a0\u4e86\u91cd\u8bd5\u673a\u5236\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u7684\u53ef\u7528\u6027\uff0c\u63d0\u9ad8\u4e86\u5bf9\u8001\u4ee3\u7801\u7684\u91cd\u6784\u6548\u7387\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042859428\">https:\/\/segmentfault.com\/a\/1190000042859428<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u9648\u660c\u6d69 1 \u80cc\u666f \u968f\u7740\u6570\u636e\u91cf\u7684\u589e\u957f\uff0c\u53d1\u73b0\u7cfb\u7edf\u5728\u4e0e\u5176\u4ed6\u7cfb\u7edf\u4ea4\u4e92\u65f6\uff0c\u6279\u91cf\u63a5\u53e3\u4f1a\u51fa\u73b0\u8d85\u65f6\u73b0\u8c61\uff0c\u53d1\u73b0\u539f\u6279\u91cf\u63a5\u53e3\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006896","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006896"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006896\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}