{"id":4170,"date":"2019-04-28T12:31:24","date_gmt":"2019-04-28T04:31:24","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=4170"},"modified":"2019-04-28T15:14:33","modified_gmt":"2019-04-28T07:14:33","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e8%af%b4conditional%e6%89%8d%e6%98%afspringboot%e7%9a%84%e5%9f%ba%e7%9f%b3","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4170","title":{"rendered":"\u4e3a\u4ec0\u4e48\u8bf4@Conditional\u624d\u662fSpringBoot\u7684\u57fa\u77f3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u8bf4\u8d77 SpringBoot\uff0c\u4eba\u4eec\u80fd\u60f3\u8d77\u4e00\u5927\u5806\u7684\u540d\u8bcd\u6765\u5f62\u5bb9\u5b83\u7684\u597d\u3002\u4f46\u662f\u8bf4\u5230 SpringBoot \u7684\u6838\u5fc3\u662f\u4ec0\u4e48\uff1f\u662f\u4ec0\u4e48\u6280\u672f\u6216\u529f\u80fd\u624d\u8ba9\u5b83\u5982\u6b64\u4f18\u79c0\uff1f\u5f88\u591a\u4eba\u7b54\u975e\u6240\u95ee\u3002\u5f53\u7136\u6211\u56de\u7b54\u7684\u4e5f\u4e0d\u4e00\u5b9a\u80fd\u8ba9\u5927\u5bb6\u6ee1\u610f\uff0c\u4f46\u6211\u8ba4\u4e3a @Conditional \u6ce8\u89e3\u624d\u662f SpringBoot \u7684\u57fa\u77f3\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws2.sinaimg.cn\/large\/8fa5dcfcgy1g2i7v9lyw6j20ct08amz4.jpg\" alt=\"\" \/><figcaption>BAT \u90fd\u7231\u95ee\u7684 SpringBoot \u9762\u8bd5\u9898<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4ec0\u4e48\u8fd9\u4e48\u8bf4\uff0c\u4e14\u542c\u672c\u6587\u5206\u89e3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">@Conditional \u662f\u7531 Spring 4 \u63d0\u4f9b\u7684\u4e00\u4e2a\u65b0\u7279\u6027\uff0c\u7528\u4e8e\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6765\u63a7\u5236 Bean \u7684\u521b\u5efa\u884c\u4e3a\u3002\u800c\u5728\u6211\u4eec\u5f00\u53d1\u57fa\u4e8e Spring \u7684\u5e94\u7528\u7684\u65f6\u5019\uff0c\u96be\u514d\u4f1a\u9700\u8981\u6839\u636e\u6761\u4ef6\u6765\u6ce8\u518c Bean\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\uff0c\u6211\u4eec\u8981\u6839\u636e\u4e0d\u540c\u7684\u73af\u5883\u5b9e\u4f8b\u5316\u4e0d\u540c\u7684 DataSource\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 @Profile \u6ce8\u89e3\u6765\u5b9e\u73b0\u3002\u4f46\u662f\uff0c\u5f53\u4f60\u67e5\u770b @Profile \u6e90\u7801\u540e\uff0c\u53d1\u73b0\u5176\u5b9e\u5b83\u662f\u901a\u8fc7 @Conditional \u6ce8\u89e3\u6765\u5b9e\u73b0\u7684\u3002<br>\n\u5728 SpringBoot \u4e2d\uff0c\u6211\u4eec\u90fd\u77e5\u9053\u6709\u4e00\u4e2a\u529f\u80fd\u53eb\u201c\u81ea\u52a8\u914d\u7f6e\u201d\u3002\u4f46\u662f AutoConfigure \u662f\u600e\u6837\u8fdb\u884c\u7684\u5462\uff1f\u7b54\u6848\u5c31\u662f @Conditional\uff0c\u4ee5\u53ca\u5b83\u884d\u751f\u51fa\u6765\u7684 @ConditionalOnBean\u3001@ConditionalOnClass\u3001@ConditionalOnProperty \u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e00\u70b9\u4f60\u53ef\u4ee5\u901a\u8fc7 @EnableAutoConfiguration\uff0c@Import\uff0cEnableAutoConfigurationImportSelector \u4ee5\u53caSpringFactoriesLoader.loadFactoryNames() \u6765\u9a8c\u8bc1\u3002SpringFactoriesLoader \u7c7b\u7528\u6765\u626b\u63cf\u52a0\u8f7d\u542b\u6709 META-INF\/spring.factories \u6587\u4ef6\u7684 jar \u5305\uff0c\u8be5\u6587\u4ef6\u8bb0\u5f55\u4e86\u5177\u6709\u54ea\u4e9b\u81ea\u52a8\u914d\u7f6e\u7c7b\u3002\u7136\u540e\u4f60\u5728 spring.factories \u6587\u4ef6\u4e2d\u968f\u4fbf\u627e\u4e00\u4e2a\u81ea\u52a8\u914d\u7f6e\u7c7b\u3002\u6700\u7ec8\u901a\u8fc7\u6e90\u7801\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u4eec\u80cc\u540e\u7684\u6210\u529f\u90fd\u6709\u4e00\u4e2a @Conditional \u6216\u8005\u5176\u884d\u751f\u7684\u6ce8\u89e3\u5728\u652f\u6301\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u6211\u8ba4\u4e3a @Conditional \u624d\u662f SpringBoot \u7684\u57fa\u77f3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u5173\u4e8e SpringBoot \u7684\u9762\u8bd5\u9898\uff0c\u8bf7\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5c0f\u7a0b\u5e8f\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4170\">\u4e3a\u4ec0\u4e48\u8bf4@Conditional\u624d\u662fSpringBoot\u7684\u57fa\u77f3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u8d77 SpringBoot\uff0c\u4eba\u4eec\u80fd\u60f3\u8d77\u4e00\u5927\u5806\u7684\u540d\u8bcd\u6765\u5f62\u5bb9\u5b83\u7684\u597d\u3002\u4f46\u662f\u8bf4\u5230 SpringBoot \u7684\u6838\u5fc3\u662f\u4ec0\u4e48\uff1f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1522,590,579,62,410],"class_list":["post-4170","post","type-post","status-publish","format-standard","hentry","category-java","tag-springboot","tag-springboottest","tag-579","tag-62","tag-410"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4170"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4170\/revisions"}],"predecessor-version":[{"id":4186,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4170\/revisions\/4186"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}