{"id":7532,"date":"2023-11-28T19:34:52","date_gmt":"2023-11-28T11:34:52","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7532"},"modified":"2023-12-05T15:33:39","modified_gmt":"2023-12-05T07:33:39","slug":"springboot-zi-dong-pei-zhi-yuan-li-shi-shen-me-shu","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zi-dong-pei-zhi-yuan-li-shi-shen-me-shu\/","title":{"rendered":"SpringBoot\u81ea\u52a8\u914d\u7f6e\u539f\u7406\u662f\u4ec0\u4e48\uff0c\u8bf4\u4e00\u4e0b\uff1f"},"content":{"rendered":"<p>Spring Boot \u7684\u81ea\u52a8\u914d\u7f6e\u539f\u7406\u57fa\u4e8e\u6761\u4ef6\u5316\u914d\u7f6e\uff08Conditional Configuration\uff09\u548c Spring \u7684 <code>@Conditional<\/code> \u6ce8\u89e3\u3002\u5b83\u7684\u6838\u5fc3\u601d\u60f3\u662f\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684 classpath\u3001\u5b58\u5728\u7684\u7c7b\u3001Bean \u7684\u5b58\u5728\u7b49\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u542f\u7528\u67d0\u4e2a\u914d\u7f6e\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f Spring Boot \u81ea\u52a8\u914d\u7f6e\u7684\u4e3b\u8981\u539f\u7406\uff1a<\/p>\n<ol>\n<li>\n<strong>\u6761\u4ef6\u5316\u914d\u7f6e\uff08Conditional Configuration\uff09\uff1a<\/strong> Spring Boot \u4f7f\u7528\u6761\u4ef6\u5316\u914d\u7f6e\u6765\u6839\u636e\u4e00\u7ec4\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u5e94\u8be5\u542f\u7528\u67d0\u4e2a\u914d\u7f6e\u7c7b\u6216 Bean\u3002\u6761\u4ef6\u901a\u5e38\u662f\u7531 <code>@Conditional<\/code> \u6ce8\u89e3\u548c\u5b9e\u73b0 <code>Condition<\/code> \u63a5\u53e3\u7684\u6761\u4ef6\u7c7b\u7ec4\u6210\u3002<\/p>\n<pre><code class=\"language-java\">@Configuration\n@ConditionalOnClass(MyClass.class)\npublic class MyAutoConfiguration {\n    \/\/ Configuration details...\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\uff0c\u53ea\u6709\u5728 classpath \u4e2d\u5b58\u5728 <code>MyClass<\/code> \u7c7b\u65f6\uff0c<code>MyAutoConfiguration<\/code> \u624d\u4f1a\u88ab\u52a0\u8f7d\u3002\n<\/li>\n<li>\n<strong>\u81ea\u52a8\u914d\u7f6e\u7c7b\u547d\u540d\u89c4\u5219\uff1a<\/strong> Spring Boot \u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\u901a\u5e38\u4f7f\u7528 <code>AutoConfiguration<\/code> \u540e\u7f00\uff0c\u5e76\u4e14\u8fd9\u4e9b\u7c7b\u901a\u5e38\u5305\u542b\u5728 <code>META-INF\/spring.factories<\/code> \u6587\u4ef6\u4e2d\u3002Spring Boot \u5728\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u626b\u63cf classpath \u4e0b\u7684 <code>spring.factories<\/code> \u6587\u4ef6\uff0c\u52a0\u8f7d\u5176\u4e2d\u58f0\u660e\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\u3002<\/p>\n<pre><code class=\"language-properties\">org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\\ncom.example.MyAutoConfiguration\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>com.example.MyAutoConfiguration<\/code> \u5c31\u662f\u4e00\u4e2a\u81ea\u52a8\u914d\u7f6e\u7c7b\u3002\n<\/li>\n<li>\n<strong>\u6761\u4ef6\u6ce8\u89e3\uff08<code>@ConditionalOnXxx<\/code>\uff09\uff1a<\/strong> Spring Boot \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u6761\u4ef6\u6ce8\u89e3\uff0c\u5982 <code>@ConditionalOnClass<\/code>\u3001<code>@ConditionalOnMissingClass<\/code>\u3001<code>@ConditionalOnBean<\/code>\u3001<code>@ConditionalOnMissingBean<\/code> \u7b49\uff0c\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u6765\u51b3\u5b9a\u662f\u5426\u5e94\u7528\u67d0\u4e2a\u914d\u7f6e\u3002<\/p>\n<pre><code class=\"language-java\">@Configuration\n@ConditionalOnClass(MyClass.class)\npublic class MyAutoConfiguration {\n    \/\/ Configuration details...\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u8868\u793a\uff0c\u53ea\u6709\u5728 classpath \u4e2d\u5b58\u5728 <code>MyClass<\/code> \u7c7b\u65f6\uff0c\u624d\u4f1a\u5e94\u7528 <code>MyAutoConfiguration<\/code>\u3002\n<\/li>\n<li>\n<strong>\u6761\u4ef6\u5339\u914d\u5668\uff08ConditionEvaluator\uff09\uff1a<\/strong> Spring Boot \u5728\u8fd0\u884c\u65f6\u4f1a\u4f7f\u7528\u6761\u4ef6\u5339\u914d\u5668\u6765\u5224\u65ad\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\u3002\u6761\u4ef6\u5339\u914d\u5668\u901a\u8fc7\u8c03\u7528\u6761\u4ef6\u7c7b\u7684 <code>matches<\/code> \u65b9\u6cd5\u6765\u5224\u65ad\u6761\u4ef6\u662f\u5426\u6210\u7acb\u3002\n<\/li>\n<li>\n<strong>\u81ea\u52a8\u914d\u7f6e\u7684\u52a0\u8f7d\u65f6\u673a\uff1a<\/strong> Spring Boot \u81ea\u52a8\u914d\u7f6e\u7684\u52a0\u8f7d\u65f6\u673a\u662f\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u542f\u52a8\u8fc7\u7a0b\u4e2d\u3002\u5f53 Spring Boot \u542f\u52a8\u65f6\uff0c\u5b83\u4f1a\u626b\u63cf classpath \u4e0b\u7684 <code>META-INF\/spring.factories<\/code> \u6587\u4ef6\uff0c\u52a0\u8f7d\u5176\u4e2d\u58f0\u660e\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\uff0c\u5e76\u6839\u636e\u6761\u4ef6\u51b3\u5b9a\u662f\u5426\u542f\u7528\u5b83\u4eec\u3002\n<\/li>\n<\/ol>\n<p>\u603b\u7684\u6765\u8bf4\uff0cSpring Boot \u81ea\u52a8\u914d\u7f6e\u7684\u539f\u7406\u662f\u901a\u8fc7\u6761\u4ef6\u5316\u914d\u7f6e\u6765\u51b3\u5b9a\u662f\u5426\u52a0\u8f7d\u67d0\u4e2a\u914d\u7f6e\u7c7b\u6216 Bean\uff0c\u800c\u6761\u4ef6\u7684\u5224\u65ad\u4f9d\u8d56\u4e8e\u6761\u4ef6\u6ce8\u89e3\u548c\u6761\u4ef6\u5339\u914d\u5668\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u5f00\u53d1\u8005\u80fd\u591f\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u7684\u5b9e\u9645\u60c5\u51b5\u6765\u81ea\u52a8\u914d\u7f6e\u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u7684\u81ea\u52a8\u914d\u7f6e\u539f\u7406\u57fa\u4e8e\u6761\u4ef6\u5316\u914d\u7f6e\uff08Conditional Configuration\uff09\u548c  [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-7532","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=7532"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7532\/revisions"}],"predecessor-version":[{"id":16520,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7532\/revisions\/16520"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7532"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}