{"id":11588,"date":"2023-11-28T22:26:53","date_gmt":"2023-11-28T14:26:53","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11588"},"modified":"2023-12-11T11:25:29","modified_gmt":"2023-12-11T03:25:29","slug":"spring-zhong-na-xie-shi-zhong-yao-debean-sheng-min","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-na-xie-shi-zhong-yao-debean-sheng-min\/","title":{"rendered":"Spring\u4e2d\u54ea\u4e9b\u662f\u91cd\u8981\u7684bean\u751f\u547d\u5468\u671f\u65b9\u6cd5\uff1f\u4f60\u80fd\u91cd\u8f7d\u5b83\u4eec\u5417\uff1f"},"content":{"rendered":"<p>\u5728Spring\u4e2d\uff0cBean\u7684\u751f\u547d\u5468\u671f\u7531\u4e00\u7cfb\u5217\u56de\u8c03\u65b9\u6cd5\u7ec4\u6210\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728Bean\u7684\u521b\u5efa\u3001\u521d\u59cb\u5316\u548c\u9500\u6bc1\u9636\u6bb5\u6267\u884c\u81ea\u5b9a\u4e49\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u91cd\u8981\u7684Bean\u751f\u547d\u5468\u671f\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<strong>\u521d\u59cb\u5316\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li><strong><code>afterPropertiesSet<\/code>\u65b9\u6cd5\uff1a<\/strong> \u8be5\u65b9\u6cd5\u5728Bean\u7684\u6240\u6709\u5c5e\u6027\u88ab\u8bbe\u7f6e\u4e4b\u540e\u3001\u4efb\u4f55\u914d\u7f6e\u56de\u8c03\uff08\u4f8b\u5982<code>Aware<\/code>\u63a5\u53e3\uff09\u88ab\u8c03\u7528\u4e4b\u540e\u88ab\u8c03\u7528\u3002\u5b83\u662f<code>InitializingBean<\/code>\u63a5\u53e3\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.InitializingBean;\n\npublic class MyBean implements InitializingBean {\n    @Override\n    public void afterPropertiesSet() throws Exception {\n        \/\/ \u6267\u884c\u521d\u59cb\u5316\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<ul>\n<li><strong><code>@PostConstruct<\/code>\u6ce8\u89e3\uff1a<\/strong> \u901a\u8fc7\u5728\u81ea\u5b9a\u4e49\u65b9\u6cd5\u4e0a\u6dfb\u52a0<code>@PostConstruct<\/code>\u6ce8\u89e3\uff0c\u53ef\u4ee5\u5c06\u8be5\u65b9\u6cd5\u6807\u8bc6\u4e3aBean\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import javax.annotation.PostConstruct;\n\npublic class MyBean {\n    @PostConstruct\n    public void customInit() {\n        \/\/ \u6267\u884c\u521d\u59cb\u5316\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<ul>\n<li><strong>\u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5\uff1a<\/strong> \u5728XML\u914d\u7f6e\u6216Java\u914d\u7f6e\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>init-method<\/code>\u5c5e\u6027\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot; init-method=&quot;customInit&quot; \/&gt;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u9500\u6bc1\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li><strong><code>destroy<\/code>\u65b9\u6cd5\uff1a<\/strong> \u8be5\u65b9\u6cd5\u5728Bean\u9500\u6bc1\u4e4b\u524d\u88ab\u8c03\u7528\u3002\u5b83\u662f<code>DisposableBean<\/code>\u63a5\u53e3\u7684\u4e00\u90e8\u5206\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import org.springframework.beans.factory.DisposableBean;\n\npublic class MyBean implements DisposableBean {\n    @Override\n    public void destroy() throws Exception {\n        \/\/ \u6267\u884c\u9500\u6bc1\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<ul>\n<li><strong><code>@PreDestroy<\/code>\u6ce8\u89e3\uff1a<\/strong> \u901a\u8fc7\u5728\u81ea\u5b9a\u4e49\u65b9\u6cd5\u4e0a\u6dfb\u52a0<code>@PreDestroy<\/code>\u6ce8\u89e3\uff0c\u53ef\u4ee5\u5c06\u8be5\u65b9\u6cd5\u6807\u8bc6\u4e3aBean\u7684\u9500\u6bc1\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">import javax.annotation.PreDestroy;\n\npublic class MyBean {\n    @PreDestroy\n    public void customDestroy() {\n        \/\/ \u6267\u884c\u9500\u6bc1\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<ul>\n<li><strong>\u81ea\u5b9a\u4e49\u9500\u6bc1\u65b9\u6cd5\uff1a<\/strong> \u5728XML\u914d\u7f6e\u6216Java\u914d\u7f6e\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>destroy-method<\/code>\u5c5e\u6027\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u9500\u6bc1\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;myBean&quot; class=&quot;com.example.MyBean&quot; destroy-method=&quot;customDestroy&quot; \/&gt;\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u6216\u7ec4\u5408\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528<code>@PostConstruct<\/code>\u548c<code>@PreDestroy<\/code>\u6ce8\u89e3\u6216XML\u914d\u7f6e\u7684\u65b9\u5f0f\u6bd4\u8f83\u5e38\u89c1\u3002\u5982\u679cBean\u5b9e\u73b0\u4e86<code>InitializingBean<\/code>\u548c<code>DisposableBean<\/code>\u63a5\u53e3\uff0c\u90a3\u4e48<code>afterPropertiesSet<\/code>\u548c<code>destroy<\/code>\u65b9\u6cd5\u4e5f\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring\u4e2d\uff0cBean\u7684\u751f\u547d\u5468\u671f\u7531\u4e00\u7cfb\u5217\u56de\u8c03\u65b9\u6cd5\u7ec4\u6210\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728Bean\u7684\u521b\u5efa\u3001\u521d\u59cb\u5316\u548c\u9500\u6bc1\u9636\u6bb5\u6267\u884c\u81ea\u5b9a [&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-11588","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11588","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=11588"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11588\/revisions"}],"predecessor-version":[{"id":17086,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11588\/revisions\/17086"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11588"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}