{"id":12043,"date":"2023-11-28T22:41:08","date_gmt":"2023-11-28T14:41:08","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12043"},"modified":"2023-12-11T15:04:11","modified_gmt":"2023-12-11T07:04:11","slug":"spring-zhong-shi-yongaop-bu-huo-ri-zhi-hui-ma","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-shi-yongaop-bu-huo-ri-zhi-hui-ma\/","title":{"rendered":"Spring\u4e2d\u4f7f\u7528AOP\u6355\u83b7\u65e5\u5fd7\u4f1a\u5417\uff1f"},"content":{"rendered":"<p>\u662f\u7684\uff0cSpring\u6846\u67b6\u4e2d\u4f7f\u7528AOP\uff08Aspect-Oriented Programming\uff09\u975e\u5e38\u9002\u5408\u6355\u83b7\u548c\u5904\u7406\u65e5\u5fd7\u3002\u901a\u8fc7AOP\uff0c\u4f60\u53ef\u4ee5\u5c06\u6a2a\u5207\u5173\u6ce8\u70b9\uff08cross-cutting concerns\uff09\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u76d1\u63a7\u3001\u4e8b\u52a1\u7ba1\u7406\u7b49\u4ece\u4e3b\u8981\u4e1a\u52a1\u903b\u8f91\u4e2d\u5206\u79bb\u51fa\u6765\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6a21\u5757\u5316\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Spring AOP\u8fdb\u884c\u65e5\u5fd7\u8bb0\u5f55\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\n<strong>\u5b9a\u4e49\u5207\u9762\uff08Aspect\uff09\uff1a<\/strong><br \/>\n\u521b\u5efa\u4e00\u4e2a\u5207\u9762\u7c7b\uff0c\u5b9a\u4e49\u5728\u4f55\u65f6\u4f55\u5730\u6267\u884c\u6a2a\u5207\u903b\u8f91\uff0c\u4f8b\u5982\u8bb0\u5f55\u65b9\u6cd5\u6267\u884c\u524d\u540e\u7684\u65e5\u5fd7\u3002<\/p>\n<pre><code class=\"language-java\">import org.aspectj.lang.JoinPoint;\nimport org.aspectj.lang.annotation.After;\nimport org.aspectj.lang.annotation.Aspect;\nimport org.aspectj.lang.annotation.Before;\n\n@Aspect\npublic class LoggingAspect {\n\n    @Before(&quot;execution(* com.example.service.*.*(..))&quot;)\n    public void logBefore(JoinPoint joinPoint) {\n        System.out.println(&quot;Before executing: &quot; + joinPoint.getSignature().toShortString());\n    }\n\n    @After(&quot;execution(* com.example.service.*.*(..))&quot;)\n    public void logAfter(JoinPoint joinPoint) {\n        System.out.println(&quot;After executing: &quot; + joinPoint.getSignature().toShortString());\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u914d\u7f6eAOP\uff1a<\/strong><br \/>\n\u5728Spring\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6eAOP\uff0c\u5c06\u5207\u9762\u7c7b\u548c\u5207\u70b9\u5173\u8054\u8d77\u6765\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;aop:aspectj-autoproxy \/&gt;\n\n&lt;bean id=&quot;loggingAspect&quot; class=&quot;com.example.aspect.LoggingAspect&quot; \/&gt;\n<\/code><\/pre>\n<p>\u6216\u8005\uff0c\u5982\u679c\u4f60\u4f7f\u7528Java\u914d\u7f6e\u7c7b\uff1a<\/p>\n<pre><code class=\"language-java\">@Configuration\n@EnableAspectJAutoProxy\npublic class AppConfig {\n\n    @Bean\n    public LoggingAspect loggingAspect() {\n        return new LoggingAspect();\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5e94\u7528\u4e1a\u52a1\u903b\u8f91\uff1a<\/strong><br \/>\n\u521b\u5efa\u4e00\u4e2a\u4e1a\u52a1\u670d\u52a1\u7c7b\uff0c\u4f8b\u5982<code>MyService<\/code>\uff0c\u5e76\u5728\u8be5\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e9b\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-java\">package com.example.service;\n\npublic class MyService {\n\n    public void performTask() {\n        System.out.println(&quot;Performing the task...&quot;);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u8fd0\u884c\u5e94\u7528\uff1a<\/strong><br \/>\n\u5728\u8fd0\u884c\u5e94\u7528\u65f6\uff0c\u4f60\u5c06\u770b\u5230\u5728<code>performTask<\/code>\u65b9\u6cd5\u6267\u884c\u524d\u540e\uff0c\u65e5\u5fd7\u4fe1\u606f\u88ab\u8bb0\u5f55\u4e86\u3002<\/p>\n<pre><code class=\"language-java\">public static void main(String[] args) {\n    ApplicationContext context = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\n    MyService myService = context.getBean(MyService.class);\n    myService.performTask();\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7AOP\uff0c\u5728<code>MyService<\/code>\u7c7b\u7684<code>performTask<\/code>\u65b9\u6cd5\u6267\u884c\u524d\u540e\uff0c\u65e5\u5fd7\u4fe1\u606f\u88ab\u5207\u9762<code>LoggingAspect<\/code>\u6355\u83b7\u5e76\u8bb0\u5f55\u3002\u8fd9\u6837\u7684\u65e5\u5fd7\u8bb0\u5f55\u662fAOP\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u573a\u666f\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u5728\u4e0d\u4fb5\u5165\u4e3b\u8981\u4e1a\u52a1\u903b\u8f91\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u6a2a\u5207\u5173\u6ce8\u70b9\u7684\u590d\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u662f\u7684\uff0cSpring\u6846\u67b6\u4e2d\u4f7f\u7528AOP\uff08Aspect-Oriented Programming\uff09\u975e\u5e38\u9002\u5408\u6355\u83b7\u548c\u5904\u7406 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-12043","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=12043"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12043\/revisions"}],"predecessor-version":[{"id":44865,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12043\/revisions\/44865"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12043"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}