{"id":12323,"date":"2023-11-28T22:50:20","date_gmt":"2023-11-28T14:50:20","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12323"},"modified":"2023-12-11T16:20:08","modified_gmt":"2023-12-11T08:20:08","slug":"spring-na-li-yong-dao-le-dan-li","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-na-li-yong-dao-le-dan-li\/","title":{"rendered":"Spring\u54ea\u91cc\u7528\u5230\u4e86\u5355\u4f8b\uff1f"},"content":{"rendered":"<p>Spring\u6846\u67b6\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u4e86\u5355\u4f8b\u6a21\u5f0f\uff0c\u5c24\u5176\u662f\u5728Spring\u5bb9\u5668\u7ba1\u7406Bean\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e3b\u8981\u573a\u666f\uff1a<\/p>\n<ol>\n<li>\n<strong>Spring\u5bb9\u5668\u4e2d\u7684Bean\u9ed8\u8ba4\u4e3a\u5355\u4f8b\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728Spring\u5bb9\u5668\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u7279\u522b\u6307\u5b9a\uff0cBean \u9ed8\u8ba4\u662f\u5355\u4f8b\u7684\u3002\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u5bb9\u5668\u4e2d\u7684Bean\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u8bf7\u6c42\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Service \/\/ \u9ed8\u8ba4\u4e3a\u5355\u4f8b\npublic class MyService {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Singleton\u4f5c\u7528\u57df\uff1a<\/strong><\/p>\n<ul>\n<li>Spring\u5bb9\u5668\u4e2d\u7684Bean\u53ef\u4ee5\u901a\u8fc7<code>@Scope(&quot;singleton&quot;)<\/code>\u6ce8\u89e3\u660e\u786e\u6307\u5b9a\u4e3a\u5355\u4f8b\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u5728\u5bb9\u5668\u4e2d\u8bf7\u6c42\u591a\u5c11\u6b21\uff0c\u90fd\u4f1a\u8fd4\u56de\u540c\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\n@Scope(&quot;singleton&quot;)\npublic class MySingletonBean {\n    \/\/ ...\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Spring\u5bb9\u5668\u4e2d\u7684\u5355\u4f8b\u7f13\u5b58\uff1a<\/strong><\/p>\n<ul>\n<li>Spring\u5bb9\u5668\u672c\u8eab\u4e5f\u4f7f\u7528\u4e86\u5355\u4f8b\u6a21\u5f0f\u3002\u5bb9\u5668\u4e2d\u7684Bean\u5b9a\u4e49\u548c\u5b9e\u4f8b\u88ab\u7f13\u5b58\uff0c\u786e\u4fdd\u5728\u5bb9\u5668\u7684\u751f\u547d\u5468\u671f\u5185\u53ea\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u6709\u52a9\u4e8e\u63d0\u9ad8\u6027\u80fd\u548c\u964d\u4f4e\u8d44\u6e90\u6d88\u8017\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>ApplicationContext\u7684\u5355\u4f8b\u7f13\u5b58\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728Spring\u4e2d\uff0c<code>ApplicationContext<\/code>\u662f\u4e00\u4e2a\u5168\u5c40\u7684\u5355\u4f8b\u5bf9\u8c61\uff0c\u5b83\u8d1f\u8d23\u7ba1\u7406\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7684Bean\u5b9a\u4e49\u548c\u5b9e\u4f8b\u3002\u901a\u8fc7\u5bb9\u5668\u83b7\u53d6Bean\u65f6\uff0c\u9996\u5148\u67e5\u627e\u5bb9\u5668\u4e2d\u662f\u5426\u5df2\u6709\u8be5Bean\u7684\u5b9e\u4f8b\uff0c\u5982\u679c\u6709\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5219\u521b\u5efa\u65b0\u7684\u5b9e\u4f8b\u5e76\u52a0\u5165\u7f13\u5b58\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);\nMyService myService = context.getBean(MyService.class); \/\/ \u83b7\u53d6MyService\u5b9e\u4f8b\uff0c\u53ef\u80fd\u662f\u5bb9\u5668\u4e2d\u7684\u5355\u4f8b\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u603b\u4f53\u800c\u8a00\uff0cSpring\u5728\u591a\u4e2a\u5c42\u6b21\u4e0a\u4f7f\u7528\u4e86\u5355\u4f8b\u6a21\u5f0f\uff0c\u4ee5\u786e\u4fdd\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5171\u4eab\u76f8\u540c\u7684\u5b9e\u4f8b\uff0c\u63d0\u9ad8\u6027\u80fd\u5e76\u786e\u4fdd\u4e00\u81f4\u6027\u3002\u7136\u800c\uff0c\u5f00\u53d1\u8005\u4ecd\u7136\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u4f5c\u7528\u57df\uff0c\u5982\u539f\u578b\uff08prototype\uff09\u7b49\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring\u6846\u67b6\u4e2d\u5e7f\u6cdb\u4f7f\u7528\u4e86\u5355\u4f8b\u6a21\u5f0f\uff0c\u5c24\u5176\u662f\u5728Spring\u5bb9\u5668\u7ba1\u7406Bean\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4e3b\u8981\u573a\u666f\uff1a Sprin [&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-12323","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12323","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=12323"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12323\/revisions"}],"predecessor-version":[{"id":47886,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12323\/revisions\/47886"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12323"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}