{"id":629,"date":"2024-12-31T20:29:38","date_gmt":"2024-12-31T12:29:38","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=629"},"modified":"2024-12-31T20:30:00","modified_gmt":"2024-12-31T12:30:00","slug":"javaweb-springboot%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=629","title":{"rendered":"JavaWeb-SpringBoot\u539f\u7406"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Bean\u7ba1\u7406\uff1a<\/h1>\n\n\n\n<p>Spring IoC\uff08\u63a7\u5236\u53cd\u8f6c\uff09\u5bb9\u5668\u5bf9\u8c61\uff1a ApplicationContext<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.  \u624b\u52a8\u83b7\u53d6bean\uff1a<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6839\u636ename\u83b7\u53d6bean:    <code>Object applicationContext<\/code>.<code>getBean(String name)<\/code><\/li>\n\n\n\n<li>\u6839\u636e\u7c7b\u578b\u83b7\u53d6bean:<code>    &lt;T> T <code>applicationContext<\/code>.getBean(Class&lt;T> requiredType)<\/code><\/li>\n\n\n\n<li>\u6839\u636ename\u83b7\u53d6bean\uff08\u5e26\u7c7b\u578b\u8f6c\u6362\uff09:<code>    &lt;T> T <code>applicationContext<\/code>.getBean(String name, Class&lt;T> requiredType)<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Bean\u4f5c\u7528\u57df\uff1a<\/h2>\n\n\n\n<p>Spring \u6846\u67b6\u652f\u6301\u4e94\u79cd\u4f5c\u7528\u57df\uff0c\u5176\u4e2d\u540e\u4e09\u79cd\u4f5c\u7528\u57df\u4e3b\u8981\u5728 Web \u73af\u5883\u4e2d\u751f\u6548\uff0c\u4e0d\u5173\u5fc3\u3002\u4ee5\u4e0b\u662f\u8fd9\u4e9b\u4f5c\u7528\u57df\u7684\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>singleton<\/strong>\uff1a\u8fd9\u662f\u9ed8\u8ba4\u7684\u4f5c\u7528\u57df\u3002\u5728 Spring \u5bb9\u5668\u4e2d\uff0c\u540c\u4e00\u4e2a\u540d\u79f0\u7684 bean \u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u610f\u5473\u7740\u65e0\u8bba\u8bf7\u6c42\u591a\u5c11\u6b21\uff0c\u90fd\u4f1a\u8fd4\u56de\u540c\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>prototype<\/strong>\uff1a\u4e0e singleton \u76f8\u53cd\uff0c\u6bcf\u6b21\u4f7f\u7528\u8be5 bean \u65f6\uff0cSpring \u5bb9\u5668\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\u3002\u8fd9\u5141\u8bb8\u6bcf\u4e2a\u8bf7\u6c42\u6216\u6bcf\u6b21\u6ce8\u5165\u65f6\u90fd\u6709\u72ec\u7acb\u7684 bean \u5b9e\u4f8b\u3002<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Singleton \u4f7f\u7528\u573a\u666f<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u7528\u4e8e\u5171\u4eab\u8d44\u6e90\u7ba1\u7406\uff0c\u5982\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff0c\u907f\u514d\u91cd\u590d\u521b\u5efa\u8d44\u6e90\u3002<\/li>\n\n\n\n<li>\u5b58\u50a8\u5168\u5c40\u914d\u7f6e\u4fe1\u606f\uff0c\u4fdd\u8bc1\u5404\u7ec4\u4ef6\u83b7\u53d6\u76f8\u540c\u914d\u7f6e\u3002<\/li>\n\n\n\n<li>\u63d0\u4f9b\u65e0\u72b6\u6001\u670d\u52a1\uff0c\u50cf\u65e5\u5fd7\u8bb0\u5f55\u5668\u7b49\u5de5\u5177\u7c7b\u3002<\/li>\n\n\n\n<li>\u7ba1\u7406\u7f13\u5b58\uff0c\u5b58\u50a8\u4e0d\u5e38\u53d8\u7684\u9891\u7e41\u8bbf\u95ee\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Prototype \u4f7f\u7528\u573a\u666f<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5904\u7406\u6709\u72b6\u6001\u7684\u4e1a\u52a1\u5bf9\u8c61\uff0c\u5982\u7528\u6237\u4f1a\u8bdd\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u5e94\u5bf9\u591a\u5b9e\u4f8b\u7684\u4e1a\u52a1\u903b\u8f91\u5bf9\u8c61\uff0c\u50cf\u8ba2\u5355\u5904\u7406\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u7528\u4e8e\u6570\u636e\u8f6c\u6362\u5bf9\u8c61\uff0c\u6bcf\u6b21\u8f6c\u6362\u4efb\u52a1\u72ec\u7acb\u8fdb\u884c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">@Scope(&#8220;<strong>singleton<\/strong>&#8220;\/&#8221;<strong>prototype<\/strong>&#8220;)  \u914d\u7f6e\u4f5c\u7528\u57df<\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u4e3a<strong>singleton<\/strong>\uff1a\u5355\u4f8b<\/p>\n\n\n\n<p>\u5b9e\u9645\u5f00\u53d1\u65f6\uff0c\u7edd\u5927\u90e8\u5206Bean\u662f\u5355\u4f8b\u7684\uff0c\u5373\u7edd\u5927\u90e8\u5206Bean\u4e0d\u5173\u5fc3Scope\u5c5e\u6027<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">@Lazy \u5ef6\u8fdf\u521d\u59cb\u5316<\/h3>\n\n\n\n<p>\u5ef6\u8fdf\u5230\u7b2c\u4e00\u6b21\u4f7f\u7528\u7684\u65f6\u5019\uff08\u9ed8\u8ba4\u662f\u5bb9\u5668\u542f\u52a8\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u7b2c\u4e09\u65b9Bean\u7ba1\u7406\uff1a@Bean<\/h2>\n\n\n\n<p>\u81ea\u5b9a\u4e49\u7684bean\u53ef\u4ee5\u7528@Component\u53ca\u5176\u884d\u751f\u6ce8\u89e3\u5c06\u5bf9\u8c61\u4ea4\u4ed8\u4e8eIOC\u5bb9\u5668\uff0c\u800c\u6765\u81ea\u4e8e\u7b2c\u4e09\u65b9\u7684bean\uff08\u6e90\u6587\u4ef6\u53ea\u8bfb\uff09\u4e2d\u7528@Bean\u6ce8\u89e3\u3002 <\/p>\n\n\n\n<p>\u4e00\u822c\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff08\u7528@Configuration\u58f0\u660e\uff09\u5bf9\u7b2c\u4e09\u65b9bean\u96c6\u4e2d\u7ba1\u7406\uff1a<\/p>\n\n\n\n<p>\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Configuration\npublic class CommonConfig {\n    @Bean\n    public SAXReader saxReader() {\n        return new SAXReader();\n    }\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7 @Bean \u6ce8\u89e3\u7684 name \u6216 value \u5c5e\u6027\u53ef\u4ee5\u58f0\u660e bean \u7684\u540d\u79f0\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u9ed8\u8ba4 bean \u7684\u540d\u79f0\u5c31\u662f\u65b9\u6cd5\u540d\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u7b2c\u4e09\u65b9 bean \u9700\u8981\u4f9d\u8d56\u5176\u5b83 bean \u5bf9\u8c61\uff0c\u76f4\u63a5\u5728 bean \u5b9a\u4e49\u65b9\u6cd5\u4e2d\u8bbe\u7f6e\u5f62\u53c2\u5373\u53ef\uff0c\u5bb9\u5668\u4f1a\u6839\u636e\u7c7b\u578b\u81ea\u52a8\u88c5\u914d\u3002<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">SpringBoot\u539f\u7406<\/h1>\n\n\n\n<p>SpringBoot\u76f8\u8f83\u4e8e\u539f\u751fSpring\u7684\u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8d77\u6b65\u4f9d\u8d56\uff1a\u5927\u5927\u7b80\u5316\u4e86pom\u6587\u4ef6\u4e2d\u4f9d\u8d56\u7684\u914d\u7f6e<\/li>\n\n\n\n<li>\u81ea\u52a8\u914d\u7f6e\uff1a\u7b80\u5316\u5f00\u53d1\u65f6Bean\u7684\u58f0\u660e\u4e0e\u914d\u7f6e<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u8d77\u6b65\u4f9d\u8d56\uff1a<\/h2>\n\n\n\n<p>\u57fa\u4e8eMaven\u7684\u4f9d\u8d56\u4f20\u9012<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u81ea\u52a8\u914d\u7f6e\uff08\u6838\u5fc3\uff09\uff1a<\/h2>\n\n\n\n<p> Spring\u5bb9\u5668\u542f\u52a8\u540e\uff0c\u81ea\u52a8\u5c06\u914d\u7f6e\u7c7b\u3001bean\u5bf9\u8c61\u5c31\u5b58\u5165\u5230IOC\u5bb9\u5668\u4e2d\u3002<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-8cf370e7 wp-block-group-is-layout-flex\">\n<p>\u542f\u52a8\u7c7bApplication\u7684\u6ce8\u89e3\uff1a@SpringBootApplication &#8211;><\/p>\n\n\n\n<p>@EnableAutoConfiguration&#8211;><\/p>\n\n\n\n<p>@Import({AutoConfigurationImportSelector.class})&#8211;><\/p>\n\n\n\n<p>{AutoConfigurationImportSelector.class\u8fd9\u4e2a\u5b9e\u73b0\u7c7b\u7684String[] selectImports\uff0c\u5176\u8fd4\u56de\u503c\u5373\u4e3a\u8981\u4ea4\u7ed9IOC\u5bb9\u5668\u7ba1\u7406\u7684\u7c7b\u7684\u7c7b\u540d,\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8bfb\u53d6\u4ee5\u4e0b\u4e24\u4e2a\u6587\u4ef6&#8211;><\/p>\n\n\n\n<p>1.spring.factories<\/p>\n\n\n\n<p>2.AutoConfiguration.imports:\u914d\u7f6e\u8981\u81ea\u52a8\u914d\u7f6e\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u4e2d\u4f7f\u7528@Condition\u7684\u884d\u751f\u6ce8\u89e3\u4f9d\u636e\u8bbe\u5b9a\u6761\u4ef6\u8fdb\u884c\u88c5\u914d\u3002  <\/p>\n\n\n\n<p><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bean\u7ba1\u7406\uff1a Spring IoC\uff08\u63a7\u5236\u53cd\u8f6c\uff09\u5bb9\u5668\u5bf9\u8c61\uff1a ApplicationContext 1. \u624b\u52a8\u83b7 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-javaweblist"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=629"}],"version-history":[{"count":1,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":630,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions\/630"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}