{"id":535,"date":"2023-03-23T21:19:42","date_gmt":"2023-03-23T13:19:42","guid":{"rendered":""},"modified":"2023-03-23T21:19:42","modified_gmt":"2023-03-23T13:19:42","slug":"Spring \u81ea\u52a8\u88c5\u914d","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/535.html","title":{"rendered":"Spring \u81ea\u52a8\u88c5\u914d"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Spring \u81ea\u52a8\u88c5\u914d<\/h1>\n<\/p><\/div>\n
\n Spring\u6846\u67b6\u7684\u81ea\u52a8\u88c5\u914d\u529f\u80fd\u4f7f\u60a8\u53ef\u4ee5\u9690\u5f0f\u6ce8\u5165\u5bf9\u8c61\u4f9d\u8d56\u9879\u3002\u5b83\u5728\u5185\u90e8\u4f7f\u7528setter\u6216\u6784\u9020\u51fd\u6570\u6ce8\u5165\u3002\n <\/div>\n
\n \u81ea\u52a8\u88c5\u914d\u4e0d\u80fd\u7528\u4e8e\u6ce8\u5165\u57fa\u672c\u503c\u548c\u5b57\u7b26\u4e32\u503c\u3002\u5b83\u4ec5\u9002\u7528\u4e8e\u53c2\u8003\u3002\n <\/div>\n
\n

\u81ea\u52a8\u88c5\u914d\u7684\u4f18\u70b9<\/h2>\n
\n \u5b83\u9700\u8981
\n \u66f4\u5c11\u7684\u4ee3\u7801<\/b>\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u9700\u8981\u7f16\u5199\u4ee3\u7801\u6765\u663e\u5f0f\u6ce8\u5165\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\n

\u81ea\u52a8\u88c5\u914d\u7684\u7f3a\u70b9<\/h2>\n
\n \u6ca1\u6709\u7a0b\u5e8f\u5458\u7684\u63a7\u5236\u6743\u3002\n <\/div>\n
\n \u5b83\u4e0d\u80fd\u7528\u4e8e\u539f\u59cb\u503c\u548c\u5b57\u7b26\u4e32\u503c\u3002\n <\/div>\n
\n

\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f<\/h2>\n
\n \u6709\u5f88\u591a\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f:\n <\/div>\n\n\n\n\n\n\n\n\n
\u6a21\u5f0f<\/td>\n\u8bf4\u660e<\/td>\n<\/tr>\n
no<\/td>\n\u8fd9\u662f\u9ed8\u8ba4\u7684\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\u3002\u8fd9\u610f\u5473\u7740\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6ca1\u6709\u81ea\u52a8\u88c5\u914d\u3002<\/td>\n<\/tr>\n
byName <\/td>\n byName\u6a21\u5f0f\u6839\u636ebean\u7684\u540d\u79f0\u6ce8\u5165\u5bf9\u8c61\u4f9d\u8d56\u9879\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c5e\u6027\u540d\u79f0\u548cbean\u540d\u79f0\u5fc5\u987b\u76f8\u540c\u3002\u5b83\u5728\u5185\u90e8\u8c03\u7528setter\u65b9\u6cd5\u3002<\/td>\n<\/tr>\n
byType <\/td>\n byType\u6a21\u5f0f\u6839\u636e\u7c7b\u578b\u6ce8\u5165\u5bf9\u8c61\u4f9d\u8d56\u9879\u3002\u56e0\u6b64\u5c5e\u6027\u540d\u79f0\u548cbean\u540d\u79f0\u53ef\u4ee5\u4e0d\u540c\u3002\u5b83\u5728\u5185\u90e8\u8c03\u7528setter\u65b9\u6cd5\u3002<\/td>\n<\/tr>\n
constructor<\/td>\n\u6784\u9020\u51fd\u6570\u6a21\u5f0f\u901a\u8fc7\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u6ce8\u5165\u4f9d\u8d56\u9879\u3002\u5b83\u4f1a\u8c03\u7528\u5177\u6709\u5927\u91cf\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002<\/td>\n<\/tr>\n
autodetect<\/td>\n\u4eceSpring 3\u5f00\u59cb\u4e0d\u63a8\u8350\u4f7f\u7528\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\n

\u81ea\u52a8\u88c5\u914d\u793a\u4f8b<\/h2>\n
\n \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u5728Spring\u4f7f\u7528\u81ea\u52a8\u88c5\u914d\u7684\u7b80\u5355\u4ee3\u7801\u3002\u60a8\u9700\u8981\u4f7f\u7528bean\u5143\u7d20\u7684autowire\u5c5e\u6027\u6765\u5e94\u7528\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\u3002\n <\/div>\n
\n
\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"byName\"<\/span>><\/bean>\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u8ba9\u6211\u4eec\u770b\u770bSpring\u81ea\u52a8\u5e03\u7ebf\u7684\u5b8c\u6574\u793a\u4f8b\u3002\u4e3a\u4e86\u521b\u5efa\u6b64\u793a\u4f8b\uff0c\u6211\u4eec\u521b\u5efa\u4e864\u4e2a\u6587\u4ef6\u3002\n <\/div>\n

B.java <\/b> <\/span>
\n A.java <\/b> <\/span>
\n applicationContext.xml <\/b> <\/span>
\n Test.java <\/b> <\/span> <\/p>\n

\n B.java <\/strong>\n <\/div>\n
\n \u6b64\u7c7b\u4ec5\u5305\u542b\u6784\u9020\u51fd\u6570\u548c\u65b9\u6cd5\u3002\n <\/div>\n
\n
\npackage <\/span>org.sssit;\npublic <\/span>class <\/span>B {\nB(){System.out.println<\/span>(\"b is created\"<\/span>);}\nvoid print(){System.out.println<\/span>(\"hello b\"<\/span>);}\n}\n<\/pre>\n

<\/code>\n <\/div>\n

\n A.java <\/strong>\n <\/div>\n
\n \u6b64\u7c7b\u5305\u542bB\u7c7b\u7684\u5f15\u7528\u4ee5\u53ca\u6784\u9020\u51fd\u6570\u548c\u65b9\u6cd5\u3002\n <\/div>\n
\n
\npackage <\/span>org.sssit;\npublic <\/span>class <\/span>A {\nB b;\nA(){System.out.println<\/span>(\"a is created\"<\/span>);}\npublic <\/span>B getB() {\n  return <\/span>b;\n}\npublic void <\/span>setB(B b) {\n  this<\/span>.b = b;\n}\nvoid print(){System.out.println<\/span>(\"hello a\"<\/span>);}\nvoid display(){\n  print();\n  b.print<\/span>();\n}\n}\n<\/pre>\n

<\/code>\n <\/div>\n

\n applicationContext.xml <\/strong>\n <\/div>\n
\n
\n<?xml version=\"1.0\"<\/span> encoding=\"UTF-8\"<\/span>?>\n<beans\n  xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"<\/span>\n  xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"<\/span>\n  xmlns:p=\"http:\/\/www.springframework.org\/schema\/p\"<\/span>\n  xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans \nhttp:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd\">\n<bean id=\"b\"<\/span> class=\"org.sssit.B\"<\/span>><\/bean>\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"byName\"<\/span>><\/bean>\n<\/beans>\n<\/pre>\n

<\/code>\n <\/div>\n

\n Test.java <\/strong>\n <\/div>\n
\n \u6b64\u7c7b\u4eceapplicationContext.xml\u6587\u4ef6\u83b7\u53d6Bean\u5e76\u8c03\u7528\u663e\u793a\u65b9\u6cd5\u3002\n <\/div>\n
\n
\npackage <\/span>org.sssit;\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\npublic <\/span>class <\/span>Test {\npublic <\/span>static void <\/span>main(String[] args) {\n  ApplicationContext context=new ClassPathXmlApplicationContext(\"applicationContext.xml\"<\/span>);\n  A a=context.getBean<\/span>(\"a\"<\/span>,A.class);\n  a.display<\/span>();\n}\n}\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u8f93\u51fa:\n <\/div>\n
\n
\nb is created\na is created\nhello a\nhello b\n<\/pre>\n

<\/code>\n <\/div>\n


\n

1)byName\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f<\/h3>\n
\n \u5728byName\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\u4e0b\uff0cbean id\u548c\u5f15\u7528\u540d\u79f0\u5fc5\u987b\u76f8\u540c\u3002\n <\/div>\n
\n \u5185\u90e8\u4f7f\u7528setter\u6ce8\u5c04\u3002\n <\/div>\n
\n
\n<bean id=\"b\"<\/span> class=\"org.sssit.B\"<\/span>><\/bean>\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"byName\"<\/span>><\/bean>\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u4f46\u662f\uff0c\u5982\u679c\u66f4\u6539bean\u7684\u540d\u79f0\uff0c\u5b83\u5c06\u4e0d\u4f1a\u6ce8\u5165\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u770b\u5c06bean\u7684\u540d\u79f0\u4eceb\u66f4\u6539\u4e3ab1\u7684\u4ee3\u7801\u3002\n <\/div>\n
\n
\n<bean id=\"b1\"<\/span> class=\"org.sssit.B\"<\/span>><\/bean>\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"byName\"<\/span>><\/bean>\n<\/pre>\n

<\/code>\n <\/div>\n


\n

2)byType\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f<\/h3>\n
\n \u5728byType\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\u4e0b\uff0cbean ID\u548c\u5f15\u7528\u540d\u79f0\u53ef\u80fd\u4e0d\u540c\u3002\u4f46\u662f\u53ea\u80fd\u6709\u4e00\u4e2a\u7c7b\u578b\u7684bean\u3002\n <\/div>\n
\n \u5185\u90e8\u4f7f\u7528setter\u6ce8\u5165\u3002\n <\/div>\n
\n
\n<bean id=\"b1\"<\/span> class=\"org.sssit.B\"<\/span>><\/bean>\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"byType\"<\/span>><\/bean>\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u56e0\u4e3a\u60a8\u521b\u5efa\u4e86B\u7c7b\u578b\u7684\u5b9e\u4f8b\u3002\u6ca1\u5173\u7cfb\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e0e\u5f15\u7528\u540d\u79f0\u4e0d\u540c\u7684Bean\u540d\u79f0\u3002\n <\/div>\n
\n \u4f46\u662f\uff0c\u5982\u679c\u60a8\u5177\u6709\u4e00\u79cd\u7c7b\u578b\u7684\u591a\u4e2aBean\uff0c\u5b83\u5c06\u4e0d\u8d77\u4f5c\u7528\u5e76\u629b\u51fa\u5f02\u5e38\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u770b\u4ee3\u7801\u4e2d\u6709\u5f88\u591aB\u578bbean\u3002\n <\/div>\n
\n
\n<bean id=\"b1\"<\/span> class=\"org.sssit.B\"<\/span><\/span>><\/bean>\n<bean id=\"b2\"<\/span> class=\"org.sssit.B\"><\/bean>\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"byName\"<\/span>><\/bean>\n<\/pre>\n

<\/code>\n <\/div>\n

\n \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5b83\u5c06\u5f15\u53d1\u5f02\u5e38\u3002\n <\/div>\n
\n

3)\u6784\u9020\u51fd\u6570\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f<\/h3>\n
\n \u5728\u6784\u9020\u51fd\u6570\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\u4e0b\uff0cspring\u5bb9\u5668\u901a\u8fc7\u6700\u9ad8\u53c2\u6570\u5316\u7684\u6784\u9020\u51fd\u6570\u6ce8\u5165\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\n \u5982\u679c\u4e00\u4e2a\u7c7b\u4e2d\u76843\u4e2a\u6784\u9020\u51fd\u6570\uff0c\u96f6\u53c2\u6570\uff0c\u4e00\u4e2a\u53c2\u6570\u548c\u4e24\u4e2a\u53c2\u6570\uff0c\u7136\u540e\u901a\u8fc7\u8c03\u7528\u4e24\u4e2a\u53c2\u6570\u6784\u9020\u51fd\u6570\u6267\u884c\u6ce8\u5165\u3002\n <\/div>\n
\n
\n<bean id=\"b\"<\/span> class=\"org.sssit.B\"<\/span>><\/bean>\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"constructor\"<\/span>><\/bean>\n<\/pre>\n

<\/code>\n <\/div>\n


\n

4)\u6ca1\u6709\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f<\/h3>\n
\n \u5728\u6ca1\u6709\u81ea\u52a8\u88c5\u914d\u6a21\u5f0f\u7684\u60c5\u51b5\u4e0b\uff0cspring\u5bb9\u5668\u4e0d\u4f1a\u901a\u8fc7\u81ea\u52a8\u88c5\u914d\u6ce8\u5165\u4f9d\u8d56\u9879\u3002\n <\/div>\n
\n
\n<bean id=\"b\"<\/span> class=\"org.sssit.B\"<\/span>><\/bean>\n<bean id=\"a\"<\/span> class=\"org.sssit.A\"<\/span> autowire=\"no\"<\/span>><\/bean>\n<\/pre>\n

<\/code>\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Spring \u81ea\u52a8\u88c5\u914dzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-bc_spring_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/535"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=535"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/535\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}