{"id":289,"date":"2022-04-06T19:28:30","date_gmt":"2022-04-06T11:28:30","guid":{"rendered":"https:\/\/blog.lijiajia.top\/?p=289"},"modified":"2026-06-19T21:40:23","modified_gmt":"2026-06-19T13:40:23","slug":"spring%e6%a1%86%e6%9e%b6%e5%ad%a6%e4%b9%a0%ef%bc%9aioc%e6%93%8d%e4%bd%9cbean%e7%ae%a1%e7%90%86-bean%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e4%b8%83","status":"publish","type":"post","link":"https:\/\/blog.ttwow.top\/?p=289","title":{"rendered":"Spring\u6846\u67b6\u5b66\u4e60\uff1aIOC\u64cd\u4f5cBean\u7ba1\u7406-Bean\u7684\u751f\u547d\u5468\u671f(\u4e03)"},"content":{"rendered":"<p>Spring Bean \u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s\/v3eWiHIbAUOcHd5N5D4nuA\">https:\/\/mp.weixin.qq.com\/s\/v3eWiHIbAUOcHd5N5D4nuA<\/a><\/p>\n<h2>1\u3001\u751f\u547d\u5468\u671f<\/h2>\n<p>(1)\u4ece\u5bf9\u8c61\u521b\u5efa\u5230\u5bf9\u8c61\u9500\u6bc1\u7684\u8fc7\u7a0b<\/p>\n<h2>2\u3001bean\u751f\u547d\u5468\u671f<\/h2>\n<p>(1)\u901a\u8fc7\u6784\u9020\u5668\u521b\u5efabean\u5b9e\u4f8b\uff08\u65e0\u53c2\u6570\u6784\u9020\uff09<br \/>\n(2)\u4e3abean\u7684\u5c5e\u6027\u8bbe\u7f6e\u503c\u548c\u5bf9\u5176\u4ed6bean\u5f15\u7528\uff08\u8c03\u7528set\u65b9\u6cd5\uff09<br \/>\n(3)\u8c03\u7528bean\u7684\u521d\u59cb\u5316\u7684\u65b9\u6cd5\uff08\u9700\u8981\u8fdb\u884c\u914d\u7f6e\u521d\u59cb\u5316\u7684\u65b9\u6cd5\uff09<br \/>\n(4)bean\u53ef\u4ee5\u4f7f\u7528\u4e86\uff08\u5bf9\u8c61\u83b7\u53d6\u5230\u4e86\uff09<br \/>\n(5)\u5f53\u5bb9\u5668\u5173\u95ed\u65f6\u5019\uff0c\u8c03\u7528bean\u7684\u9500\u6bc1\u7684\u65b9\u6cd5\uff08\u9700\u8981\u8fdb\u884c\u914d\u7f6e\u9500\u6bc1\u7684\u65b9\u6cd5\uff09<\/p>\n<pre><code class=\"language-java\">public class Orders {\n\n    \/\/\u65e0\u53c2\u6570\u6784\u9020\n    public Orders() {\n        System.out.println(&quot;\u7b2c\u4e00\u6b65 \u6267\u884c\u65e0\u53c2\u6570\u6784\u9020\u521b\u5efabean\u5b9e\u4f8b&quot;);\n    }\n\n    private String oname;\n\n    public void setOname(String oname) {\n        this.oname = oname;\n        System.out.println(&quot;\u7b2c\u4e8c\u6b65 \u8c03\u7528set\u65b9\u6cd5\u8bbe\u7f6e\u5c5e\u6027\u503c&quot;);\n    }\n\n    public void initMethod(){\n        System.out.println(&quot;\u7b2c\u4e09\u6b65 \u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5&quot;);\n    }\n    public void destroyMethod(){\n        System.out.println(&quot;\u7b2c\u4e94\u6b65 \u6267\u884c\u9500\u6bc1\u65b9\u6cd5&quot;);\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;orders&quot; class=&quot;com.spring5.bean.Orders&quot; init-method=&quot;initMethod&quot; destroy-method=&quot;destroyMethod&quot;&gt;\n        &lt;property name=&quot;oname&quot; value=&quot;\u624b\u673a&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;<\/code><\/pre>\n<p>junit\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-java\">  @Test\n    public void test4(){\n\/\/        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&quot;bean4.xml&quot;);\n        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(&quot;bean4.xml&quot;);\n        Orders orders = applicationContext.getBean(&quot;orders&quot;, Orders.class);\n        System.out.println(&quot;\u7b2c\u56db\u6b65 \u83b7\u53d6\u521b\u5efa\u7684bean\u5b9e\u4f8b\u5bf9\u8c61&quot;);\n        System.out.println(orders);\n\n        \/\/\u624b\u52a8\u8ba9bean\u5b9e\u4f8b\u9500\u6bc1\n        applicationContext.close();\n    }<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-java\">\u7b2c\u4e00\u6b65 \u6267\u884c\u65e0\u53c2\u6570\u6784\u9020\u521b\u5efabean\u5b9e\u4f8b\n\u7b2c\u4e8c\u6b65 \u8c03\u7528set\u65b9\u6cd5\u8bbe\u7f6e\u5c5e\u6027\u503c\n\u7b2c\u4e09\u6b65 \u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\n\u7b2c\u56db\u6b65 \u83b7\u53d6\u521b\u5efa\u7684bean\u5b9e\u4f8b\u5bf9\u8c61\ncom.spring5.bean.Orders@396e2f39\n\u7b2c\u4e94\u6b65 \u6267\u884c\u9500\u6bc1\u65b9\u6cd5<\/code><\/pre>\n<h2>4\u3001bean\u7684\u540e\u7f6e\u5904\u7406\u5668\uff0cbean\u751f\u547d\u5468\u671f\u6709\u4e03\u6b65<\/h2>\n<p>\u5f53\u914d\u7f6e\u540e\u7f6e\u5904\u7406bean\uff0c\u4f1a\u5728bean\u521d\u59cb\u5316\u524d\u540e\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u4e0d\u914d\u7f6e\uff0c\u4e0d\u6267\u884c<br \/>\n(1)\u901a\u8fc7\u6784\u9020\u5668\u521b\u5efabean\u5b9e\u4f8b\uff08\u65e0\u53c2\u6570\u6784\u9020\uff09<br \/>\n(2)\u4e3abean\u7684\u5c5e\u6027\u8bbe\u7f6e\u503c\u548c\u5bf9\u5176\u4ed6bean\u5f15\u7528\uff08\u8c03\u7528set\u65b9\u6cd5\uff09<br \/>\n(3)\u628abean\u5b9e\u4f8b\u4f20\u9012bean\u540e\u7f6e\u5904\u7406\u5668\u7684\u65b9\u6cd5postProcessBeforeInitialization<br \/>\n(4)\u8c03\u7528bean\u7684\u521d\u59cb\u5316\u7684\u65b9\u6cd5\uff08\u9700\u8981\u8fdb\u884c\u914d\u7f6e\u521d\u59cb\u5316\u7684\u65b9\u6cd5\uff09<br \/>\n(5)\u628abean\u5b9e\u4f8b\u4f20\u9012bean\u540e\u7f6e\u5904\u7406\u5668\u7684\u65b9\u6cd5postProcessAfterInitialization<br \/>\n(6)bean\u53ef\u4ee5\u4f7f\u7528\u4e86\uff08\u5bf9\u8c61\u83b7\u53d6\u5230\u4e86\uff09<br \/>\n(7)\u5f53\u5bb9\u5668\u5173\u95ed\u65f6\u5019\uff0c\u8c03\u7528bean\u7684\u9500\u6bc1\u7684\u65b9\u6cd5\uff08\u9700\u8981\u8fdb\u884c\u914d\u7f6e\u9500\u6bc1\u7684\u65b9\u6cd5\uff09<\/p>\n<p>\u5b9e\u73b0BeanPostProcessor\u63a5\u53e3\uff0c\u521b\u5efa\u540e\u7f6e\u5904\u7406\u5668<\/p>\n<pre><code class=\"language-java\">public class MyBeanPost implements BeanPostProcessor {\n\n    @Override\n    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {\n        System.out.println(&quot;\u5728\u521d\u59cb\u5316\u6267\u884c\u4e4b\u524d \u6267\u884c\u7684\u65b9\u6cd5&quot;);\n        return bean;\n    }\n\n    @Override\n    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {\n        System.out.println(&quot;\u5728\u521d\u59cb\u5316\u6267\u884c\u4e4b\u540e \u6267\u884c\u7684\u65b9\u6cd5&quot;);\n        return bean;\n    }\n\n}<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;bean id=&quot;orders&quot; class=&quot;com.spring5.bean.Orders&quot; init-method=&quot;initMethod&quot; destroy-method=&quot;destroyMethod&quot;&gt;\n        &lt;property name=&quot;oname&quot; value=&quot;\u624b\u673a&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n    &lt;!--\u672cxml\u7684bean\u521d\u59cb\u5316\u524d\u540e\u90fd\u4f1a\u6267\u884c\u7684\u540e\u7f6e\u5904\u7406\u5668--&gt;\n    &lt;bean id=&quot;beanPost&quot; class=&quot;com.spring5.bean.MyBeanPost&quot;&gt;&lt;\/bean&gt;<\/code><\/pre>\n<p>junit\u6d4b\u8bd5\uff1a<\/p>\n<pre><code class=\"language-java\"> @Test\n    public void test4(){\n\/\/        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&quot;bean4.xml&quot;);\n        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(&quot;bean4.xml&quot;);\n        Orders orders = applicationContext.getBean(&quot;orders&quot;, Orders.class);\n        System.out.println(&quot;\u7b2c\u56db\u6b65 \u83b7\u53d6\u521b\u5efa\u7684bean\u5b9e\u4f8b\u5bf9\u8c61&quot;);\n        System.out.println(orders);\n\n        \/\/\u624b\u52a8\u8ba9bean\u5b9e\u4f8b\u9500\u6bc1\n        applicationContext.close();\n    }<\/code><\/pre>\n<p>\u6267\u884c\u7ed3\u679c<\/p>\n<pre><code class=\"language-java\">\u7b2c\u4e00\u6b65 \u6267\u884c\u65e0\u53c2\u6570\u6784\u9020\u521b\u5efabean\u5b9e\u4f8b\n\u7b2c\u4e8c\u6b65 \u8c03\u7528set\u65b9\u6cd5\u8bbe\u7f6e\u5c5e\u6027\u503c\n\u5728\u521d\u59cb\u5316\u6267\u884c\u4e4b\u524d \u6267\u884c\u7684\u65b9\u6cd5\n\u7b2c\u4e09\u6b65 \u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\n\u5728\u521d\u59cb\u5316\u6267\u884c\u4e4b\u540e \u6267\u884c\u7684\u65b9\u6cd5\n\u7b2c\u56db\u6b65 \u83b7\u53d6\u521b\u5efa\u7684bean\u5b9e\u4f8b\u5bf9\u8c61\ncom.spring5.bean.Orders@7e0e6aa2\n\u7b2c\u4e94\u6b65 \u6267\u884c\u9500\u6bc1\u65b9\u6cd5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring Bean \u7684\u5b9e\u4f8b\u5316\u8fc7\u7a0b\uff1ahttps:\/\/mp.weixin.qq.com\/s\/v3eWiHIbA [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[125],"tags":[68],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-java-backend","tag-spring"],"_links":{"self":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289"}],"version-history":[{"count":1,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":2160,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions\/2160"}],"wp:attachment":[{"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ttwow.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}