{"id":257,"date":"2021-05-09T23:58:40","date_gmt":"2021-05-09T15:58:40","guid":{"rendered":"https:\/\/explause.com\/?p=257"},"modified":"2022-07-05T04:18:38","modified_gmt":"2022-07-04T20:18:38","slug":"spring-aop%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/devdoge.org\/?p=257","title":{"rendered":"Spring AOP\u7f16\u7a0b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u9762\u5411\u5207\u9762\u7f16\u7a0b\u7684\u7f29\u5199\u3002\u5e38\u7528\u4e8e\u65e5\u5fd7\u3001\u5ba1\u8ba1\u3001\u58f0\u660e\u5f0f\u4e8b\u52a1\u3001\u5b89\u5168\u548c\u7f13\u5b58\u7b49\u65b9\u9762\uff0c\u751a\u81f3\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u65b0\u903b\u8f91\uff0c\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AOP\u672f\u8bed\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes\"><table><tbody><tr><td><strong>Aspect<\/strong><\/td><td>\u5207\u9762<\/td><\/tr><tr><td><strong>Join point<\/strong><\/td><td>\u7a0b\u5e8f\u6267\u884c\u65f6\u7684\u70b9\uff0c\u901a\u5e38\u662f\u65b9\u6cd5<\/td><\/tr><tr><td><strong>Advice<\/strong><\/td><td>\u65b9\u6cd5\u6267\u884c\u524d\/\u540e\u7684\u52a8\u4f5c<\/td><\/tr><tr><td><strong>Pointcut<\/strong><\/td><td>advice\u6267\u884c\u7684\u5207\u5165\u70b9<\/td><\/tr><tr><td><strong>Introduction<\/strong><\/td><td>\u4e3a\u5df2\u5b58\u5728\u7684\u7c7b\u6dfb\u52a0\u65b9\u6cd5\u6216\u5c5e\u6027<\/td><\/tr><tr><td><strong>Target object<\/strong><\/td><td>\u5207\u9762\u4f5c\u7528\u7684\u5bf9\u8c61<\/td><\/tr><tr><td><strong>AOP proxy<\/strong><\/td><td>AOP\u6846\u67b6\u521b\u5efa\u7684\u7528\u4e8e\u5b8c\u6210\u5207\u9762\u52a8\u4f5c\u7684\u5bf9\u8c61<\/td><\/tr><tr><td><strong>Weaving<\/strong><\/td><td>\u8fde\u63a5\u5e94\u7528\u5bf9\u8c61\u4e0e\u5207\u9762\u7684\u8fc7\u7a0b<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Advice\u7c7b\u578b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes\"><table><tbody><tr><td><strong>before<\/strong><\/td><td>\u65b9\u6cd5\u6267\u884c\u524d\u8fd0\u884cadvice<\/td><\/tr><tr><td><strong>after<\/strong><\/td><td>\u65b9\u6cd5\u6267\u884c\u540e\u8fd0\u884cadvice<\/td><\/tr><tr><td><strong>after-returning<\/strong><\/td><td>\u65b9\u6cd5\u6267\u884c\u5b8c\u5168\u6210\u529f\u540e\u8fd0\u884cadvice<\/td><\/tr><tr><td><strong>after-throwing<\/strong><\/td><td>\u65b9\u6cd5\u6267\u884c\u51fa\u73b0\u5f02\u5e38\u5e76\u9000\u51fa\u540e\u8fd0\u884cadvice<\/td><\/tr><tr><td><strong>around<\/strong><\/td><td>\u8fd0\u884c\u65b9\u6cd5\u524d\u548c\u540e\u90fd\u6267\u884cadvice<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u9009\u62e9\u57fa\u4e8exml\u548c\u57fa\u4e8e\u6ce8\u89e3\u65b9\u5f0f\u7684\u914d\u7f6e\uff0c\u4f46\u662f\u73b0\u5728\u7684Spring Boot\/Cloud\u90fd\u662f\u7528Java\u914d\u7f6e\u4e86\uff0c\u6240\u4ee5\u8fd8\u662f\u7528\u6ce8\u89e3\u5427\uff0cxml\u6587\u4ef6\u8981\u5199\u4e00\u5927\u5806\u4e1c\u897f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/explause.com\/?p=221\">\u521b\u5efa\u4e00\u4e2a\u7a7a\u9879\u76ee<\/a>\u6216\u8005<a href=\"https:\/\/explause.com\/wp-content\/uploads\/2022\/07\/AOPExamples.zip\">\u4e0b\u8f7d\u6211\u505a\u597d\u7684\u793a\u4f8b<\/a>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528AOP\u9700\u8981\u6dfb\u52a0AspectJ\u7684\u4e24\u4e2a\u4f9d\u8d56\uff0c\u5728pom.xml\u6587\u4ef6\u91cc\u6dfb\u52a0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency>\n\t&lt;groupId>org.aspectj&lt;\/groupId>\n\t&lt;artifactId>aspectjrt&lt;\/artifactId>\n&lt;\/dependency>\n&lt;dependency>\n\t&lt;groupId>org.aspectj&lt;\/groupId>\n\t&lt;artifactId>aspectjweaver&lt;\/artifactId>\n&lt;\/dependency><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7248\u672c\u4e0d\u7528\u6dfb\u52a0\uff0cSpring Boot\u6709\u5185\u7f6e\u7684\u7ba1\u7406\u7684\u7248\u672c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u666e\u901a\u7684\u5bf9\u8c61\uff0c\u4e00\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4f5c\u5207\u5165\u70b9\uff0c\u6bd4\u5982HelloWorld.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.aopExamples;\n\npublic class HelloWorld {\n\tprivate String message;\n\t\n\tpublic void setMessage(String message){\n\t\tthis.message  = message;\n\t}\n\tpublic String getMessage(){\n\t\treturn message;\n\t}\n\tpublic void throwException() throws Exception{\n\t\tSystem.out.println(\"Exception raised\");\n\t\tthrow new RuntimeException(\"An exception.\");\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u521b\u5efaHelloWorld\u7684\u5207\u9762\u914d\u7f6e\u6587\u4ef6\uff0cHelloWorldAspect.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.aopExamples;\n\nimport org.aspectj.lang.annotation.After;\nimport org.aspectj.lang.annotation.AfterReturning;\nimport org.aspectj.lang.annotation.AfterThrowing;\nimport org.aspectj.lang.annotation.Aspect;\nimport org.aspectj.lang.annotation.Before;\nimport org.aspectj.lang.annotation.Pointcut;\n\n@Aspect\npublic class HelloWorldAspect {\n\t@Pointcut(\"execution(* com.explause.aopExamples.HelloWorld.*(..))\")\n\tprivate void selectAll() {}\n\t\n\t@Before(\"selectAll()\")\n\tpublic void beforeAdvice(){\n\t\tSystem.out.println(\"In the beforeAdvice.\");\n\t}\n\t@After(\"selectAll()\")\n\tpublic void afterAdvice(){\n\t\tSystem.out.println(\"In the afterAdvice.\");\n\t}\n\t@AfterReturning(pointcut = \"selectAll()\", returning = \"retVal\")\n\tpublic void afterReturningAdvice(Object retVal) {\n\t\tSystem.out.println(\"In the afterReturningAdvice:\" + retVal.toString() );\n\t}\n\t@AfterThrowing(pointcut = \"selectAll()\", throwing = \"ex\")\n\tpublic void AfterThrowingAdvice(Exception ex){\n\t\tSystem.out.println(\"In the AfterThrowingAdvice: \" + ex.toString());   \n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4e0a\u9762@Aspect\u6ce8\u89e3\u8868\u793a\u8fd9\u662f\u4e00\u4e2a\u5207\u9762\u914d\u7f6e\u3002<\/strong><br>@Pointcut(&#8220;execution(* com.explause.aopExamples.HelloWorld.*(..))&#8221;)\u8868\u793a\u6267\u884cHelloWorld\u5185\u7684\u4efb\u4f55\u8bbf\u95ee\u6743\u9650\uff08public\u3001private\u6216\u8005package\uff09\u7684\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u7684\u4efb\u4f55\u65b9\u6cd5\uff0c\u90fd\u662f\u4e00\u4e2a\u5207\u5165\u70b9\u3002<br>\u53ef\u4ee5\u6309\u9700\u5b9a\u5236\uff0c\u5177\u4f53\u7684\u6587\u6863\u5728<a rel=\"noreferrer noopener\" href=\"https:\/\/www.eclipse.org\/aspectj\/doc\/released\/adk15notebook\/annotations-pointcuts-and-advice.html#example-pointcuts\" target=\"_blank\">eclipse\u7684\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efaBean\u914d\u7f6e\u6587\u4ef6BeanConfig.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.aopExamples;\n\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.context.annotation.EnableAspectJAutoProxy;\n\n@Configuration\n@EnableAspectJAutoProxy\npublic class BeanConfig {\n\t@Bean\n\tpublic HelloWorld hw() {\n\t\tHelloWorld hw = new HelloWorld();\n\t\thw.setMessage(\"message in hw\");\n\t\treturn hw;\n\t}\n\t@Bean\n\tpublic HelloWorldAspect aop() {\n\t\treturn new HelloWorldAspect();\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5207\u9762\u914d\u7f6e\u540c\u6837\u9700\u8981\u6ce8\u518c\u4e3aBean\uff0c\u8fd8\u8981\u6dfb\u52a0@EnableAspectJAutoProxy\u6ce8\u89e3\u5f00\u542fAOP\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efamain\u51fd\u6570\uff08\u65b9\u6cd5\uff09AnnotationConfigDemo.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.aopExamples;\n\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\n\npublic class AnnotationConfigDemo {\n\tpublic static void main(String&#91;] args) throws Exception {\n\t\tAnnotationConfigApplicationContext context = \n\t\t\t\tnew AnnotationConfigApplicationContext(\"com.explause.aopExamples\");\n\t\tHelloWorld hw = context.getBean(HelloWorld.class);\n\t\thw.getMessage();\n\t\thw.throwException();\n\t\tcontext.close();\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"470\" src=\"https:\/\/explause.com\/wp-content\/uploads\/2022\/07\/AoP.png\" alt=\"\" class=\"wp-image-840\" srcset=\"https:\/\/devdoge.org\/wp-content\/uploads\/2022\/07\/AoP.png 801w, https:\/\/devdoge.org\/wp-content\/uploads\/2022\/07\/AoP-300x176.png 300w, https:\/\/devdoge.org\/wp-content\/uploads\/2022\/07\/AoP-768x451.png 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5207\u9762\u5904\u7406\u5f88\u6709\u7528\uff0c\u5e38\u7528\u4e8e\u65e5\u5fd7\u3001\u5ba1\u8ba1\u3001\u7533\u660e\u5f0f\u4e8b\u52a1\u3001\u5b89\u5168\u548c\u7f13\u5b58\u7b49\u65b9\u9762\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[18],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-spring","tag-spring"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=257"}],"version-history":[{"count":18,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":859,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/257\/revisions\/859"}],"wp:attachment":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}