{"id":297,"date":"2022-04-07T21:15:08","date_gmt":"2022-04-07T13:15:08","guid":{"rendered":"https:\/\/blog.lijiajia.top\/?p=297"},"modified":"2026-06-19T21:38:54","modified_gmt":"2026-06-19T13:38:54","slug":"spring%e6%a1%86%e6%9e%b6%e5%ad%a6%e4%b9%a0%ef%bc%9aaop%e5%ba%95%e5%b1%82%e5%8e%9f%e7%90%86%e5%8d%81%e5%9b%9b","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=297","title":{"rendered":"Spring5\u6846\u67b6\u5b66\u4e60\uff1aAOP\u5e95\u5c42\u539f\u7406(\u5341\u56db)"},"content":{"rendered":"<h2>AOP\u5e95\u5c42\u4f7f\u7528\u52a8\u6001\u4ee3\u7406<\/h2>\n<p>(1)\u6709\u4e24\u79cd\u60c5\u51b5\u52a8\u6001\u4ee3\u7406<\/p>\n<h3>\u7b2c\u4e00\u79cd\uff1a\u6709\u63a5\u53e3\u60c5\u51b5\uff0c\u4f7f\u7528JDK\u52a8\u6001\u4ee3\u7406<\/h3>\n<p>*\u521b\u5efa\u63a5\u53e3\u5b9e\u73b0\u7c7b\u4ee3\u7406\u5bf9\u8c61\uff0c\u589e\u5f3a\u7c7b\u7684\u65b9\u6cd5<\/p>\n<h3>\u7b2c\u4e8c\u79cd\uff1a\u6ca1\u6709\u63a5\u53e3\u60c5\u51b5\uff0c\u4f7f\u7528CGLIB\u52a8\u6001\u4ee3\u7406<\/h3>\n<p>*\u521b\u5efa\u5b50\u7c7b\u7684\u4ee3\u7406\u5bf9\u8c61\uff0c\u589e\u5f3a\u7c7b\u7684\u65b9\u6cd5<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/04\/image-1649379122050.png'><img class=\"lazyload lazyload-style-11\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/bucket-blog-img.oss-cn-shanghai.aliyuncs.com\/wp-content\/uploads\/2022\/04\/image-1649379122050.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"file\" \/><\/div><\/p>\n<h2>JDK\u52a8\u6001\u4ee3\u7406\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<h3>1.\u4f7f\u7528Proxy\u7c7b\u91cc\u9762\u65b9\u6cd5\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61<\/h3>\n<pre><code class=\"language-markdown\">Class Proxy\njava.lang.Object\njava.lang.reflect.Proxy<\/code><\/pre>\n<h3>2.\u8c03\u7528newProxyInstance\u65b9\u6cd5<\/h3>\n<pre><code class=\"language-markdown\">static Object   newProxyInstance(ClassLoader loader, \u7c7b&lt;?&gt;[] interfaces, InvocationHandler h)\n\u8fd4\u56de\u6307\u5b9a\u63a5\u53e3\u7684\u4ee3\u7406\u7c7b\u7684\u5b9e\u4f8b\uff0c\u8be5\u63a5\u53e3\u5c06\u65b9\u6cd5\u8c03\u7528\u5206\u6d3e\u7ed9\u6307\u5b9a\u7684\u8c03\u7528\u5904\u7406\u7a0b\u5e8f\u3002<\/code><\/pre>\n<p>\u65b9\u6cd5\u6709\u4e09\u4e2a\u53c2\u6570\uff1a<br \/>\n1\uff09.\u7c7b\u52a0\u8f7d\u5668<br \/>\n2\uff09.\u589e\u5f3a\u65b9\u6cd5\u6240\u5728\u7684\u7c7b<br \/>\n3\uff09.\u5b9e\u73b0InvocationHandler\u63a5\u53e3\uff0c\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\uff0c\u5199\u589e\u5f3a\u90e8\u5206\u4ee3\u7801<\/p>\n<h3>3.JDK\u52a8\u6001\u4ee3\u7406\u4ee3\u7801<\/h3>\n<p>1\uff09\u521b\u5efa\u63a5\u53e3\uff0c\u5b9a\u4e49\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public interface UserDao {\n\n    public int add(int a,int b);\n\n    public String update(String id);\n}<\/code><\/pre>\n<p>2\uff09\u521b\u5efa\u63a5\u53e3\u5b9e\u73b0\u7c7b\uff0c\u5b9e\u73b0\u65b9\u6cd5<\/p>\n<pre><code class=\"language-java\">public class UserDaoImpl implements UserDao{\n\n    @Override\n    public int add(int a, int b) {\n        System.out.println(&quot;add\u65b9\u6cd5\u6267\u884c\u4e86\u3002\u3002\u3002\u3002&quot;);\n        return a+b;\n    }\n\n    @Override\n    public String update(String id) {\n        System.out.println(&quot;update\u65b9\u6cd5\u6267\u884c\u4e86\u3002\u3002\u3002\u3002&quot;);\n        return id;\n    }\n}<\/code><\/pre>\n<p>3\uff09\u4f7f\u7528Proxy\u7c7b\uff0c\u521b\u5efa\u63a5\u53e3\u4ee3\u7406\u5bf9\u8c61<\/p>\n<pre><code class=\"language-java\">\/\/\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u4ee3\u7801\nclass UserDaoProxy implements InvocationHandler{\n\n    \/\/\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\uff0c\u5c06\u9700\u8981\u4ee3\u7406\u7684\u5bf9\u8c61\u4f20\u9012\u8fdb\u6765\n    \/\/    \u6709\u53c2\u6784\u9020\u4f20\u9012\n    \/\/\u53ef\u4ee5\u76f4\u63a5 \u4f20\u9012 UserDaoImpl\u5bf9\u8c61\uff0c\u4f46\u4e3a\u4e86\u901a\u7528\uff0c\u4f7f\u7528Object\n    private Object object;\n    public UserDaoProxy(Object object){\n        this.object = object;\n    }\n\n    \/\/\u589e\u5f3a\u7684\u4ee3\u7406\u903b\u8f91\n    @Override\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n        \/\/\u53ef\u4ee5\u6839\u636e\u83b7\u53d6\u7684\u5bf9\u8c61\uff0c\u4ee3\u7801\u540d\u79f0\u5224\u65ad \u662f\u5426\u589e\u5f3a\n\n        \/\/\u65b9\u6cd5\u4e4b\u524d\n        System.out.println(&quot;\u65b9\u6cd5\u4e4b\u524d\u6267\u884c\u3002\u3002\u3002\u3002\u3002&quot;    + method.getName() + &quot;  \u4f20\u9012\u7684\u53c2\u6570\uff1a&quot;+ Arrays.toString(args));\n\n        \/\/\u6267\u884c\u65b9\u6cd5-\u88ab\u589e\u5f3a\u7684\u65b9\u6cd5\n        Object res = method.invoke(object, args);\n\n        \/\/\u65b9\u6cd5\u4e4b\u540e\n        System.out.println(&quot;\u65b9\u6cd5\u4e4b\u540e\u6267\u884c1\u3002\u3002\u3002\u3002\u3002&quot;    + method.getName() + &quot;  \u4f20\u9012\u7684\u53c2\u6570\uff1a&quot;+ Arrays.toString(args));\n        System.out.println(&quot;\u65b9\u6cd5\u4e4b\u540e\u6267\u884c2\u3002\u3002\u3002\u3002\u3002&quot;    + method.getName() + &quot;  \u8fd4\u56de\u503c\uff1a&quot;+ res);\n        System.out.println(&quot;\u65b9\u6cd5\u4e4b\u540e\u6267\u884c3\u3002\u3002\u3002\u3002\u3002&quot;    + method.getName() + &quot;  \u589e\u5f3a\u7684\u5bf9\u8c61\uff1a&quot;+ object);\n        return res;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">public class JDKProxy {\n\n    public static void main(String[] args) {\n\n        \/\/\u521b\u5efa\u63a5\u53e3\u5b9e\u73b0\u7c7b\u4ee3\u7406\u5bf9\u8c61\n        Class[] interfases = {UserDao.class};\n\n        \/\/1.InvocationHandler\u5185\u90e8\u7c7b\u7684\u5b9e\u73b0\u65b9\u5f0f\n\/\/        Object o = Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfases, new InvocationHandler() {\n\/\/            @Override\n\/\/            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n\/\/                return null;\n\/\/            }\n\/\/        });\n\n\/\/        2.InvocationHandler\u5916\u90e8\u5b9e\u73b0\n        UserDao userDao = new UserDaoImpl();\n        UserDao dao =\n                (UserDao)Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfases,new UserDaoProxy(userDao));\n\n        int add = dao.add(2, 3);\n        System.out.println(&quot;\u76f8\u52a0\u7684\u7ed3\u679c\uff1a&quot;+add);\n        System.out.println(&quot;=============================================&quot;);\n        String update = dao.update(&quot;30&quot;);\n        System.out.println(&quot;\u66f4\u65b0\u7684\u7ed3\u679c\uff1a&quot;+update);\n    }\n}<\/code><\/pre>\n<p>4)\u4ee3\u7801\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-java\">\u65b9\u6cd5\u4e4b\u524d\u6267\u884c\u3002\u3002\u3002\u3002\u3002add  \u4f20\u9012\u7684\u53c2\u6570\uff1a[2, 3]\nadd\u65b9\u6cd5\u6267\u884c\u4e86\u3002\u3002\u3002\u3002\n\u65b9\u6cd5\u4e4b\u540e\u6267\u884c1\u3002\u3002\u3002\u3002\u3002add  \u4f20\u9012\u7684\u53c2\u6570\uff1a[2, 3]\n\u65b9\u6cd5\u4e4b\u540e\u6267\u884c2\u3002\u3002\u3002\u3002\u3002add  \u8fd4\u56de\u503c\uff1a5\n\u65b9\u6cd5\u4e4b\u540e\u6267\u884c3\u3002\u3002\u3002\u3002\u3002add  \u589e\u5f3a\u7684\u5bf9\u8c61\uff1acom.spring5.UserDaoImpl@355da254\n\u76f8\u52a0\u7684\u7ed3\u679c\uff1a5\n=============================================\n\u65b9\u6cd5\u4e4b\u524d\u6267\u884c\u3002\u3002\u3002\u3002\u3002update  \u4f20\u9012\u7684\u53c2\u6570\uff1a[30]\nupdate\u65b9\u6cd5\u6267\u884c\u4e86\u3002\u3002\u3002\u3002\n\u65b9\u6cd5\u4e4b\u540e\u6267\u884c1\u3002\u3002\u3002\u3002\u3002update  \u4f20\u9012\u7684\u53c2\u6570\uff1a[30]\n\u65b9\u6cd5\u4e4b\u540e\u6267\u884c2\u3002\u3002\u3002\u3002\u3002update  \u8fd4\u56de\u503c\uff1a30\n\u65b9\u6cd5\u4e4b\u540e\u6267\u884c3\u3002\u3002\u3002\u3002\u3002update  \u589e\u5f3a\u7684\u5bf9\u8c61\uff1acom.spring5.UserDaoImpl@355da254\n\u66f4\u65b0\u7684\u7ed3\u679c\uff1a30<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AOP\u5e95\u5c42\u4f7f\u7528\u52a8\u6001\u4ee3\u7406 (1)\u6709\u4e24\u79cd\u60c5\u51b5\u52a8\u6001\u4ee3\u7406 \u7b2c\u4e00\u79cd\uff1a\u6709\u63a5\u53e3\u60c5\u51b5\uff0c\u4f7f\u7528JDK\u52a8\u6001\u4ee3\u7406 *\u521b\u5efa\u63a5\u53e3\u5b9e\u73b0\u7c7b\u4ee3\u7406 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[125],"tags":[31,68],"class_list":["post-297","post","type-post","status-publish","format-standard","hentry","category-java-backend","tag-aop","tag-spring"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/297\/revisions"}],"predecessor-version":[{"id":2148,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/297\/revisions\/2148"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}