
{"id":2660,"date":"2020-12-10T15:24:52","date_gmt":"2020-12-10T07:24:52","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2660"},"modified":"2020-12-10T15:59:04","modified_gmt":"2020-12-10T07:59:04","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%e4%b9%8bspring%e7%9a%84bean%e4%bd%9c%e7%94%a8%e5%9f%9f","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2660","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684bean\u4f5c\u7528\u57df"},"content":{"rendered":"<h3>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684bean\u4f5c\u7528\u57df<\/h3>\n<p>Scope\u4f5c\u7528\u57df<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5355\u4f8b singleton\uff1a<\/strong><\/span><\/p>\n<p>\u5728\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ea\u521b\u5efabean\u7684\u4e00\u4e2a\u5b9e\u4f8b<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u539f\u578b prototype\uff1a<\/strong><\/span><\/p>\n<p>\u6bcf\u6b21\u6ce8\u5165\u6216\u901a\u8fc7spring\u4e0a\u4e0b\u6587\u83b7\u53d6\u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684bean\u5b9e\u4f8b<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u4f1a\u8bdd session\uff1a<\/strong><\/span><\/p>\n<p>\u5728web\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4e3a\u6bcf\u4e2a\u4f1a\u8bdd\u521b\u5efa\u4e00\u4e2abean\u5b9e\u4f8b<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u8bf7\u6c42request\uff1a<\/strong><\/span><\/p>\n<p>\u5728web\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4e3a\u6bcf\u4e2a\u8bf7\u6c42\u521b\u5efa\u4e00\u4e2abean\u5b9e\u4f8b<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5355\u4f8b\u548c\u591a\u4f8b<\/strong><\/span><\/p>\n<p><!--more--><\/p>\n<p>1.\u9ed8\u8ba4\u60c5\u51b5\u4e0bbean\u662f\u5355\u4f8b\u7684\uff0cscope=&#8221;singleton&#8217;;<br \/>\n2.\u591a\u4f8b\u914d\u7f6e\uff1ascope=&#8221;prototype&#8221;;<br \/>\n3.scope=&#8221;request&#8221;;<br \/>\n4.scope=&#8221;session&#8221;;<\/p>\n<pre>&lt;!-- singleton\u662f\u5355\u4f8b \u83b7\u53d6\u7684\u5bf9\u8c61\u90fd\u662f\u540c\u4e00\u4e2a\u5730\u5740 --&gt;\r\n&lt;bean name=\"users2\" class=\"entity.Users\" scope=\"singleton\"&gt;&lt;\/bean&gt;\r\n&lt;!-- prototype \u662f \u591a\u4f8b \u6bcf\u6b21\u6784\u5efa\u5bf9\u8c61\u90fd new\u4e00\u4e2a\u65b0\u7684\u5730\u5740 --&gt;\r\n&lt;bean name=\"users2\" class=\"entity.Users\" scope=\"prototype\"&gt;&lt;\/bean&gt;<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>bean\u521b\u5efa\u6a21\u5f0f<\/strong><\/span><\/p>\n<p>singleton\u5355\u4f8b(\u9ed8\u8ba4)\uff1b<\/p>\n<p>\u5728\u540c\u4e00\u4e2aspring\u5de5\u5382\u4e2d\uff0c\u4e00\u4e2aBean\u53ea\u4f1a\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u3002<br \/>\n\u591a\u6b21getBean()\uff0c\u6216\u591a\u6b21\u6ce8\u5165\u4f7f\u7528\u7684\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<br \/>\n\u968f\u5de5\u5382\u521b\u5efa\u800c\u521b\u5efa\uff0c\u968f\u5de5\u5382\u5173\u95ed\u800c\u9500\u6bc1\u3002<\/p>\n<p><span style=\"color: #ff0000;\">prototype\uff1a\u591a\u4f8b(\u539f\u578b)<\/span><\/p>\n<pre>&lt;bean class=\"bean.Users\" id=\"users\" scope=\"prototype\"\/&gt;<\/pre>\n<p>\u6bcf\u6b21getBean()\uff0c\u6216\u591a\u6b21\u6ce8\u5165\u90fd\u4f1a\u91cd\u65b0\u521b\u5efa\u5bf9\u8c61\u3002<br \/>\n\u4e0d\u968f\u5de5\u5382\u521b\u5efa\u800c\u521b\u5efa\uff0c\u4e0d\u968f\u5de5\u5382\u5173\u95ed\u800c\u9500\u6bc1<br \/>\n\u88ab\u7528\u5230\u65f6\u624d\u4f1a\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u751f\u547d\u5468\u671f<\/strong><\/span><\/p>\n<p>\u5355\u4f8bBean\uff1a\u6784\u9020(\u5de5\u5382\u542f\u52a8)&#8211;&gt;set&#8211;&gt;init&#8211;&gt;User&#8211;&gt;destory(\u9500\u6bc1)<\/p>\n<p>\u591a\u4f8bBean\uff1a\u83b7\u53d6\u65f6\u624d\u521b\u5efa&#8211;&gt;set&#8211;&gt;init&#8211;&gt;User&#8211;&gt;\u4e0d\u4f1a\u968f\u5de5\u5382\u5173\u95ed\u800c\u9500\u6bc1<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5ef6\u8fdf\u521b\u5efa\u5bf9\u8c61(\u5ef6\u8fdf\u52a0\u8f7d)<\/strong><\/span><\/p>\n<p>lazy-init=&#8221;true&#8221; \u5ef6\u8fdf\u521b\u5efa\u5bf9\u8c61\uff0c\u5bb9\u5668\u542f\u52a8\u65f6\u4e0d\u521b\u5efa\uff0c\u83b7\u53d6\u65f6\u518d\u521b\u5efa<\/p>\n<p>lazy-init=&#8221;false&#8221; \u9ed8\u8ba4\u503c\uff0c\u4e0d\u5ef6\u8fdf\u521b\u5efa\u5bf9\u8c61\uff0c\u5bb9\u5668\u542f\u52a8\u65f6\u7acb\u5373\u521b\u5efa<\/p>\n<pre>&lt;bean name=\"users2\" class=\"entity.Users\" scope=\"singleton\" lazy-init=\"true\"&gt;&lt;\/bean&gt;<\/pre>\n<p><span style=\"color: #ff0000;\">\u6ce8\uff1a\u5ef6\u8fdf\u521b\u5efa\u5bf9\u8c61\u53ea\u5bf9 \u5355\u4f8b \u6709\u6548<\/span><\/p>\n<pre>init-method=\"init\" destroy-method=\"destroy<\/pre>\n<p>init\u521d\u59cb\u5316\u65b9\u6cd5\u3001destroy\u9500\u6bc1\u65b9\u6cd5\uff0c\u9700\u8981\u8c03\u7528destroy()\u65b9\u6cd5\u6216close()\u65b9\u6cd5\uff0c\u7136\u540e\u5c06ApplicationContext\u6539\u6210AbstractApplicationContext<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p><strong>\u4f5c\u7528\u57df\u914d\u7f6e\uff1a<\/strong><\/p>\n<p><span style=\"color: #ff0000;\"><strong>XML\u914d\u7f6e\uff1a<\/strong><\/span><br \/>\nscope=&#8221;singleton&#8221;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u81ea\u52a8\u88c5\u914d\uff1a<\/strong><\/span><br \/>\n@component<br \/>\n@Scope(&#8220;singleton&#8221;)<\/p>\n<p><strong><span style=\"color: #ff0000;\">javaConfig\u914d\u7f6e\uff1a<\/span><\/strong><br \/>\n@Bean<br \/>\n@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)<\/p>\n<p><strong>\u5ef6\u8fdf\u52a0\u8f7d<\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">XML\u914d\u7f6e\uff1a<\/span><\/strong><br \/>\nlazy-init=&#8221;true&#8221;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u81ea\u52a8\u88c5\u914d\uff1a<\/strong><\/span><br \/>\n@Component<br \/>\n@Lazy<\/p>\n<p><span style=\"color: #ff0000;\"><strong>JavaConfig\uff1a<\/strong><\/span><br \/>\n@Bean<br \/>\n@Lazy<\/p>\n<p><strong>\u521d\u59cb\u5316\u65b9\u6cd5\u548c\u9500\u6bc1\u65b9\u6cd5<\/strong><\/p>\n<p><span style=\"color: #ff0000;\"><strong>XML\u914d\u7f6e\uff1a<\/strong><\/span><br \/>\ndestroy-method=&#8221;destroy&#8221; init-method=&#8221;init&#8221;<\/p>\n<p><strong><span style=\"color: #ff0000;\">\u81ea\u52a8\u88c5\u914d\u548cJavaConfig<\/span><\/strong><\/p>\n<p>@PostConstrut<br \/>\npublic void init(){<br \/>\nSystem.out.println(&#8220;\u521d\u59cb\u5316\u65b9\u6cd5&#8221;);<br \/>\n}<\/p>\n<p>@PreDestory<br \/>\npublic void destory(){<br \/>\nSystem.out.println(&#8220;\u9500\u6bc1\u65b9\u6cd5&#8221;);<br \/>\n}<\/p>\n<p><strong>\u5de5\u5382\u65b9\u6cd5<\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">\u9759\u6001\u5de5\u5382\uff1a<\/span><\/strong><\/p>\n<p>&lt;bean id=&#8221;users&#8221; class=&#8221;dao.Users&#8221; factory-method=&#8221;crateUsers1&#8243;\/&gt;<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5b9e\u4f8b\u5de5\u5382\uff1a<\/strong><\/span><\/p>\n<p>&lt;bean id=&#8221;users2Factory&#8221; class=&#8221;dao.Users&#8221; \/&gt;<br \/>\n&lt;bean id=&#8221;users2&#8243; factory-bean=&#8221;users2Factory&#8221; factory-method=&#8221;crateUsers2&#8243;\/&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\u4e4bspring\u7684bean\u4f5c\u7528\u57df Scope\u4f5c\u7528\u57df \u5355\u4f8b singleton\uff1a \u5728\u6574\u4e2a\u5e94 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2660","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2660"}],"version-history":[{"count":4,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2660\/revisions"}],"predecessor-version":[{"id":2664,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2660\/revisions\/2664"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}