{"id":153,"date":"2025-06-20T12:06:26","date_gmt":"2025-06-20T04:06:26","guid":{"rendered":"https:\/\/lib.kimsor.top\/?p=153"},"modified":"2025-06-20T12:07:08","modified_gmt":"2025-06-20T04:07:08","slug":"%e4%bd%bf%e7%94%a8autowire%e7%9a%84%e6%b3%a8%e5%85%a5%e5%af%b9%e8%b1%a1%e6%97%a0%e6%b3%95%e5%8f%8d%e5%b0%84","status":"publish","type":"post","link":"https:\/\/lib.kimsor.top\/?p=153","title":{"rendered":"\u4f7f\u7528@Autowire\u7684\u6ce8\u5165\u5bf9\u8c61\u65e0\u6cd5\u53cd\u5c04"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5148\u770b\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ee5\u53ca\u8981\u53cd\u5c04\u8c03\u7528\u7684\u65b9\u6cd5\u548c\u65b9\u6cd5\u7684\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public static &lt;T>Future&lt;T> getFuture(final Object t, final String methodName, final Object... dto) {\n        Callable callable = new Callable() {\n            @Override\n            public Object call() throws Exception {\n                Object result = null;\n                Method&#91;] methods = t.getClass().getMethods();\n                for (Method m : methods) {\n                    if (methodName.equalsIgnoreCase(m.getName())) {\n                        try {\n                            result = m.invoke(t, dto);\n                        } catch (InvocationTargetException e) {\n                            \/\/ \u83b7\u53d6\u5b9e\u9645\u76ee\u6807\u5f02\u5e38\n                            Throwable targetException = e.getTargetException();\n                            log.error(methodName+\"\u3010Future\u3011\u6267\u884c\u5f02\u5e38\uff1a\"+targetException);\n                            throw e;\n                        }\n                    }\n                }\n                return result;\n            }\n        };\n        return executor.submit(callable);\n    }<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u6ce8\u5165\u5bf9\u8c61<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"267\" height=\"37\" src=\"https:\/\/lib.kimsor.top\/wp-content\/uploads\/2025\/06\/image.png\" alt=\"\" class=\"wp-image-154\" style=\"width:483px;height:auto\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u73b0\u5728\u60f3\u901a\u8fc7\u53cd\u5c04base64\u8fd9\u4e2a\u5bf9\u8c61\u5e76\u8c03\u7528get64\u8fd9\u4e2a\u65b9\u6cd5<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"23\" src=\"https:\/\/lib.kimsor.top\/wp-content\/uploads\/2025\/06\/image-1.png\" alt=\"\" class=\"wp-image-155\" style=\"width:650px;height:auto\" srcset=\"https:\/\/lib.kimsor.top\/wp-content\/uploads\/2025\/06\/image-1.png 415w, https:\/\/lib.kimsor.top\/wp-content\/uploads\/2025\/06\/image-1-300x17.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/figure>\n\n\n\n<p>\u4f46\u662f\u6267\u884c\u4e4b\u540e\u8fd4\u56de\u7684\u7ed3\u679c\u662fnull\uff0c\u8bf4\u660e\u8c03\u7528\u5931\u8d25\u4e86\u3002\u4f46\u662f\u901a\u8fc7@Autowire\u6ce8\u5165\u7684mapper\u5374\u53ef\u4ee5\u8c03\u7528\uff0c\u8fd9\u91cc\u5c31\u5f88\u5947\u602a\u4e86\u3002<\/p>\n\n\n\n<p>\u540c\u6837\u662f\u6ce8\u5165\u5bf9\u8c61\uff0c\u4e3a\u4ec0\u4e48\u4e00\u4e2a\u80fd\u8c03\u7528\uff0c\u4e00\u4e2a\u65e0\u6cd5\u8c03\u7528\u5462\uff1f<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u540e\u9762\u67e5\u8be2\u5230\u539f\u6765mybatis\u7684mapper\u662fjdk\u4ee3\u7406\u7684\uff0c\u800c\u88ab@Component\uff0c@Service\u7b49\u4fee\u9970\u7684\u5bf9\u8c61\u6ce8\u5165\u662f\u7531CGLIB\u4ee3\u7406\u7684\uff0c\u6240\u4ee5\u53cd\u5c04\u51fa\u6765\u7684\u5bf9\u8c61\u65e0\u6cd5\u8c03\u7528\u539f\u672c\u7684\u65b9\u6cd5\uff0c\u53ea\u80fd\u83b7\u53d6\u539f\u5bf9\u8c61\u53bb\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u66f4\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public static &lt;T>Future&lt;T> getFuture(final Object t, final String methodName, final Object... dto) {\n        Callable callable = new Callable() {\n            @Override\n            public Object call() throws Exception {\n                Object target = t;\n                if (AopUtils.isAopProxy(target)) {\n                    target = AopProxyUtils.getSingletonTarget(target);\n                }\n                Object result = null;\n                Method&#91;] methods = target.getClass().getMethods();\n                for (Method m : methods) {\n                    if (methodName.equalsIgnoreCase(m.getName())) {\n                        try {\n                            result = m.invoke(target, dto);\n                        } catch (InvocationTargetException e) {\n                            \/\/ \u83b7\u53d6\u5b9e\u9645\u76ee\u6807\u5f02\u5e38\n                            Throwable targetException = e.getTargetException();\n                            log.error(methodName+\"\u3010Future\u3011\u6267\u884c\u5f02\u5e38\uff1a\"+targetException);\n                            throw e;\n                        }\n                    }\n                }\n                return result;\n            }\n        };\n        return executor.submit(callable);\n    }<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u901a\u8fc7AopUtils\u8fd9\u4e2a\u7c7b\u7684isAopProxy\u65b9\u6cd5\u53bb\u9a8c\u8bc1\u662f\u5426\u662fcglib\u4ee3\u7406\u7684\u5bf9\u8c61<\/p>\n\n\n\n<p>isAopProxy\u6e90\u7801\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"79\" src=\"https:\/\/lib.kimsor.top\/wp-content\/uploads\/2025\/06\/image-2.png\" alt=\"\" class=\"wp-image-157\" srcset=\"https:\/\/lib.kimsor.top\/wp-content\/uploads\/2025\/06\/image-2.png 714w, https:\/\/lib.kimsor.top\/wp-content\/uploads\/2025\/06\/image-2-300x33.png 300w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>\u5982\u679c\u662fCGLIB\u4ee3\u7406\u5bf9\u8c61\u5219\u901a\u8fc7AopProxyUtils.getSingletonTarget\u65b9\u6cd5\u53bb\u83b7\u53d6\u539f\u5bf9\u8c61\uff0c\u6b64\u65f6\u518d\u4f7f\u7528invoke\u8c03\u7528\u65b9\u6cd5\u5219\u8c03\u7528\u6210\u529f<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u770b\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ee5\u53ca\u8981\u53cd\u5c04\u8c03\u7528\u7684\u65b9\u6cd5\u548c\u65b9\u6cd5\u7684\u53c2\u6570\uff1a \u8fd9\u91cc\u6709\u4e00\u4e2a\u6ce8\u5165\u5bf9\u8c61 \u73b0\u5728\u60f3\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-springboot","category-5"],"_links":{"self":[{"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=153"}],"version-history":[{"count":3,"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":159,"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=\/wp\/v2\/posts\/153\/revisions\/159"}],"wp:attachment":[{"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lib.kimsor.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}