{"id":174,"date":"2021-04-26T21:35:36","date_gmt":"2021-04-26T13:35:36","guid":{"rendered":"http:\/\/112.124.66.68\/?p=174"},"modified":"2021-04-26T21:35:36","modified_gmt":"2021-04-26T13:35:36","slug":"spring-bean%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2021\/04\/26\/spring-bean%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f\/","title":{"rendered":"spring bean\u7684\u751f\u547d\u5468\u671f"},"content":{"rendered":"\n<p> \u9996\u5148\u8bf4\u4e00\u4e0bServlet\u7684\u751f\u547d\u5468\u671f\uff1a\u5b9e\u4f8b\u5316\uff0c\u521d\u59cbinit\uff0c\u63a5\u6536\u8bf7\u6c42service\uff0c\u9500\u6bc1destroy\uff1b<br>Spring\u4e0a\u4e0b\u6587\u4e2d\u7684Bean\u751f\u547d\u5468\u671f\u4e5f\u7c7b\u4f3c\uff0c\u5982\u4e0b\uff1a<br>\uff081\uff09\u5b9e\u4f8b\u5316Bean\uff1a<br>\u5bf9\u4e8eBeanFactory\u5bb9\u5668\uff0c\u5f53\u5ba2\u6237\u5411\u5bb9\u5668\u8bf7\u6c42\u4e00\u4e2a\u5c1a\u672a\u521d\u59cb\u5316\u7684bean\u65f6\uff0c\u6216\u521d\u59cb\u5316bean\u7684\u65f6\u5019\u9700\u8981\u6ce8\u5165<br>\u53e6\u4e00\u4e2a\u5c1a\u672a\u521d\u59cb\u5316\u7684\u4f9d\u8d56\u65f6\uff0c\u5bb9\u5668\u5c31\u4f1a\u8c03\u7528createBean\u8fdb\u884c\u5b9e\u4f8b\u5316\u3002\u5bf9\u4e8eApplicationContext\u5bb9\u5668\uff0c\u5f53<br>\u5bb9\u5668\u542f\u52a8\u7ed3\u675f\u540e\uff0c\u901a\u8fc7\u83b7\u53d6BeanDefinition\u5bf9\u8c61\u4e2d\u7684\u4fe1\u606f\uff0c\u5b9e\u4f8b\u5316\u6240\u6709\u7684bean\u3002<br>\uff082\uff09\u8bbe\u7f6e\u5bf9\u8c61\u5c5e\u6027\uff08\u4f9d\u8d56\u6ce8\u5165\uff09\uff1a<br>\u5b9e\u4f8b\u5316\u540e\u7684\u5bf9\u8c61\u88ab\u5c01\u88c5\u5728BeanWrapper\u5bf9\u8c61\u4e2d\uff0c\u7d27\u63a5\u7740\uff0cSpring\u6839\u636eBeanDefinition\u4e2d\u7684\u4fe1\u606f \u4ee5\u53ca \u901a<br>\u8fc7BeanWrapper\u63d0\u4f9b\u7684\u8bbe\u7f6e\u5c5e\u6027\u7684\u63a5\u53e3\u5b8c\u6210\u4f9d\u8d56\u6ce8\u5165\u3002<br>\uff083\uff09\u5904\u7406Aware\u63a5\u53e3\uff1a<br>\u63a5\u7740\uff0cSpring\u4f1a\u68c0\u6d4b\u8be5\u5bf9\u8c61\u662f\u5426\u5b9e\u73b0\u4e86xxxAware\u63a5\u53e3\uff0c\u5e76\u5c06\u76f8\u5173\u7684xxxAware\u5b9e\u4f8b\u6ce8\u5165\u7ed9Bean\uff1a<br>\u2460\u5982\u679c\u8fd9\u4e2aBean\u5df2\u7ecf\u5b9e\u73b0\u4e86BeanNameAware\u63a5\u53e3\uff0c\u4f1a\u8c03\u7528\u5b83\u5b9e\u73b0\u7684setBeanName(String beanId)\u65b9<br>\u6cd5\uff0c\u6b64\u5904\u4f20\u9012\u7684\u5c31\u662fSpring\u914d\u7f6e\u6587\u4ef6\u4e2dBean\u7684id\u503c\uff1b<br>\u2461\u5982\u679c\u8fd9\u4e2aBean\u5df2\u7ecf\u5b9e\u73b0\u4e86BeanFactoryAware\u63a5\u53e3\uff0c\u4f1a\u8c03\u7528\u5b83\u5b9e\u73b0\u7684setBeanFactory()\u65b9\u6cd5\uff0c\u4f20\u9012\u7684<br>\u662fSpring\u5de5\u5382\u81ea\u8eab\u3002<br>\u2462\u5982\u679c\u8fd9\u4e2aBean\u5df2\u7ecf\u5b9e\u73b0\u4e86ApplicationContextAware\u63a5\u53e3\uff0c\u4f1a\u8c03\u7528<br>setApplicationContext(ApplicationContext)\u65b9\u6cd5\uff0c\u4f20\u5165Spring\u4e0a\u4e0b\u6587\uff1b<br>\uff084\uff09BeanPostProcessor\uff1a<br>\u5982\u679c\u60f3\u5bf9Bean\u8fdb\u884c\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u5904\u7406\uff0c\u90a3\u4e48\u53ef\u4ee5\u8ba9Bean\u5b9e\u73b0\u4e86BeanPostProcessor\u63a5\u53e3\uff0c\u90a3\u5c06\u4f1a\u8c03\u7528<br>postProcessBeforeInitialization(Object obj, String s)\u65b9\u6cd5\u3002<br>\uff085\uff09InitializingBean \u4e0e init-method\uff1a<br>\u5982\u679cBean\u5728Spring\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u4e86 init-method \u5c5e\u6027\uff0c\u5219\u4f1a\u81ea\u52a8\u8c03\u7528\u5176\u914d\u7f6e\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002<br>\uff086\uff09\u5982\u679c\u8fd9\u4e2aBean\u5b9e\u73b0\u4e86BeanPostProcessor\u63a5\u53e3\uff0c\u5c06\u4f1a\u8c03\u7528postProcessAfterInitialization(Object<br>obj, String s)\u65b9\u6cd5\uff1b\u7531\u4e8e\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728Bean\u521d\u59cb\u5316\u7ed3\u675f\u65f6\u8c03\u7528\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u88ab\u5e94\u7528\u4e8e\u5185\u5b58\u6216\u7f13\u5b58\u6280\u672f\uff1b<br>\u4ee5\u4e0a\u51e0\u4e2a\u6b65\u9aa4\u5b8c\u6210\u540e\uff0cBean\u5c31\u5df2\u7ecf\u88ab\u6b63\u786e\u521b\u5efa\u4e86\uff0c\u4e4b\u540e\u5c31\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2aBean\u4e86\u3002<br>\uff087\uff09DisposableBean\uff1a<br>\u5f53Bean\u4e0d\u518d\u9700\u8981\u65f6\uff0c\u4f1a\u7ecf\u8fc7\u6e05\u7406\u9636\u6bb5\uff0c\u5982\u679cBean\u5b9e\u73b0\u4e86DisposableBean\u8fd9\u4e2a\u63a5\u53e3\uff0c\u4f1a\u8c03\u7528\u5176\u5b9e\u73b0\u7684<br>destroy()\u65b9\u6cd5\uff1b<br>\uff088\uff09destroy-method\uff1a<br>\u6700\u540e\uff0c\u5982\u679c\u8fd9\u4e2aBean\u7684Spring\u914d\u7f6e\u4e2d\u914d\u7f6e\u4e86destroy-method\u5c5e\u6027\uff0c\u4f1a\u81ea\u52a8\u8c03\u7528\u5176\u914d\u7f6e\u7684\u9500\u6bc1\u65b9\u6cd5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u8bf4\u4e00\u4e0bServlet\u7684\u751f\u547d\u5468&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,8,11],"tags":[],"class_list":["post-174","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring","category-springboot","category-springmvc"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media\/171"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}