{"id":10006404,"date":"2022-09-06T01:10:54","date_gmt":"2022-09-05T17:10:54","guid":{"rendered":"https:\/\/ityet.com\/?p=10006404"},"modified":"2022-09-06T01:10:54","modified_gmt":"2022-09-05T17:10:54","slug":"%e9%9d%a2%e8%af%95%e7%aa%81%e5%87%bb80%ef%bc%9a%e8%af%b4%e4%b8%80%e4%b8%8b-spring-%e4%b8%ad-bean-%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006404","title":{"rendered":"\u9762\u8bd5\u7a81\u51fb80\uff1a\u8bf4\u4e00\u4e0b Spring \u4e2d Bean \u7684\u751f\u547d\u5468\u671f\uff1f"},"content":{"rendered":"<p>Java \u4e2d\u7684\u516c\u5171\u7c7b\u79f0\u4e4b\u4e3a Bean \u6216 Java Bean\uff0c\u800c Spring \u4e2d\u7684 Bean \u6307\u7684\u662f\u5c06\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\uff0c\u4ea4\u4e2a Spring IoC \u5bb9\u5668\u6765\u7ba1\u7406\u7684\u5bf9\u8c61\u3002\u6240\u4ee5 Spring \u4e2d\u7684 Bean \u5bf9\u8c61\u5728\u4f7f\u7528\u65f6\uff0c\u65e0\u9700\u901a\u8fc7 new \u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u53ea\u9700\u8981\u901a\u8fc7 DI\uff08\u4f9d\u8d56\u6ce8\u5165\uff09\uff0c\u4ece Spring \u4e2d\u53d6\u51fa\u8981\u4f7f\u7528\u7684\u5bf9\u8c61\u5373\u53ef\u3002<br \/>\u90a3\u4e48 Spring \u4e2d\uff0cBean \u7684\u751f\u547d\u5468\u671f\u53c8\u6709\u54ea\u4e9b\u5462\uff1f\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u3002<\/p>\n<h2>1.Bean \u751f\u547d\u5468\u671f<\/h2>\n<p><strong>Spring \u4e2d Bean \u7684\u751f\u547d\u5468\u671f\u662f\u6307\uff1aBean \u5728 Spring\uff08IoC\uff09\u4e2d\u4ece\u521b\u5efa\u5230\u9500\u6bc1\u7684\u6574\u4e2a\u8fc7\u7a0b\u3002<\/strong><br \/>Spring \u4e2d Bean \u7684\u751f\u547d\u5468\u671f\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b 5 \u90e8\u5206\uff1a<\/p>\n<ol>\n<li>\u5b9e\u4f8b\u5316\uff1a\u4e3a Bean \u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff1b<\/li>\n<li>\u8bbe\u7f6e\u5c5e\u6027\uff1a\u5c06\u5f53\u524d\u7c7b\u4f9d\u8d56\u7684 Bean \u5c5e\u6027\uff0c\u8fdb\u884c\u6ce8\u5165\u548c\u88c5\u914d\uff1b<\/li>\n<li>\n<p>\u521d\u59cb\u5316\uff1a<\/p>\n<ol>\n<li>\u6267\u884c\u5404\u79cd\u901a\u77e5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u524d\u7f6e\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u540e\u7f6e\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\u4f7f\u7528 Bean\uff1a\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Bean \u5bf9\u8c61\uff1b<\/li>\n<li>\u9500\u6bc1 Bean\uff1a\u5c06 Bean \u5bf9\u8c61\u8fdb\u884c\u9500\u6bc1\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u751f\u547d\u5468\u671f\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<strong>\u201c\u5b9e\u4f8b\u5316\u201d\u548c\u201c\u521d\u59cb\u5316\u201d\u662f\u4e24\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u8fc7\u7a0b\uff0c\u5343\u4e07\u4e0d\u8981\u641e\u6df7\uff0c\u5b9e\u4f8b\u5316\u53ea\u662f\u7ed9 Bean \u5206\u914d\u4e86\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u521d\u59cb\u5316\u5219\u662f\u5c06\u7a0b\u5e8f\u7684\u6267\u884c\u6743\uff0c\u4ece\u7cfb\u7edf\u7ea7\u522b\u8f6c\u6362\u5230\u7528\u6237\u7ea7\u522b\uff0c\u5e76\u5f00\u59cb\u6267\u884c\u7528\u6237\u6dfb\u52a0\u7684\u4e1a\u52a1\u4ee3\u7801<\/strong>\u3002<\/p>\n<h2>2.\u4ee3\u7801\u6f14\u793a<\/h2>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u4f7f\u7528\u4ee3\u7801\u7684\u65b9\u5f0f\u5728 Spring Boot \u4e2d\uff0c\u7ed9\u5927\u5bb6\u6f14\u793a\u4e00\u4e0b Bean \u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<blockquote><p>PS\uff1a\u56e0\u4e3a Spring Boot \u662f\u57fa\u4e8e Spring \u521b\u5efa\u7684\uff0c\u6240\u4ee5 Bean \u5728 Spring \u6216 Spring Boot \u4e2d\u7684\u884c\u4e3a\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u800c Spring Boot \u53c8\u662f\u76ee\u524d\u4e3b\u6d41\u7684\u6846\u67b6\uff0c\u6240\u4ee5\u672c\u6587\u4f7f\u7528 Spring Boot \u6765\u6f14\u793a Bean \u7684\u751f\u547d\u5468\u671f\u3002<\/p><\/blockquote>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a Bean \u5bf9\u8c61\uff0c\u8d77\u540d\u4e3a BeanLifeComponent\uff08\u7c7b\u547d\u65e0\u6240\u8c13\uff0c\u53ef\u968f\u610f\u6307\u5b9a\uff09\uff0c\u5b83\u7684\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.beans.factory.BeanNameAware;\nimport org.springframework.stereotype.Component;\n\nimport javax.annotation.PostConstruct;\nimport javax.annotation.PreDestroy;\n\n@Component\npublic class BeanLifeComponent implements BeanNameAware {\n    public void setBeanName(String s) {\n        System.out.println(&quot;\u6267\u884c BeanName \u7684\u901a\u77e5\u65b9\u6cd5&quot;);\n    }\n\n    @PostConstruct\n    public void postConstruct() {\n        System.out.println(&quot;\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5&quot;);\n    }\n\n    public void use() {\n        System.out.println(&quot;\u4f7f\u7528 Bean&quot;);\n    }\n\n    @PreDestroy\n    public void preDestroy() {\n        System.out.println(&quot;\u6267\u884c\u9500\u6bc1\u65b9\u6cd5&quot;);\n    }\n}<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u518d\u521b\u5efa\u4e00\u4e2a MyBeanPostProcessor \u7c7b\uff08\u7c7b\u547d\u65e0\u6240\u8c13\uff0c\u53ef\u968f\u610f\u6307\u5b9a\uff09\uff0c\u6765\u5b9e\u73b0\u521d\u59cb\u5316\u7684\u524d\u7f6e\u65b9\u6cd5\u548c\u521d\u59cb\u5316\u7684\u540e\u7f6e\u65b9\u6cd5\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.beans.BeansException;\nimport org.springframework.beans.factory.config.BeanPostProcessor;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class MyBeanPostProcessor implements BeanPostProcessor {\n    @Override\n    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {\n        if (beanName.equals(&quot;beanLifeComponent&quot;)) {\n            System.out.println(&quot;\u6267\u884c\u521d\u59cb\u5316\u524d\u7f6e\u65b9\u6cd5&quot;);\n        }\n        return bean;\n    }\n\n    @Override\n    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {\n        if (beanName.equals(&quot;beanLifeComponent&quot;)) {\n            System.out.println(&quot;\u6267\u884c\u521d\u59cb\u5316\u540e\u7f6e\u65b9\u6cd5&quot;);\n        }\n        return bean;\n    }\n}<\/code><\/pre>\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u7c7b\u6765\u6267\u884c\u521d\u59cb\u5316\u7684\u524d\u7f6e\u65b9\u6cd5\u548c\u521d\u59cb\u5316\u7684\u540e\u7f6e\u65b9\u6cd5\u5462\uff1f<\/strong><br \/><strong>\u8fd9\u662f\u56e0\u4e3a\u521d\u59cb\u5316\u7684\u524d\u7f6e\u65b9\u6cd5\u548c\u540e\u7f6e\u65b9\u6cd5\u662f\u4e3a\u6240\u6709 Bean \u670d\u52a1\u7684\uff0c\u800c\u975e\u4e3a\u67d0\u4e00\u4e2a Bean \u670d\u52a1\u7684\uff0c\u6240\u4ee5\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u4e0d\u80fd\u5199\u5728\u67d0\u4e2a\u5177\u4f53\u7684 Bean \u4e2d\uff0c\u5426\u5219\uff08\u8fd9\u4e24\u4e2a\u65b9\u6cd5\uff09\u4e0d\u4f1a\u6267\u884c\u3002<\/strong><br \/>\u6700\u540e\uff0c\u5728 Spring Boot \u7684\u542f\u52a8\u7c7b\u4e2d\u83b7\u53d6 Bean\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.context.ConfigurableApplicationContext;\n\n@SpringBootApplication\npublic class DemoApplication {\n    public static void main(String[] args) {\n        \/\/ \u5f97\u5230\u4e0a\u4e0b\u6587\u5bf9\u8c61\uff0c\u5e76\u542f\u52a8 Spring Boot \u9879\u76ee\n        ConfigurableApplicationContext context = \n            SpringApplication.run(DemoApplication.class, args);\n        \/\/ \u83b7\u53d6 Bean\n        BeanLifeComponent component = context.getBean(BeanLifeComponent.class);\n        \/\/ \u4f7f\u7528 Bean\n        component.use();\n        \/\/ \u505c\u6b62 Spring Boot \u9879\u76ee\n        context.close();\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6700\u7ec8\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042435875\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u4ece\u4e0a\u9762\u7684\u6267\u884c\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u4ee3\u7801\u6267\u884c\u987a\u5e8f\u7b26\u5408 Bean \u751f\u547d\u5468\u671f\u7684\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n<ol>\n<li>\u5b9e\u4f8b\u5316\uff1a\u4e3a Bean \u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff1b<\/li>\n<li>\u8bbe\u7f6e\u5c5e\u6027\uff1a\u5c06\u5f53\u524d\u7c7b\u4f9d\u8d56\u7684 Bean \u5c5e\u6027\uff0c\u8fdb\u884c\u6ce8\u5165\u548c\u88c5\u914d\uff1b<\/li>\n<li>\n<p>\u521d\u59cb\u5316\uff1a<\/p>\n<ol>\n<li>\u6267\u884c\u5404\u79cd\u901a\u77e5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u524d\u7f6e\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u540e\u7f6e\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\u4f7f\u7528 Bean\uff1a\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Bean \u5bf9\u8c61\uff1b<\/li>\n<li>\u9500\u6bc1 Bean\uff1a\u5c06 Bean \u5bf9\u8c61\u8fdb\u884c\u9500\u6bc1\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p>\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u80fd\u4e0d\u80fd\u5148\u6267\u884c\u521d\u59cb\u5316\u518d\u6267\u884c\u8bbe\u7f6e\u5c5e\u6027\u5462\uff1f\u4e5f\u5c31\u662f\u5c06\u751f\u547d\u5468\u671f\u4e2d\u7684\u6b65\u9aa4 2 \u548c\u6b65\u9aa4 3 \u7684\u6267\u884c\u987a\u5e8f\u4ea4\u6362\u4e00\u4e0b\uff1f<br \/>\u7b54\u6848\u662f\u5426\u5b9a\u7684\u3002\u60f3\u8c61\u4e00\u4e2a\u573a\u666f\uff0c\u5982\u679c\u5728\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u8981\u7528\u5230\u88ab\u6ce8\u5165\u5bf9\u8c61\u7684\u67d0\u4e2a\u65b9\u6cd5\uff0c\u6bd4\u5982\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">@Controller\npublic class UserController {\n    @Resource\n    private UserService userService;\n\n    @PostConstruct \/\/ \u521d\u59cb\u5316\u65b9\u6cd5\n    public void postConstruct() {\n        userService.sayHi();\n    }\n}<\/code><\/pre>\n<p>\u6b64\u65f6\u5982\u679c\u5148\u6267\u884c\u6b65\u9aa4 2\uff0c\u5148\u5c06 UserService \u6ce8\u5165\u5230\u5f53\u524d\u7c7b\uff0c\u518d\u8c03\u7528\u6b65\u9aa4 3 \u6267\u884c\u521d\u59cb\u5316\uff0c\u90a3\u4e48\u7a0b\u5e8f\u7684\u6267\u884c\u662f\u6b63\u5e38\u7684\u3002\u7136\u800c\u5982\u679c\u5c06\u4ea4\u4e92\u6b65\u9aa4 2 \u548c\u6b65\u9aa4 3 \u7684\u6267\u884c\u987a\u5e8f\uff0c\u90a3\u4e48\u7a0b\u5e8f\u6267\u884c\u5c31\u4f1a\u62a5\u9519\uff08\u7a7a\u6307\u9488\u5f02\u5e38\uff09\uff0c\u6240\u4ee5 Bean \u7684\u751f\u547d\u5468\u671f\u7684\u987a\u5e8f\u5fc5\u987b\u662f\uff1a<\/p>\n<p>1.\u5b9e\u4f8b\u5316\uff1a\u4e3a Bean \u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff1b<br \/>2.\u8bbe\u7f6e\u5c5e\u6027\uff1a\u5c06\u5f53\u524d\u7c7b\u4f9d\u8d56\u7684 Bean \u5c5e\u6027\uff0c\u8fdb\u884c\u6ce8\u5165\u548c\u88c5\u914d\uff1b<br \/>3.\u521d\u59cb\u5316\uff1a<\/p>\n<ol>\n<li>\u6267\u884c\u5404\u79cd\u901a\u77e5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u524d\u7f6e\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u540e\u7f6e\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p>4.\u4f7f\u7528 Bean\uff1a\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Bean \u5bf9\u8c61\uff1b<br \/>5.\u9500\u6bc1 Bean\uff1a\u5c06 Bean \u5bf9\u8c61\u8fdb\u884c\u9500\u6bc1\u64cd\u4f5c\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>Bean \u7684\u751f\u547d\u5468\u671f\u6307\u7684\u662f Bean \u5728 Spring\uff08IoC\uff09\u4e2d\u4ece\u521b\u5efa\u5230\u9500\u6bc1\u7684\u6574\u4e2a\u8fc7\u7a0b\u3002Bean \u7684\u751f\u547d\u5468\u671f\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b 5 \u4e2a\u6d41\u7a0b\uff1a<br \/>1.\u5b9e\u4f8b\u5316\uff1a\u4e3a Bean \u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff1b<br \/>2.\u8bbe\u7f6e\u5c5e\u6027\uff1a\u5c06\u5f53\u524d\u7c7b\u4f9d\u8d56\u7684 Bean \u5c5e\u6027\uff0c\u8fdb\u884c\u6ce8\u5165\u548c\u88c5\u914d\uff1b<br \/>3.\u521d\u59cb\u5316\uff1a<\/p>\n<ol>\n<li>\u6267\u884c\u5404\u79cd\u901a\u77e5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u524d\u7f6e\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff1b<\/li>\n<li>\u6267\u884c\u521d\u59cb\u5316\u7684\u540e\u7f6e\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p>4.\u4f7f\u7528 Bean\uff1a\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Bean \u5bf9\u8c61\uff1b<br \/>5.\u9500\u6bc1 Bean\uff1a\u5c06 Bean \u5bf9\u8c61\u8fdb\u884c\u9500\u6bc1\u64cd\u4f5c\u3002<\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u9762\u8bd5\u771f\u9898\u89e3\u6790<\/p>\n<p>\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=1nIKBI7x8VpEjih21GlMDQ%3D%3D.qmVFC1IfgAx9hbGwgLuzhEhUzCEODS9sYRJRXQBL9XbN11hBDNFDat3Zg5yxjAcH\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042435873\">https:\/\/segmentfault.com\/a\/1190000042435873<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java \u4e2d\u7684\u516c\u5171\u7c7b\u79f0\u4e4b\u4e3a Bean \u6216 Java Bean\uff0c\u800c Spring \u4e2d\u7684 Bean \u6307\u7684\u662f\u5c06\u5bf9\u8c61\u7684 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006404","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006404"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006404\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}