{"id":6396,"date":"2024-07-24T15:37:51","date_gmt":"2024-07-24T07:37:51","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6396"},"modified":"2024-07-24T15:48:41","modified_gmt":"2024-07-24T07:48:41","slug":"spring-aop-%e4%b8%be%e4%b8%aa%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6396","title":{"rendered":"Spring AOP \u4e3e\u4f8b"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/docs.spring.io\/spring-boot\/reference\/features\/aop.html\">https:\/\/docs.spring.io\/spring-boot\/reference\/features\/aop.html<\/a><\/p>\n<p>Spring AOP\uff08Aspect-Oriented Programming\uff0c\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u65e8\u5728\u901a\u8fc7\u5206\u79bb\u6a2a\u5207\u5173\u6ce8\u70b9\uff08cross-cutting concerns\uff09\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u6a21\u5757\u5316\u3002\u5e38\u89c1\u7684\u6a2a\u5207\u5173\u6ce8\u70b9\u5305\u62ec\u65e5\u5fd7\u8bb0\u5f55\u3001\u5b89\u5168\u3001\u4e8b\u52a1\u7ba1\u7406\u7b49\u3002\u5728 Spring \u4e2d\uff0cAOP \u7684\u5b9e\u73b0\u4e3b\u8981\u4f9d\u8d56\u4e8e<strong>\u52a8\u6001\u4ee3\u7406<\/strong>\u673a\u5236\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Spring AOP \u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 AOP \u6765\u8bb0\u5f55\u65b9\u6cd5\u6267\u884c\u7684\u65f6\u95f4\u3002<\/p>\n<h3>\u6b65\u9aa4 1\uff1a\u6dfb\u52a0\u4f9d\u8d56<\/h3>\n<p>\u5728\u4f7f\u7528 Spring AOP \u4e4b\u524d\uff0c\u9700\u8981\u5728\u9879\u76ee\u7684\u6784\u5efa\u6587\u4ef6\u4e2d\u6dfb\u52a0 Spring AOP \u7684\u4f9d\u8d56\u3002\u5982\u679c\u4f7f\u7528 Maven\uff0c\u53ef\u4ee5\u5728 <code>pom.xml<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-aop&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u5982\u679c\u4f7f\u7528 Gradle\uff0c\u53ef\u4ee5\u5728 <code>build.gradle<\/code> \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-gradle\">implementation &#039;org.springframework.boot:spring-boot-starter-aop&#039;<\/code><\/pre>\n<h3>\u6b65\u9aa4 2\uff1a\u5b9a\u4e49\u4e1a\u52a1\u903b\u8f91\u7c7b<\/h3>\n<p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u4e1a\u52a1\u903b\u8f91\u7c7b\uff0c\u4f8b\u5982 <code>UserService<\/code>\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.stereotype.Service;\n\n@Service\npublic class UserService {\n\n    public void createUser(String username) {\n        System.out.println(&quot;User &quot; + username + &quot; is created.&quot;);\n    }\n}<\/code><\/pre>\n<h3>\u6b65\u9aa4 3\uff1a\u5b9a\u4e49\u5207\u9762\uff08Aspect\uff09<\/h3>\n<p>\u63a5\u4e0b\u6765\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5207\u9762\u7c7b\uff0c\u7528\u4e8e\u8bb0\u5f55\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f7f\u7528 <code>@Aspect<\/code> \u6ce8\u89e3\u6807\u8bb0\u8be5\u7c7b\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5207\u70b9\u548c\u901a\u77e5\uff1a<\/p>\n<pre><code class=\"language-java\">import org.aspectj.lang.ProceedingJoinPoint;\nimport org.aspectj.lang.annotation.Around;\nimport org.aspectj.lang.annotation.Aspect;\nimport org.springframework.stereotype.Component;\n\n@Aspect\n@Component\npublic class LoggingAspect {\n\n    @Around(&quot;execution(* com.example.demo.UserService.*(..))&quot;)\n    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {\n        long start = System.currentTimeMillis();\n\n        Object proceed = joinPoint.proceed(); \/\/ \u6267\u884c\u76ee\u6807\u65b9\u6cd5\n\n        long executionTime = System.currentTimeMillis() - start;\n        System.out.println(joinPoint.getSignature() + &quot; executed in &quot; + executionTime + &quot;ms&quot;);\n\n        return proceed;\n    }\n}<\/code><\/pre>\n<h3>\u6b65\u9aa4 4\uff1a\u914d\u7f6e Spring \u5e94\u7528<\/h3>\n<p>\u786e\u4fdd Spring \u80fd\u591f\u626b\u63cf\u5230\u4f60\u7684\u5207\u9762\u7c7b\u548c\u4e1a\u52a1\u903b\u8f91\u7c7b\u3002\u53ef\u4ee5\u5728 Spring Boot \u4e3b\u5e94\u7528\u7c7b\u4e2d\u4f7f\u7528 <code>@SpringBootApplication<\/code> \u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\n@SpringBootApplication\npublic class DemoApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n}<\/code><\/pre>\n<h3>\u6b65\u9aa4 5\uff1a\u6d4b\u8bd5 AOP \u529f\u80fd<\/h3>\n<p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5\u7c7b\u6216\u63a7\u5236\u5668\u6765\u8c03\u7528\u4e1a\u52a1\u903b\u8f91\u7c7b\u7684\u65b9\u6cd5\uff0c\u9a8c\u8bc1 AOP \u5207\u9762\u662f\u5426\u751f\u6548\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.CommandLineRunner;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class TestRunner implements CommandLineRunner {\n\n    @Autowired\n    private UserService userService;\n\n    @Override\n    public void run(String... args) throws Exception {\n        userService.createUser(&quot;John&quot;);\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u540e\uff0c\u4f60\u5e94\u8be5\u4f1a\u5728\u63a7\u5236\u53f0\u4e0a\u770b\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u8f93\u51fa\uff1a<\/p>\n<pre><code class=\"language-plaintext\">User John is created.\nvoid com.example.demo.UserService.createUser(String) executed in 10ms<\/code><\/pre>\n<h3>\u8be6\u7ec6\u89e3\u6790<\/h3>\n<ol>\n<li><strong>\u4e1a\u52a1\u903b\u8f91\u7c7b\uff08UserService\uff09<\/strong>\uff1a\u8fd9\u662f\u4e00\u4e2a\u666e\u901a\u7684 Spring \u670d\u52a1\u7c7b\uff0c\u5305\u542b\u4e00\u4e2a <code>createUser<\/code> \u65b9\u6cd5\u3002<\/li>\n<li><strong>\u5207\u9762\u7c7b\uff08LoggingAspect\uff09<\/strong>\uff1a\n<ul>\n<li><strong>@Aspect<\/strong>\uff1a\u6807\u8bb0\u8fd9\u4e2a\u7c7b\u4e3a\u5207\u9762\u3002<\/li>\n<li><strong>@Component<\/strong>\uff1a\u5c06\u8fd9\u4e2a\u7c7b\u6ce8\u518c\u4e3a Spring \u5bb9\u5668\u4e2d\u7684\u4e00\u4e2a Bean\u3002<\/li>\n<li><strong>@Around(&quot;execution(<em> com.example.demo.UserService.<\/em>(..))&quot;)<\/strong>\uff1a\u5b9a\u4e49\u4e00\u4e2a\u73af\u7ed5\u901a\u77e5\uff0c\u5207\u70b9\u662f <code>UserService<\/code> \u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u3002<\/li>\n<li><strong>logExecutionTime<\/strong> \u65b9\u6cd5\uff1a\u8bb0\u5f55\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5e76\u5728\u65b9\u6cd5\u6267\u884c\u524d\u540e\u6253\u5370\u65e5\u5fd7\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u4e3b\u5e94\u7528\u7c7b\uff08DemoApplication\uff09<\/strong>\uff1aSpring Boot \u7684\u4e3b\u7c7b\uff0c\u7528\u4e8e\u542f\u52a8\u5e94\u7528\u3002<\/li>\n<li><strong>\u6d4b\u8bd5\u7c7b\uff08TestRunner\uff09<\/strong>\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u6d4b\u8bd5\u7c7b\uff0c\u7528\u4e8e\u89e6\u53d1 <code>UserService<\/code> \u7684\u65b9\u6cd5\uff0c\u9a8c\u8bc1 AOP \u529f\u80fd\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Spring AOP \u6765\u5b9e\u73b0\u65b9\u6cd5\u6267\u884c\u65f6\u95f4\u7684\u65e5\u5fd7\u8bb0\u5f55\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u8bb0\u5f55\u8fd9\u6837\u7684\u6a2a\u5207\u5173\u6ce8\u70b9\u4ece\u4e1a\u52a1\u903b\u8f91\u4e2d\u5206\u79bb\u51fa\u6765\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\u3001ChatGPT \u95ee\u7b54\u751f\u6210 https:\/\/docs.spring.io\/spring-boot\/ref&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6396\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[133],"class_list":["post-6396","post","type-post","status-publish","format-standard","hentry","category-123","tag-spring"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6396"}],"version-history":[{"count":3,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6396\/revisions"}],"predecessor-version":[{"id":6399,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6396\/revisions\/6399"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}