{"id":10020379,"date":"2024-11-06T09:23:03","date_gmt":"2024-11-06T01:23:03","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020379"},"modified":"2024-11-06T09:23:03","modified_gmt":"2024-11-06T01:23:03","slug":"springboot%e5%90%af%e5%8a%a8%e5%8e%9f%e7%90%86%e8%af%a6%e8%a7%a3%e5%9b%be%e6%96%87%e5%85%a8%e9%9d%a2%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020379","title":{"rendered":"SpringBoot\u542f\u52a8\u539f\u7406\u8be6\u89e3(\u56fe\u6587\u5168\u9762\u603b\u7ed3)"},"content":{"rendered":"<p>@[toc]<br \/>\u867d\u7136\u6211\u4eec\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0cSpring Boot \u4f7f\u7528\u975e\u5e38\u591a\uff0c\u7b97\u662f\u76ee\u524d Java \u5f00\u53d1\u9886\u57df\u4e00\u4e2a\u6807\u914d\u4e86\uff0c\u4f46\u662f\u5c0f\u4f19\u4f34\u4eec\u4ed4\u7ec6\u60f3\u60f3\u81ea\u5df1\u7684\u9762\u8bd5\u7ecf\u5386\uff0c\u548c Spring Boot \u76f8\u5173\u7684\u9762\u8bd5\u9898\u90fd\u6709\u54ea\u4e9b\uff1f\u4e2a\u4eba\u611f\u89c9\u5e94\u8be5\u662f\u6bd4\u8f83\u5c11\u7684\uff0cSpring Boot \u672c\u8d28\u4e0a\u8fd8\u662f\u66fe\u7ecf SSM \u90a3\u4e00\u5957\uff0c\u53ea\u662f\u901a\u8fc7\u5404\u79cd starter \u7b80\u5316\u4e86\u914d\u7f6e\u800c\u5df2\uff0c\u5176\u4ed6\u90fd\u662f\u4e00\u6a21\u4e00\u6837\u7684\uff0c\u6240\u4ee5 Spring Boot \u4e2d\u5f88\u591a\u9762\u8bd5\u9898\u8fd8\u662f\u5f97\u56de\u5f52\u5230 Spring \u4e2d\u53bb\u89e3\u7b54\uff01\u5f53\u7136\u8fd9\u5e76\u4e0d\u662f\u8bf4 Spring Boot \u4e2d\u6ca1\u4ec0\u4e48\u53ef\u95ee\u7684\uff0cSpring Boot \u4e2d\u5176\u5b9e\u4e5f\u6709\u4e00\u4e2a\u975e\u5e38\u7ecf\u5178\u7684\u9762\u8bd5\u9898\uff0c\u90a3\u5c31\u662f Spring Boot \u7684\u542f\u52a8\u539f\u7406\u662f\u4ec0\u4e48\uff1f\u4eca\u5929\u677e\u54e5\u5c31\u6765\u548c\u5404\u4f4d\u5c0f\u4f19\u4f34\u804a\u4e00\u4e0b\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u5176\u5b9e\u677e\u54e5\u4e4b\u524d\u548c\u5c0f\u4f19\u4f34\u4eec\u804a\u8fc7\u76f8\u5173\u7684\u95ee\u9898\uff0c\u4e0d\u8fc7\u90fd\u662f\u96f6\u6563\u7684\uff0c\u6ca1\u6709\u7cfb\u7edf\u68b3\u7406\u8fc7\uff0c\u4e4b\u524d\u4e5f\u5e26\u9886\u5c0f\u4f19\u4f34\u4eec\u81ea\u5b9a\u4e49\u8fc7\u4e00\u4e2a starter\uff0c\u76f8\u4fe1\u5404\u4f4d\u5c0f\u4f19\u4f34\u5bf9\u4e8e starter \u7684\u539f\u7406\u4e5f\u6709\u4e00\u5b9a\u4e86\u89e3\uff0c\u6240\u4ee5\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u4e00\u4e9b\u8fc7\u4e8e\u7ec6\u8282\u7684\u5185\u5bb9\u6211\u5c31\u4e0d\u8d58\u8ff0\u4e86\uff0c\u5927\u5bb6\u53ef\u4ee5\u7ffb\u770b\u4e4b\u524d\u7684\u6587\u7ae0\u3002<\/p>\n<h2>\u4e00 @SpringBootApplication<\/h2>\n<p>\u8981\u8bf4 Spring Boot \u7684\u81ea\u52a8\u5316\u914d\u7f6e\uff0c\u90a3\u5fc5\u987b\u4ece\u9879\u76ee\u7684\u542f\u52a8\u7c7b <code>@SpringBootApplication<\/code> \u8bf4\u8d77\uff0c\u8fd9\u662f\u6574\u4e2a Spring Boot \u5b87\u5b99\u7684\u8d77\u70b9\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e0b\u8fd9\u4e2a\u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"java\">@Target(ElementType.TYPE)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Inherited\n@SpringBootConfiguration\n@EnableAutoConfiguration\n@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),\n@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })\npublic @interface SpringBootApplication {\n\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>@SpringBootApplication<\/code> \u6ce8\u89e3\u7ec4\u5408\u4e86\u591a\u4e2a\u5e38\u89c1\u6ce8\u89e3\u7684\u529f\u80fd\uff0c\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>\u524d\u56db\u4e2a\u662f\u5143\u6ce8\u89e3\uff0c\u8fd9\u91cc\u6211\u4eec\u4e0d\u505a\u8ba8\u8bba\u3002<\/li>\n<li>\u7b2c\u4e94\u4e2a <code>@SpringBootConfiguration<\/code> \u662f\u4e00\u4e2a\u652f\u6301\u914d\u7f6e\u7c7b\u7684\u6ce8\u89e3\uff0c\u8fd9\u91cc\u6211\u4eec\u4e5f\u4e0d\u505a\u8ba8\u8bba\u3002<\/li>\n<li>\u7b2c\u516d\u4e2a <code>@EnableAutoConfiguration<\/code> \u8fd9\u4e2a\u6ce8\u89e3\u5c31\u8868\u793a\u5f00\u542f\u81ea\u52a8\u5316\u914d\u7f6e\uff0c\u8fd9\u662f\u6211\u4eec\u4eca\u5929\u8981\u804a\u5f97\u91cd\u70b9\u3002<\/li>\n<li>\u7b2c\u4e03\u4e2a <code>@ComponentScan<\/code> \u662f\u4e00\u4e2a\u5305\u626b\u63cf\u6ce8\u89e3\uff0c\u4e3a\u4ec0\u4e48 <code>Spring Boot<\/code> \u9879\u76ee\u4e2d\u7684 <code>Bean<\/code> \u53ea\u8981\u653e\u5bf9\u4f4d\u7f6e\u5c31\u4f1a\u88ab\u81ea\u52a8\u626b\u63cf\u5230\uff0c\u548c\u8fd9\u4e2a\u6ce8\u89e3\u6709\u5173\u3002<\/li>\n<\/ul>\n<p>\u522b\u770b\u8fd9\u91cc\u6ce8\u89e3\u591a\uff0c\u5176\u5b9e\u771f\u6b63\u7531 <code>Spring Boot<\/code> \u63d0\u4f9b\u7684\u6ce8\u89e3\u4e00\u5171\u5c31\u4e24\u4e2a\uff0c\u5206\u522b\u662f <code>@SpringBootConfiguration<\/code> \u548c <code>@EnableAutoConfiguration<\/code> \u4e24\u4e2a\uff0c\u5176\u4ed6\u6ce8\u89e3\u5728 <code>Spring Boot<\/code> \u51fa\u73b0\u4e4b\u524d\u5c31\u5df2\u7ecf\u5b58\u5728\u591a\u5e74\u4e86\u3002<\/p>\n<h2>\u4e8c @EnableAutoConfiguration<\/h2>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u6765\u770b\u770b <code>@EnableAutoConfiguration<\/code> \u662f\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u5316\u914d\u7f6e\u7684\u3002<\/p>\n<pre><code class=\"java\">@Target(ElementType.TYPE)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Inherited\n@AutoConfigurationPackage\n@Import(AutoConfigurationImportSelector.class)\npublic @interface EnableAutoConfiguration {\n\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6ce8\u89e3\u8d77\u5173\u952e\u4f5c\u7528\u7684\u5c31\u662f\u4e24\u4e2a\u4e1c\u897f\uff1a<\/p>\n<ol>\n<li><code>@AutoConfigurationPackage<\/code>\uff1a\u8fd9\u4e2a\u8868\u793a\u81ea\u52a8\u626b\u63cf\u5404\u79cd\u7b2c\u4e09\u65b9\u7684\u6ce8\u89e3\uff0c\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u677e\u54e5\u5df2\u7ecf\u548c\u5927\u5bb6\u804a\u8fc7\u8fd9\u4e2a\u6ce8\u89e3\u7684\u4f5c\u7528\u4e86\uff0c\u4f20\u9001\u95e8\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=IIeso%2F4zXPZbNqHE2VEJkQ%3D%3D.3%2BO%2FnQLjaUfH7lFxudp6oeuCt1bLCpQ6NJNGj0Uph3nAlKn6gt2F65k863Baaz52d454YO%2BvM173hInbB7iZjw%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">@AutoConfigurationPackage \u548c @ComponentScan \u6709\u4f55\u533a\u522b\uff1f<\/a><\/li>\n<li><code>@Import<\/code> \u5219\u662f\u5728\u5bfc\u5165 <code>AutoConfigurationImportSelector<\/code> \u914d\u7f6e\u7c7b\uff0c\u8fd9\u4e2a\u914d\u7f6e\u7c7b\u91cc\u8fb9\u5c31\u662f\u53bb\u52a0\u8f7d\u5404\u79cd\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u7684\u3002<\/li>\n<\/ol>\n<h2>\u4e09 AutoConfigurationImportSelector<\/h2>\n<p><code>AutoConfigurationImportSelector<\/code> \u7c7b\u4e2d\u7684\u65b9\u6cd5\u6bd4\u8f83\u591a\uff0c\u5165\u53e3\u7684\u5730\u65b9\u5219\u662f process \u65b9\u6cd5\uff0c\u6240\u4ee5\u6211\u4eec\u8fd9\u91cc\u5c31\u4ece process \u65b9\u6cd5\u5f00\u59cb\u770b\u8d77\uff1a<\/p>\n<pre><code class=\"java\">@Override\npublic void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {\n    Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,\n            () -&gt; String.format(&quot;Only %s implementations are supported, got %s&quot;,\n                    AutoConfigurationImportSelector.class.getSimpleName(),\n                    deferredImportSelector.getClass().getName()));\n    AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)\n        .getAutoConfigurationEntry(annotationMetadata);\n    this.autoConfigurationEntries.add(autoConfigurationEntry);\n    for (String importClassName : autoConfigurationEntry.getConfigurations()) {\n        this.entries.putIfAbsent(importClassName, annotationMetadata);\n    }\n}<\/code><\/pre>\n<p>\u4ece\u7c7b\u540d\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff0c\u8ddf\u81ea\u52a8\u5316\u914d\u7f6e\u76f8\u5173\u7684\u5bf9\u8c61\u662f\u7531 <code>AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector).getAutoConfigurationEntry(annotationMetadata);<\/code> \u8fdb\u884c\u52a0\u8f7d\u7684\u3002<\/p>\n<p>\u5f53\u7136\u8fd9\u91cc\u7684 <code>getAutoConfigurationEntry<\/code> \u65b9\u6cd5\u5b9e\u9645\u4e0a\u5c31\u662f\u5f53\u524d\u7c7b\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u6765\u770b\u4e0b\u8be5\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {\n    if (!isEnabled(annotationMetadata)) {\n        return EMPTY_ENTRY;\n    }\n    AnnotationAttributes attributes = getAttributes(annotationMetadata);\n    List&lt;String&gt; configurations = getCandidateConfigurations(annotationMetadata, attributes);\n    configurations = removeDuplicates(configurations);\n    Set&lt;String&gt; exclusions = getExclusions(annotationMetadata, attributes);\n    checkExcludedClasses(configurations, exclusions);\n    configurations.removeAll(exclusions);\n    configurations = getConfigurationClassFilter().filter(configurations);\n    fireAutoConfigurationImportEvents(configurations, exclusions);\n    return new AutoConfigurationEntry(configurations, exclusions);\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6e90\u7801\u7684\u65b9\u6cd5\u547d\u540d\u90fd\u505a\u7684\u4e0d\u9519\uff0c\u57fa\u672c\u4e0a\u90fd\u80fd\u505a\u5230\u89c1\u540d\u77e5\u610f\uff0c\u5c0f\u4f19\u4f34\u4eec\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u5e94\u8be5\u5411\u8fd9\u6837\u7684\u547d\u540d\u601d\u8def\u770b\u9f50\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u6765\u6328\u4e2a\u770b\u4e00\u4e0b\u8fd9\u91cc\u7684\u5173\u952e\u65b9\u6cd5\u3002<\/p>\n<h3>3.1 isEnabled<\/h3>\n<p>\u9996\u5148\u8c03\u7528 isEnabled \u65b9\u6cd5\u53bb\u5224\u65ad\u81ea\u52a8\u5316\u914d\u7f6e\u5230\u5e95\u6709\u6ca1\u6709\u5f00\u542f\uff0c\u8fd9\u4e2a\u4e3b\u8981\u662f\u56e0\u4e3a\u6211\u4eec\u53ca\u65f6\u5728\u9879\u76ee\u4e2d\u5f15\u5165\u4e86 spring-boot-starter-xxx \u4e4b\u540e\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728 application.properties \u4e2d\u914d\u7f6e <code>spring.boot.enableautoconfiguration=false<\/code> \u6765\u5173\u95ed\u6240\u6709\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u3002<\/p>\n<p>\u76f8\u5173\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">protected boolean isEnabled(AnnotationMetadata metadata) {\n    if (getClass() == AutoConfigurationImportSelector.class) {\n        return getEnvironment().getProperty(EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, Boolean.class, true);\n    }\n    return true;\n}<\/code><\/pre>\n<h3>3.2 getCandidateConfigurations&#x20;<\/h3>\n<p>\u63a5\u4e0b\u6765\u8c03\u7528 <code>getCandidateConfigurations<\/code> \u65b9\u6cd5\u53bb\u83b7\u53d6\u6240\u6709\u5019\u9009\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u8fd9\u4e9b\u5019\u9009\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u4e3b\u8981\u6765\u81ea\u4e24\u4e2a\u5730\u65b9\uff1a<\/p>\n<ol>\n<li>\u5728\u4e4b\u524d\u7684\u81ea\u5b9a\u4e49 <code>starter<\/code> \u4e2d\u677e\u54e5\u548c\u5927\u5bb6\u804a\u8fc7\uff0c\u6211\u4eec\u9700\u8981\u5728 <code>claspath\/:META-INF\/spring.factories<\/code> \u4e2d\u5b9a\u4e49\u51fa\u6765\u6240\u6709\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u8fd9\u662f\u6765\u6e90\u4e00\u3002<\/li>\n<li><code>Spring Boot<\/code> \u81ea\u5e26\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u8fd9\u4e2a\u5728\u4e4b\u524d\u7684 vhr \u89c6\u9891\u4e2d\u4e5f\u548c\u5c0f\u4f19\u4f34\u4eec\u591a\u6b21\u8bb2\u8fc7\uff0c<code>Spring Boot<\/code> \u81ea\u5e26\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u4f4d\u4e8e <code>spring-boot-autoconfigure-3.0.6.jar!\/META-INF\/spring\/org.springframework.boot.autoconfigure.AutoConfiguration.imports<\/code> \u6587\u4ef6\u4e2d\u3002<\/li>\n<\/ol>\n<p>\u76f8\u5173\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">protected List&lt;String&gt; getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {\n    List&lt;String&gt; configurations = new ArrayList&lt;&gt;(\n            SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));\n    ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);\n    Assert.notEmpty(configurations,\n            &quot;No auto configuration classes found in META-INF\/spring.factories nor in META-INF\/spring\/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you &quot;\n                    + &quot;are using a custom packaging, make sure that file is correct.&quot;);\n    return configurations;\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u52a0\u8f7d\u5230\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u7684\u5168\u8def\u5f84\u88ab\u5b58\u5165\u5230 <code>configurations<\/code> \u5bf9\u8c61\u4e2d\uff0c\u8be5\u5bf9\u8c61\u6709\u4e24\u4e2a\u83b7\u53d6\u7684\u5730\u65b9\uff1a<\/p>\n<ol>\n<li>\u8c03\u7528 <code>SpringFactoriesLoader.loadFactoryNames<\/code> \u65b9\u6cd5\u83b7\u53d6\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7ec6\u8282\u6211\u5c31\u4e0d\u5e26\u5927\u5bb6\u770b\u4e86\uff0c\u6bd4\u8f83\u7b80\u5355\uff0c\u672c\u8d28\u4e0a\u5c31\u662f\u53bb\u52a0\u8f7d <code>META-INF\/spring.factories<\/code> \u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86\u5927\u91cf\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u7684\u5168\u8def\u5f84\u3002<\/li>\n<li>\u8c03\u7528 <code>ImportCandidates.load<\/code> \u65b9\u6cd5\u53bb\u52a0\u8f7d\uff0c\u8fd9\u4e2a\u5c31\u662f\u52a0\u8f7d <code>spring-boot-autoconfigure-3.0.6.jar!\/META-INF\/spring\/org.springframework.boot.autoconfigure.AutoConfiguration.imports<\/code> \u6587\u4ef6\u4e2d\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u3002<\/li>\n<\/ol>\n<p>\u5982\u679c\u8fd9\u4e24\u4e2a\u5730\u65b9\u90fd\u6ca1\u6709\u52a0\u8f7d\u5230\u4efb\u4f55\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u90a3\u4e48\u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n<h3>3.3 removeDuplicates<\/h3>\n<p><code>removeDuplicates<\/code> \u65b9\u6cd5\u8868\u793a\u79fb\u9664\u5019\u9009\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u4e2d\u91cd\u590d\u7684\u7c7b\uff0c\u79fb\u9664\u7684\u601d\u8def\u4e5f\u5f88\u6709\u610f\u601d\uff0c\u5c31\u7528\u4e00\u4e2a <code>LinkedHashSet<\/code> \u4e2d\u8f6c\u4e00\u4e0b\u5c31\u884c\u4e86\uff0c\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">protected final &lt;T&gt; List&lt;T&gt; removeDuplicates(List&lt;T&gt; list) {\n    return new ArrayList&lt;&gt;(new LinkedHashSet&lt;&gt;(list));\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u8fd9\u4e9b\u6e90\u7801\u91cc\u6709\u65f6\u5019\u4e00\u4e9b\u89e3\u51b3\u601d\u8def\u4e5f\u5f88\u6709\u610f\u601d\u3002<\/p>\n<h3>3.4 getExclusions<\/h3>\n<p><code>getExclusions<\/code> \u65b9\u6cd5\u8868\u793a\u9700\u8981\u83b7\u53d6\u5230\u6240\u6709\u88ab\u6392\u9664\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u8fd9\u4e9b\u88ab\u6392\u9664\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u53ef\u4ee5\u4ece\u4e09\u4e2a\u5730\u65b9\u83b7\u53d6\uff1a<\/p>\n<ol>\n<li>\u5f53\u524d\u6ce8\u89e3\u7684 <code>exclude<\/code> \u5c5e\u6027\u3002<\/li>\n<li>\u5f53\u524d\u6ce8\u89e3\u7684 <code>excludeName<\/code> \u5c5e\u6027\u3002<\/li>\n<li><code>application.properties<\/code> \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 <code>spring.autoconfigure.exclude<\/code> \u5c5e\u6027\u3002<\/li>\n<\/ol>\n<p>\u6765\u770b\u4e00\u4e0b\u76f8\u5173\u6e90\u7801\uff1a<\/p>\n<pre><code class=\"java\">protected Set&lt;String&gt; getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {\n    Set&lt;String&gt; excluded = new LinkedHashSet&lt;&gt;();\n    excluded.addAll(asList(attributes, &quot;exclude&quot;));\n    excluded.addAll(asList(attributes, &quot;excludeName&quot;));\n    excluded.addAll(getExcludeAutoConfigurationsProperty());\n    return excluded;\n}<\/code><\/pre>\n<p>\u8ddf\u4e0a\u9762\u8bb2\u89e3\u7684\u4e09\u70b9\u521a\u597d\u5bf9\u5e94\u3002<\/p>\n<h3>3.5 checkExcludedClasses<\/h3>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u662f\u68c0\u67e5\u6240\u6709\u88ab\u6392\u9664\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u7531\u4e8e <code>Spring Boot<\/code> \u4e2d\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u5e76\u4e0d\u9700\u8981\u7edf\u4e00\u5b9e\u73b0\u67d0\u4e00\u4e2a\u63a5\u53e3\u6216\u8005\u7edf\u4e00\u7ee7\u627f\u67d0\u4e00\u4e2a\u7c7b\uff0c\u6240\u4ee5\u5728\u5199\u6392\u9664\u7c7b\u7684\u65f6\u5019\uff0c\u5982\u679c\u5199\u9519\u4e86\u7f16\u8bd1\u662f\u6821\u9a8c\u4e0d\u51fa\u6765\u7684\uff0c\u50cf\u4e0b\u9762\u8fd9\u79cd\uff1a<\/p>\n<pre><code class=\"java\">@SpringBootApplication(exclude = HelloController.class)\npublic class App {\n    public static void main(String[] args) {\n        SpringApplication.run(App.class, args);\n    }\n}<\/code><\/pre>\n<p>\u7531\u4e8e <code>HelloController<\/code> \u5e76\u4e0d\u662f\u4e00\u4e2a\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u6240\u4ee5\u8fd9\u6837\u5199\u9879\u76ee\u542f\u52a8\u7684\u65f6\u5019\u5c31\u4f1a\u62a5\u9519\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000045450729\" alt=\"\" title=\"\"><\/p>\n<p>\u8fd9\u4e2a\u5f02\u5e38\u4ece\u54ea\u6765\u7684\u5462\uff1f\u5176\u5b9e\u5c31\u662f\u6765\u81ea <code>checkExcludedClasses<\/code> \u65b9\u6cd5\uff0c\u6211\u4eec\u6765\u770b\u4e0b\u8be5\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">private void checkExcludedClasses(List&lt;String&gt; configurations, Set&lt;String&gt; exclusions) {\n    List&lt;String&gt; invalidExcludes = new ArrayList&lt;&gt;(exclusions.size());\n    for (String exclusion : exclusions) {\n        if (ClassUtils.isPresent(exclusion, getClass().getClassLoader()) &amp;&amp; !configurations.contains(exclusion)) {\n            invalidExcludes.add(exclusion);\n        }\n    }\n    if (!invalidExcludes.isEmpty()) {\n        handleInvalidExcludes(invalidExcludes);\n    }\n}\nprotected void handleInvalidExcludes(List&lt;String&gt; invalidExcludes) {\n    StringBuilder message = new StringBuilder();\n    for (String exclude : invalidExcludes) {\n        message.append(&quot;\/t- &quot;).append(exclude).append(String.format(&quot;%n&quot;));\n    }\n    throw new IllegalStateException(String.format(\n            &quot;The following classes could not be excluded because they are not auto-configuration classes:%n%s&quot;,\n            message));\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5728 <code>checkExcludedClasses<\/code> \u65b9\u6cd5\u4e2d\uff0c\u4f1a\u9996\u5148\u627e\u5230\u6240\u6709\u4f4d\u4e8e\u5f53\u524d\u7c7b\u8def\u5f84\u4e0b\u4f46\u662f\u5374\u4e0d\u5305\u542b\u5728 <code>configurations<\/code> \u4e2d\u7684\u6240\u6709\u88ab\u6392\u9664\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u7531\u4e8e <code>configurations<\/code> \u4e2d\u7684\u5c31\u662f\u6240\u6709\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u4e86\uff0c\u6240\u4ee5\u8fd9\u4e9b\u4e0d\u5b58\u5728\u4e8e <code>configurations<\/code> \u4e2d\u7684\u7c7b\u90fd\u662f\u6709\u95ee\u9898\u7684\uff0c\u90fd\u4e0d\u662f\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\uff0c\u5c06\u8fd9\u4e9b\u6709\u95ee\u9898\u7684\u7c7b\u6536\u96c6\u8d77\u6765\uff0c\u5b58\u5165\u5230 <code>invalidExcludes<\/code> \u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u518d\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u3002<\/p>\n<p>\u6240\u8c13\u989d\u5916\u7684\u5904\u7406\u5c31\u662f\u5728 <code>handleInvalidExcludes<\/code> \u65b9\u6cd5\u4e2d\u629b\u51fa\u5f02\u5e38\uff0c\u524d\u9762\u622a\u56fe\u4e2d\u7684\u5f02\u5e38\u5c31\u662f\u6765\u81ea\u8fd9\u91cc\u3002<\/p>\n<h3>3.6 removeAll<\/h3>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u4e00\u4e2a\u4efb\u52a1\uff0c\u5c31\u662f\u4ece <code>configurations<\/code> \u4e2d\u79fb\u9664\u6389\u90a3\u4e9b\u88ab\u6392\u9664\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u3002<code>configurations<\/code> \u672c\u8eab\u5c31\u662f <code>List<\/code> \u96c6\u5408\uff0c<code>exclusions<\/code> \u5219\u662f\u4e00\u4e2a <code>Set<\/code> \u96c6\u5408\uff0c\u6240\u4ee5\u8fd9\u91cc\u76f4\u63a5\u79fb\u9664\u5373\u53ef\u3002<\/p>\n<h3>3.7 filter<\/h3>\n<p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u52a0\u8f7d\u4e86\u6240\u6709\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u4e86\uff0c\u4f46\u662f\u8fd9\u4e9b\u914d\u7f6e\u7c7b\u5e76\u4e0d\u662f\u90fd\u4f1a\u751f\u6548\uff0c\u5177\u4f53\u662f\u5426\u751f\u6548\uff0c\u8fd8\u8981\u770b\u4f60\u7684\u9879\u76ee\u662f\u5426\u4f7f\u7528\u4e86\u5177\u4f53\u7684\u4f9d\u8d56\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u73b0\u5728\u52a0\u8f7d\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u91cc\u91cc\u8fb9\u5c31\u5305\u542b\u4e86 RedisAutoConfiguration\uff0c\u8fd9\u4e2a\u662f\u81ea\u52a8\u914d\u7f6e Redis \u7684\uff0c\u4f46\u662f\u7531\u4e8e\u6211\u7684\u9879\u76ee\u4e2d\u5e76\u6ca1\u6709\u4f7f\u7528 Redis\uff0c\u6240\u4ee5\u8fd9\u4e2a\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u5e76\u4e0d\u4f1a\u751f\u6548\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u662f\u7531 <code>getConfigurationClassFilter().filter(configurations);<\/code> \u6765\u5b8c\u6210\u7684\u3002<\/p>\n<p>\u5148\u8bf4\u4e00\u4e2a\u9884\u5907\u77e5\u8bc6\uff1a<\/p>\n<p>\u7531\u4e8e\u6211\u4eec\u9879\u76ee\u4e2d\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u7279\u522b\u591a\uff0c\u6bcf\u4e00\u4e2a\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u90fd\u4f1a\u4f9d\u8d56\u522b\u7684\u7c7b\uff0c\u5f53\u522b\u7684\u7c7b\u5b58\u5728\u65f6\uff0c\u8fd9\u4e2a\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u624d\u4f1a\u751f\u6548\uff0c\u8fd9\u4e00\u5806\u4e92\u76f8\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5b58\u5728\u4e8e <code>spring-boot-autoconfigure-3.0.6.jar!\/META-INF\/spring-autoconfigure-metadata.properties<\/code> \u6587\u4ef6\u4e4b\u4e2d\uff0c\u6211\u968f\u4fbf\u4e3e\u4e00\u4e2a\u8be5\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\uff1a<\/p>\n<ul>\n<li><code>org.springframework.boot.autoconfigure.amqp.RabbitAnnotationDrivenConfiguration.ConditionalOnClass=org.springframework.amqp.rabbit.annotation.EnableRabbit<\/code> \u8868\u793a RabbitAnnotationDrivenConfiguration \u7c7b\u8981\u751f\u6548\u6709\u4e00\u4e2a\u5fc5\u5907\u6761\u4ef6\u5c31\u662f\u5f53\u524d\u9879\u76ee\u7c7b\u8def\u5f84\u4e0b\u8981\u5b58\u5728 <code>org.springframework.amqp.rabbit.annotation.EnableRabbit<\/code>\u3002<\/li>\n<\/ul>\n<p>\u6211\u4eec\u6765\u770b\u770b RabbitAnnotationDrivenConfiguration \u7c7b\u7684\u6ce8\u89e3\uff1a<\/p>\n<pre><code class=\"java\">@Configuration(proxyBeanMethods = false)\n@ConditionalOnClass(EnableRabbit.class)\nclass RabbitAnnotationDrivenConfiguration {\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7c7b\u548c\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u4e00\u81f4\u3002<\/p>\n<p>\u8fd9\u4e2a\u9884\u5907\u77e5\u8bc6\u641e\u61c2\u4e86\uff0c\u63a5\u4e0b\u6765\u7684\u5185\u5bb9\u5c31\u597d\u7406\u89e3\u4e86\u3002<\/p>\n<p>\u5148\u6765\u770b getConfigurationClassFilter \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u5c31\u662f\u83b7\u53d6\u6240\u6709\u7684\u8fc7\u6ee4\u5668\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">private ConfigurationClassFilter getConfigurationClassFilter() {\n    if (this.configurationClassFilter == null) {\n        List&lt;AutoConfigurationImportFilter&gt; filters = getAutoConfigurationImportFilters();\n        for (AutoConfigurationImportFilter filter : filters) {\n            invokeAwareMethods(filter);\n        }\n        this.configurationClassFilter = new ConfigurationClassFilter(this.beanClassLoader, filters);\n    }\n    return this.configurationClassFilter;\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u91cc\u83b7\u53d6\u5230\u7684\u8fc7\u6ee4\u5668\u90fd\u662f AutoConfigurationImportFilter \u7c7b\u578b\u7684\uff0c\u8fd9\u4e2a\u7c7b\u578b\u7684\u8fc7\u6ee4\u5668\u53ea\u6709\u4e09\u4e2a\u5b9e\u4f8b\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000045450730\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4ece\u8fd9\u4e09\u4e2a\u5b9e\u4f8b\u7684\u540d\u5b57\u4e2d\uff0c\u57fa\u672c\u4e0a\u5c31\u80fd\u770b\u51fa\u6765\u5404\u81ea\u7684\u4f5c\u7528\uff1a<\/p>\n<ul>\n<li>OnClassCondition\uff1a\u8fd9\u4e2a\u5c31\u662f\u6761\u4ef6\u6ce8\u89e3 <code>@ConditionalOnClass<\/code> \u7684\u5224\u5b9a\u6761\u4ef6\uff0c\u770b\u540d\u5b57\u5c31\u77e5\u9053\u7528\u6765\u5224\u65ad\u5f53\u524d classpath \u4e0b\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u7c7b\u3002<\/li>\n<li>OnWebApplicationCondition\uff1a\u8fd9\u4e2a\u662f\u6761\u4ef6\u6ce8\u89e3 <code>ConditionalOnWebApplication<\/code> \u7684\u5224\u5b9a\u6761\u4ef6\uff0c\u7528\u6765\u5224\u65ad\u5f53\u524d\u7cfb\u7edf\u73af\u5883\u662f\u5426\u662f\u4e00\u4e2a Web \u73af\u5883\u3002<\/li>\n<li>OnBeanCondition\uff1a\u8fd9\u4e2a\u662f\u6761\u4ef6\u6ce8\u89e3 <code>@ConditionalOnBean<\/code> \u7684\u5224\u5b9a\u6761\u4ef6\uff0c\u5c31\u662f\u5224\u65ad\u5f53\u524d\u7cfb\u7edf\u4e0b\u662f\u5426\u5b58\u5728\u67d0\u4e2a Bean\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u83b7\u53d6\u5230\u7684\u4e09\u4e2a AutoConfigurationImportFilter \u8fc7\u6ee4\u5668\u5176\u5b9e\u5c31\u662f\u4e0a\u9762\u8fd9\u4e09\u4e2a\u3002\u63a5\u4e0b\u6765\u6267\u884c filter \u65b9\u6cd5\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">List&lt;String&gt; filter(List&lt;String&gt; configurations) {\n    long startTime = System.nanoTime();\n    String[] candidates = StringUtils.toStringArray(configurations);\n    boolean skipped = false;\n    for (AutoConfigurationImportFilter filter : this.filters) {\n        boolean[] match = filter.match(candidates, this.autoConfigurationMetadata);\n        for (int i = 0; i &lt; match.length; i++) {\n            if (!match[i]) {\n                candidates[i] = null;\n                skipped = true;\n            }\n        }\n    }\n    if (!skipped) {\n        return configurations;\n    }\n    List&lt;String&gt; result = new ArrayList&lt;&gt;(candidates.length);\n    for (String candidate : candidates) {\n        if (candidate != null) {\n            result.add(candidate);\n        }\n    }\n    return result;\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u5c31\u662f\u904d\u5386\u8fd9\u4e09\u4e2a\u8fc7\u6ee4\u5668\uff0c\u7136\u540e\u5206\u522b\u8c03\u7528\u5404\u81ea\u7684 match \u65b9\u6cd5\u548c 144 \u4e2a\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u8fdb\u884c\u5339\u914d\uff0c\u5982\u679c\u8fd9\u4e9b\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u6240\u9700\u8981\u7684\u6761\u4ef6\u5f97\u5230\u6ee1\u8db3\uff0c\u5219 match \u6570\u7ec4\u5bf9\u5e94\u7684\u4f4d\u7f6e\u5c31\u4e3a true\uff0c\u5426\u5219\u5c31\u4e3a false\u3002<\/p>\n<p>\u7136\u540e\u904d\u5386 match \u6570\u7ec4\uff0c\u5c06\u4e0d\u6ee1\u8db3\u6761\u4ef6\u7684\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u7f6e\u4e3a null\uff0c\u6700\u540e\u518d\u628a\u8fd9\u4e9b null \u79fb\u9664\u6389\u3002<\/p>\n<p>\u8fd9\u6837\u5c31\u83b7\u53d6\u5230\u4e86\u6211\u4eec\u9700\u8981\u8fdb\u884c\u81ea\u52a8\u5316\u914d\u7f6e\u7684\u7c7b\u4e86\u3002<\/p>\n<p>\u6700\u540e\u4e00\u53e5 fireAutoConfigurationImportEvents \u5219\u662f\u89e6\u53d1\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u5bfc\u5165\u4e8b\u4ef6\uff0c\u8fd9\u4e2a\u6ca1\u5565\u597d\u8bf4\u7684\uff5e<\/p>\n<p>\u5f53\u8fd9\u4e9b\u81ea\u52a8\u5316\u914d\u7f6e\u7c7b\u52a0\u8f7d\u8fdb\u6765\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u5c31\u662f\u5404\u79cd\u6761\u4ef6\u6ce8\u89e3\u6765\u51b3\u5b9a\u8fd9\u4e9b\u914d\u7f6e\u7c7b\u662f\u5426\u751f\u6548\u4e86\uff0c\u8fd9\u4e9b\u90fd\u6bd4\u8f83\u7b80\u5355\u4e86\uff0c\u4e4b\u524d\u5728 vhr \u79cd\u4e5f\u548c\u5c0f\u4f19\u4f34\u4eec\u8bb2\u8fc7\u591a\u6b21\u4e86\uff0c\u8fd9\u91cc\u5c31\u4e0d\u518d\u5570\u55e6\u4e86~<\/p>\n<p>\u597d\u5566\uff0c\u7ecf\u8fc7\u4e0a\u9762\u7684\u68b3\u7406\u76f8\u4fe1\u5c0f\u4f19\u4f34\u4eec\u5bf9 Spring Boot \u542f\u52a8\u539f\u7406\u6709\u4e00\u4e2a\u5927\u6982\u7684\u8ba4\u77e5\u4e86\u5427\uff5e<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045450727\">https:\/\/segmentfault.com\/a\/1190000045450727<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>@[toc]\u867d\u7136\u6211\u4eec\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0cSpring Boot \u4f7f\u7528\u975e\u5e38\u591a\uff0c\u7b97\u662f\u76ee\u524d Java \u5f00\u53d1\u9886\u57df\u4e00\u4e2a\u6807\u914d\u4e86\uff0c [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020379","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020379"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020379\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}