{"id":201690,"date":"2025-05-10T09:55:18","date_gmt":"2025-05-10T01:55:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201690\/"},"modified":"2025-05-10T09:55:18","modified_gmt":"2025-05-10T01:55:18","slug":"%e6%80%8e%e6%a0%b7%e7%bb%93%e5%90%88aop%e5%92%8credis%e5%9c%a8springboot%e4%b8%ad%e9%98%b2%e6%ad%a2%e8%a1%a8%e5%8d%95%e9%87%8d%e5%a4%8d%e6%8f%90%e4%ba%a4%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201690\/","title":{"rendered":"\u600e\u6837\u7ed3\u5408AOP\u548cRedis\u5728SpringBoot\u4e2d\u9632\u6b62\u8868\u5355\u91cd\u590d\u63d0\u4ea4\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u600e\u6837\u7ed3\u5408AOP\u548cRedis\u5728SpringBoot\u4e2d\u9632\u6b62\u8868\u5355\u91cd\u590d\u63d0\u4ea4\uff1f<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;font-family:\">\u300a\u600e\u6837\u7ed3\u5408AOP\u548cRedis\u5728SpringBoot\u4e2d\u9632\u6b62\u8868\u5355\u91cd\u590d\u63d0\u4ea4\uff1f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;font-family:\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<ul>\n<ul><\/ul>\n<\/ul>\n<h3>\u914d\u7f6eRedis<\/h3>\n<h4>1. \u6dfb\u52a0Redis\u4f9d\u8d56<\/h4>\n<pre>\n   \n    \n&nbsp;&nbsp;&nbsp;&nbsp;\n    \n     org.springframework.boot\n    \n&nbsp;&nbsp;&nbsp;&nbsp;\n    \n     spring-boot-starter-data-redis\n    \n   <\/pre>\n<h4>2. \u6dfb\u52a0redis\u914d\u7f6e\u4fe1\u606f<\/h4>\n<pre>redis:\n&nbsp;&nbsp;host:&nbsp;127.0.0.1\n&nbsp;&nbsp;port:&nbsp;6379\n&nbsp;&nbsp;database:&nbsp;0\n&nbsp;&nbsp;password:\n&nbsp;&nbsp;#&nbsp;\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\n&nbsp;&nbsp;timeout:&nbsp;10s<\/pre>\n<h3>\u914d\u7f6eAOP<\/h3>\n<h4>1. \u81ea\u5b9a\u4e49\u6ce8\u89e3<\/h4>\n<pre>\/**\n&nbsp;*&nbsp;\u9632\u6b62\u8868\u5355\u91cd\u590d\u63d0\u4ea4\u6ce8\u89e3\n&nbsp;*\/\n@Target(ElementType.METHOD)&nbsp;\/\/&nbsp;\u6ce8\u89e3\u7684\u4f5c\u7528\u76ee\u6807\u4e3a\u65b9\u6cd5\n@Retention(RetentionPolicy.RUNTIME)&nbsp;\/\/&nbsp;\u6ce8\u89e3\u7684\u4fdd\u7559\u671f\u9650\u4e3a\u8fd0\u884c\u65f6\npublic&nbsp;@interface&nbsp;PreventDuplicateSubmission&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u65f6\u95f4\uff08s\uff09\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;time()&nbsp;default&nbsp;3;\n}<\/pre>\n<h4>2. AOP\u5207\u9762<\/h4>\n<pre>@Aspect&nbsp;\/\/&nbsp;\u8868\u660e\u8fd9\u662f\u4e00\u4e2a\u5207\u9762\u7c7b\n@Component&nbsp;\/\/&nbsp;\u8868\u793a\u8fd9\u662f\u4e00\u4e2aBean\npublic&nbsp;class&nbsp;DuplicateSubmissionAspect&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;@Autowired\n&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;StringRedisTemplate&nbsp;stringRedisTemplate;\n&nbsp;&nbsp;&nbsp;&nbsp;\n\/\/&nbsp;\u5b9a\u4e49\u5207\u5165\u70b9\uff0c\u5373\u6807\u6ce8\u4e86@PreventDuplicateSubmission\u6ce8\u89e3\u7684\u65b9\u6cd5\n&nbsp;&nbsp;&nbsp;&nbsp;@Pointcut(\"@annotation(com.example.demo.annotation.PreventDuplicateSubmission)\")\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;preventDuplicateSubmission()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;@Around(\"preventDuplicateSubmission()\")\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;Object&nbsp;around(ProceedingJoinPoint&nbsp;joinPoint)&nbsp;throws&nbsp;Throwable&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ServletRequestAttributes&nbsp;attributes&nbsp;=&nbsp;(ServletRequestAttributes)&nbsp;RequestContextHolder.getRequestAttributes();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert&nbsp;attributes&nbsp;!=&nbsp;null;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletRequest&nbsp;request&nbsp;=&nbsp;attributes.getRequest();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;requestURI&nbsp;=&nbsp;request.getRequestURI();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;key&nbsp;=&nbsp;requestURI&nbsp;+&nbsp;\":\"&nbsp;+&nbsp;JSON.toJSONString(request.getParameterMap());\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(stringRedisTemplate.hasKey(key))&nbsp;{&nbsp;\/\/&nbsp;\u5982\u679cRedis\u4e2d\u5df2\u5b58\u5728\u8be5\u8bf7\u6c42\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;RuntimeException(\"\u8bf7\u52ff\u91cd\u590d\u63d0\u4ea4\");\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u83b7\u53d6\u6ce8\u89e3\u7684\u53c2\u6570\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PreventDuplicateSubmission&nbsp;formSubmission&nbsp;=&nbsp;((MethodSignature)&nbsp;pjp.getSignature()).getMethod().getAnnotation(PreventDuplicateSubmission.class);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;time&nbsp;=&nbsp;formSubmission.time();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8bbe\u7f6e\u8bf7\u6c42\u7684key\u548cvalue\uff0c\u6709\u6548\u671f\u4e3a3\u79d2\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stringRedisTemplate.opsForValue().set(key,&nbsp;\"1\",&nbsp;time,&nbsp;TimeUnit.SECONDS);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;pjp.proceed();\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Spring Boot\u63d0\u4f9b\u7684<code>StringRedisTemplate<\/code>\u6765\u8fde\u63a5Redis\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7@Autowired\u6ce8\u89e3\u6765\u6ce8\u5165\u8be5\u5bf9\u8c61\u3002\u5728@Around\u6ce8\u89e3\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528stringRedisTemplate.hasKey()\u65b9\u6cd5\u6765\u68c0\u67e5Redis\u4e2d\u662f\u5426\u5df2\u5b58\u5728\u8be5\u8bf7\u6c42\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u4f7f\u7528stringRedisTemplate.opsForValue().set()\u65b9\u6cd5\u5c06\u8be5\u8bf7\u6c42\u5b58\u50a8\u5230Redis\u4e2d\uff0c\u540c\u65f6\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\u4e3a3\u79d2\u3002<\/p>\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<p>\u4f7f\u7528Redis\u5b58\u50a8\u8bf7\u6c42\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\n<p>Redis\u9700\u8981\u5355\u72ec\u90e8\u7f72\uff0c\u4e0d\u8981\u5c06Redis\u548c\u5e94\u7528\u7a0b\u5e8f\u90e8\u7f72\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>Redis\u7684\u6027\u80fd\u76f8\u5bf9\u4e8e\u5185\u5b58\u5b58\u50a8\u65b9\u5f0f\u53ef\u80fd\u4f1a\u6709\u6240\u4e0b\u964d\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u6d4b\u8bd5\u548c\u4f18\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679cRedis\u4e2d\u51fa\u73b0\u5f02\u5e38\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u9700\u8981\u589e\u52a0\u76f8\u5e94\u7684\u5bb9\u9519\u673a\u5236\u3002<\/p>\n<\/li>\n<li>\n<p>Redis\u5b58\u50a8\u8bf7\u6c42\u9700\u8981\u8003\u8651\u5230\u5e76\u53d1\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528Redis\u7684\u5206\u5e03\u5f0f\u9501\u6765\u89e3\u51b3\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u4e2d\u9700\u8981\u9891\u7e41\u5730\u8fdb\u884cRedis\u64cd\u4f5c\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f3Redis\u7684\u6027\u80fd\u4e0b\u964d\uff0c\u56e0\u6b64\u9700\u8981\u6ce8\u610f\u4f18\u5316Redis\u7684\u914d\u7f6e\u548c\u4f7f\u7528\u65b9\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528Redis Pipeline\u7b49\u6280\u672f\u6765\u63d0\u9ad8Redis\u7684\u6027\u80fd\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u600e\u6837\u7ed3\u5408AOP\u548cRedis\u5728Sp&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201690","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201690"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201690\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}