{"id":279,"date":"2021-05-09T22:24:49","date_gmt":"2021-05-09T14:24:49","guid":{"rendered":"https:\/\/explause.com\/?p=279"},"modified":"2022-07-04T21:29:00","modified_gmt":"2022-07-04T13:29:00","slug":"%e5%9f%ba%e4%ba%8ejava%e8%bf%9b%e8%a1%8cspring%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/devdoge.org\/?p=279","title":{"rendered":"\u57fa\u4e8eJava\u4f7f\u7528\u6ce8\u89e3\u8fdb\u884cSpring\u914d\u7f6e"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5c31\u662f\u4f7f\u7528\u6ce8\u89e3\u8fdb\u884c\u914d\u7f6e\uff0c\u4e0d\u9700\u8981Beans.xml\u6587\u4ef6\u3002\u5728<strong>Spring Boot<\/strong>\u4e2d\u5f88\u5e38\u7528\u3002\u5e38\u89c1\u7684\u6ce8\u89e3\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter is-style-stripes\"><table><tbody><tr><td>@Required<\/td><td>\u4f5c\u7528\u4e8esetter\u65b9\u6cd5\uff0c\u6307\u5b9a\u5c5e\u6027\u5fc5\u987b\u5b58\u5728\u4e8eBeans.xml\uff08\u5df2\u5e9f\u5f03\uff09<\/td><\/tr><tr><td>@Autowired&nbsp;<\/td><td>\u63a7\u5236\u81ea\u52a8\u88c5\u914d<\/td><\/tr><tr><td>@Qualifier<\/td><td>\u6709\u591a\u4e2a\u5339\u914d\u6761\u4ef6\u7684bean\u65f6\u9650\u5b9a\u4e00\u4e2a\u4f7f\u7528<\/td><\/tr><tr><td>@Configuration<\/td><td>\u4f5c\u7528\u4e8eclass\u544a\u8bc9IoC\u5bb9\u5668\u8fd9\u662fbean\u5b9a\u4e49\u6e90<\/td><\/tr><tr><td>@Bean<\/td><td>\u4f5c\u7528\u4e8e\u65b9\u6cd5\uff0c\u544a\u8bc9Spring\u5b83\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5c06\u88ab\u6ce8\u518c\u4e3abean<\/td><\/tr><tr><td>@Import<\/td><td>\u5141\u8bb8\u4ece\u53e6\u5916\u4e00\u4e2aclass\u91cc\u9762\u5bfc\u5165bean<\/td><\/tr><tr><td>@Scope<\/td><td>\u5b9a\u4e49bean\u7684\u4f5c\u7528\u57df<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148<a href=\"https:\/\/explause.com\/?p=221\">\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee<\/a>\u6216\u8005<a href=\"https:\/\/explause.com\/wp-content\/uploads\/2022\/07\/AnnotationConfigExamples.zip\">\u4e0b\u8f7d\u6211\u505a\u597d\u7684\u793a\u4f8b<\/a>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u521b\u5efa3\u4e2aBean\u7684\u5b9a\u4e49\uff0cHelloWorld.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AnnotationConfigExamples;\r\n\r\npublic class HelloWorld {\r\n\tprivate String message;\r\n\tprivate HelloWorld2 hw2;\r\n\t\r\n\tpublic void setMessage(String message){\r\n\t\tthis.message  = message;\r\n\t}\r\n\tpublic void getMessage(){\r\n\t\tSystem.out.println(\"Your Message : \" + message);\r\n\t\thw2.getMessage();\r\n\t}\r\n\tpublic HelloWorld(HelloWorld2 hw2) {\r\n\t\tthis.hw2 = hw2;\r\n\t}\r\n\tpublic void initMethod() {\r\n\t\tSystem.out.println(\"This is the initMethod in HelloWorld.\");\r\n\t}\r\n\tpublic void destroyMethod() {\r\n\t\tSystem.out.println(\"This is the destroyMethod in HelloWorld.\");\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">HelloWorld2.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AnnotationConfigExamples;\r\n\r\npublic class HelloWorld2 {\r\n\tprivate String message;\r\n\tprivate String message2;\r\n\t\r\n\tpublic void setMessage(String message){\r\n\t\tthis.message  = message;\r\n\t}\r\n\tpublic void getMessage(){\r\n\t\tSystem.out.println(\"Your Message : \" + message);\r\n\t\tSystem.out.println(\"Your Message 2: \" + message2);\r\n\t}\r\n\tpublic void setMessage2(String message2){\r\n\t\tthis.message2  = message2;\r\n\t}\r\n\t\r\n\tpublic void initMethod() {\r\n\t\tSystem.out.println(\"This is the initMethod in HelloWorld2.\");\r\n\t}\r\n\tpublic void destroyMethod() {\r\n\t\tSystem.out.println(\"This is the destroyMethod in HelloWorld2.\");\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">AutowiredBean.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AnnotationConfigExamples;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\n\r\npublic class AutowiredBean {\r\n\t@Autowired\r\n\tprivate HelloWorld hw;\r\n\t@Autowired\r\n\t@Qualifier(\"hw3\")\r\n\tprivate HelloWorld2 hw2;\r\n\t\r\n\tpublic void setHelloWorld(HelloWorld hw) {\r\n\t\tthis.hw = hw;\r\n\t}\r\n\tpublic void setHelloWorld2(HelloWorld2 hw2) {\r\n\t\tthis.hw2 = hw2;\r\n\t}\r\n\tpublic HelloWorld getHelloWorld() {\r\n\t\treturn hw;\r\n\t}\r\n\tpublic HelloWorld2 getHelloWorld2() {\r\n\t\treturn hw2;\r\n\t}\r\n\tpublic void getMessages() {\n\r        System.out.println(\"Inside the AutowiredBean.\");\n\t\thw.getMessage();\r\n\t\thw2.getMessage();\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">@Qualifier(&#8220;hw3&#8221;)\u6ce8\u89e3\u7684\u610f\u601d\u662f\u5728\u5bb9\u5668\u5185\u9009\u53d6\u540d\u5b57\u53ebhw3\u7684Bean\uff0c\u4e0b\u9762\u4f1a\u521b\u5efa\u4e24\u4e2a\u7c7b\u578b\u4e3aHelloWorld2\u7684Bean\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bean\u914d\u7f6e\u6587\u4ef6<\/strong>\uff0cBeanConfig.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AnnotationConfigExamples;\r\n\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.context.annotation.Import;\r\nimport org.springframework.context.annotation.Scope;\r\n\r\n@Configuration\r\n@Import({ AnotherBeanConfig.class })\r\npublic class BeanConfig {\r\n\t@Scope(\"singleton\")\r\n\t@Bean(initMethod=\"initMethod\", destroyMethod=\"destroyMethod\")\r\n\tpublic HelloWorld hw(HelloWorld2 hw2) {\r\n\t\tHelloWorld hw = new HelloWorld(hw2);\r\n\t\thw.setMessage(\"message in hw\");\r\n\t\treturn hw;\r\n\t}\r\n\t\r\n\t@Bean(initMethod=\"initMethod\", destroyMethod=\"destroyMethod\")\r\n\tpublic HelloWorld2 hw2() {\r\n\t\tHelloWorld2 hw2 = new HelloWorld2();\r\n\t\thw2.setMessage(\"message1 in HelloWorld2\");\r\n\t\thw2.setMessage2(\"message2 in HelloWorld2\");\r\n\t\treturn hw2;\r\n\t}\r\n\t\r\n\t@Bean\r\n\tpublic HelloWorld2 hw3() {\r\n\t\tHelloWorld2 hw2 = new HelloWorld2();\r\n\t\thw2.setMessage(\"message1 in HelloWorld3\");\r\n\t\thw2.setMessage2(\"message2 in HelloWorld3\");\r\n\t\treturn hw2;\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<strong>\u51b3\u5b9a\u4e00\u4e2aBean\u540d\u5b57\u7684\u662f\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u7684\u540d\u5b57<\/strong>\uff0c\u6bd4\u5982hw\u3001hw2\u548chw3\u3002<br>@Bean\u6ce8\u89e3\u4f5c\u7528\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09<strong>\u8fd4\u56de\u7684\u5bf9\u8c61\u5c31\u662f\u4f1a\u88ab\u6dfb\u52a0\u5230\u5bb9\u5668\u5185\u7684Bean<\/strong>\u3002<br>\u751f\u6210Bean\u5bf9\u8c61\u7684<strong>\u51fd\u6570\u53ef\u4ee5\u6709\u53c2\u6570<\/strong>\uff0cSpring\u6846\u67b6\u4f1a\u81ea\u52a8\u89e3\u6790\u4f9d\u8d56\u7684\u5bf9\u8c61\uff0c<strong>\u4f46\u662f\u8fd9\u4e9b\u53c2\u6570\u9700\u8981\u662fBean<\/strong>\uff0c<br>\u8fd9\u4e5f\u5c31\u662f\u6240\u8c13IoC\uff08Inverse of Control\uff09\u7684\u610f\u4e49\uff0c\u5bf9\u8c61\u5168\u90fd\u7531\u5bb9\u5668\u63a7\u5236\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u521b\u5efa\u4e00\u4e2aBean\u914d\u7f6e\u6587\u4ef6\uff0cAnotherBeanConfig.java\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AnnotationConfigExamples;\r\n\r\nimport org.springframework.context.annotation.Bean;\r\n\r\npublic class AnotherBeanConfig {\r\n\t@Bean\r\n\tpublic AutowiredBean autowiredBean() {\r\n\t\treturn new AutowiredBean();\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f88\u7b80\u5355\uff0c\u521b\u5efa\u4e00\u4e2aBean\uff0c\u540d\u5b57\u53ebhw3\u3002<br>\u6700\u540e\u521b\u5efamain\u51fd\u6570\uff08\u65b9\u6cd5\uff09\uff0cCollectionInjectDemo .java\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.explause.AnnotationConfigExamples;\r\n\r\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\r\n\r\npublic class CollectionInjectDemo {\r\n\tpublic static void main(String&#91;] args) {\r\n\t\tAnnotationConfigApplicationContext context = \r\n\t\t\t\tnew AnnotationConfigApplicationContext(\"com.explause.AnnotationConfigExamples\");\r\n\t\tAutowiredBean ab = context.getBean(AutowiredBean.class);\r\n\t\tab.getMessages();\r\n\t\tcontext.close();\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bb9\u5668\uff08ApplicationContext\uff09\u7684\u7c7b\u578b\u7531ClassPathXmlApplicationContext\u53d8\u4e3aAnnotationConfigApplicationContext\u4e86\uff0c\u8fd9\u4e2a\u7c7b<strong>\u5f88\u91cd\u8981\uff0c\u4f1a\u7528\u5728Spring Boot<\/strong>\u91cc\u9762\u3002<br>\u5b83\u7684\u6784\u9020\u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u81ea\u52a8\u626b\u63cf\u7684\u5305\u8def\u5f84\uff0c\u7136\u540e\u81ea\u52a8\u5904\u7406\u5305\u5185\u7c7b\u6587\u4ef6\u7684\u6ce8\u89e3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u7ec8\u7684\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"501\" height=\"459\" src=\"https:\/\/explause.com\/wp-content\/uploads\/2022\/07\/IoC.png\" alt=\"\" class=\"wp-image-819\" srcset=\"https:\/\/devdoge.org\/wp-content\/uploads\/2022\/07\/IoC.png 501w, https:\/\/devdoge.org\/wp-content\/uploads\/2022\/07\/IoC-300x275.png 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u540d\u4e3ahw\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u5728\u540d\u5b57\u5728hw2\u4e4b\u524d\uff0c\u4f46\u662f\u56e0\u4e3a\u521b\u5efa\u8fd9\u4e2aBean\u9700\u8981\u7528\u5230\u7c7b\u578b\u4e3aHelloWorld2\u7684Bean\uff0c<br>\u6b63\u597d\u5bb9\u5668\uff08ApplicationContext\uff09\u5185\u6709\u8fd9\u4e2aBean\u7684\u5b9a\u4e49\uff0c\u6240\u4ee5\u8981\u5148\u521d\u59cb\u5316\u5b83\uff0c\u4e5f\u5c31\u6709\u4e86HelloWorld2\u5728HelloWorld\u4e4b\u524d\u521d\u59cb\u5316\u3002\u81ea\u52a8\u88c5\u914d\uff08@Autowired\uff09\u7684\u53d8\u91cf\uff0c\u5bb9\u5668\u4f1a\u8ba9\u5b83\u6307\u5411\u5bb9\u5668\u5185\u7684Bean\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0d\u4f7f\u7528Beans.xml\u6587\u4ef6\u8fdb\u884c\u914d\u7f6e<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[18],"class_list":["post-279","post","type-post","status-publish","format-standard","hentry","category-spring","tag-spring"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"_links":{"self":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=279"}],"version-history":[{"count":20,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions"}],"predecessor-version":[{"id":830,"href":"https:\/\/devdoge.org\/index.php?rest_route=\/wp\/v2\/posts\/279\/revisions\/830"}],"wp:attachment":[{"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devdoge.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}