{"id":10008265,"date":"2023-09-11T16:15:11","date_gmt":"2023-09-11T08:15:11","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008265"},"modified":"2023-09-11T16:15:11","modified_gmt":"2023-09-11T08:15:11","slug":"spring-%e6%9d%a1%e4%bb%b6%e6%b3%a8%e8%a7%a3%e6%b2%a1%e7%94%9f%e6%95%88%ef%bc%9f%e5%92%8b%e5%9b%9e%e4%ba%8b","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008265","title":{"rendered":"Spring \u6761\u4ef6\u6ce8\u89e3\u6ca1\u751f\u6548\uff1f\u548b\u56de\u4e8b"},"content":{"rendered":"<p>\u6761\u4ef6\u6ce8\u89e3\u76f8\u4fe1\u5404\u4f4d\u5c0f\u4f19\u4f34\u90fd\u7528\u8fc7\uff0cSpring \u4e2d\u7684\u591a\u73af\u5883\u914d\u7f6e profile \u5e95\u5c42\u5c31\u662f\u901a\u8fc7\u6761\u4ef6\u6ce8\u89e3\u6765\u5b9e\u73b0\u7684\uff0c\u677e\u54e5\u5728\u4e4b\u524d\u7684 Spring \u89c6\u9891\u4e2d\u4e5f\u6709\u548c\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u8fc7\u6761\u4ef6\u6ce8\u89e3\u7684\u4f7f\u7528\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u6233\u8fd9\u91cc\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=zETcvLM3dIXfBRbmMDKXhA%3D%3D.x8JwOeu1VrRWwUgcbDcb2NCmrkiapZgxprnoSEx1VyvS2WJ2lqPgr%2BBM5YQtY%2FOJuqOZTNltRAwMkcidqxddrw%3D%3D\" rel=\"noopener noreferrer\">Spring\u6e90\u7801\u5e94\u8be5\u600e\u4e48\u5b66\uff1f<\/a>\u3002<\/p>\n<p>\u4ece Spring4.0 \u5f00\u59cb\uff0cSpring \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u6761\u4ef6\u6ce8\u89e3\uff1a ConfigurationCondition\u3002\u4ece\u540d\u5b57\u4e0a\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\u8fd9\u4e2a\u662f\u642d\u914d @Configuration \u6ce8\u89e3\u4e00\u8d77\u4f7f\u7528\u7684\uff0cConfigurationCondition \u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u6761\u4ef6\u5339\u914d\uff0c\u53ef\u4ee5\u5728\u914d\u7f6e\u6216\u8005 Bean \u6ce8\u518c\u7684\u65f6\u5019\u53bb\u8bc4\u4f30\u6761\u4ef6\u6ce8\u89e3\u662f\u5426\u6ee1\u8db3\u3002<\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u4e00\u4e2a\u7c7b\u4e0a\u5b58\u5728\u6761\u4ef6\u6ce8\u89e3\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u4e24\u4e2a\u8bc4\u4f30\u6761\u4ef6\u6ce8\u89e3\u662f\u5426\u6ee1\u8db3\u7684\u65f6\u673a\uff1a<\/p>\n<ol>\n<li>\u5728\u914d\u7f6e\u7684\u65f6\u5019\u53bb\u8bc4\u4f30\u3002<\/li>\n<li>\u5728 Bean \u6ce8\u518c\u7684\u65f6\u5019\u8bc4\u4f30\u3002<\/li>\n<\/ol>\n<p>\u5728\u914d\u7f6e\u7684\u65f6\u5019\u8bc4\u4f30\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f53\u524d\u7c7b\u90fd\u4e0d\u4f1a\u88ab\u52a0\u8f7d\uff0c\u5728 Bean \u6ce8\u518c\u7684\u65f6\u5019\u518d\u53bb\u8bc4\u4f30\uff0c\u610f\u5473\u7740\u5f53\u524d\u7c7b\u5c31\u4f1a\u88ab\u52a0\u8f7d\u3002<\/p>\n<h2>1. ConfigurationCondition<\/h2>\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e0b\u8fd9\u4e2a\u7c7b\u7684\u5b9a\u4e49\uff1a<\/p>\n<pre><code class=\"java\">public interface ConfigurationCondition extends Condition {\n    ConfigurationPhase getConfigurationPhase();\n    enum ConfigurationPhase {\n        PARSE_CONFIGURATION,\n        REGISTER_BEAN\n    }\n}<\/code><\/pre>\n<p>\u5927\u5bb6\u770b\u5230\uff0c\u8fd9\u91cc\u5176\u5b9e\u5c31\u662f\u5b9a\u4e49\u4e86\u4e24\u4e2a\u679a\u4e3e\u503c\uff0c\u7136\u540e\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u6cd5\u8fd4\u56de\u679a\u4e3e\u503c\u3002<\/p>\n<ul>\n<li>PARSE_CONFIGURATION\uff1a\u8fd9\u4e2a\u8868\u793a Condition \u6761\u4ef6\u5e94\u8be5\u5728\u89e3\u6790 @Configuration \u7c7b\u65f6\u8fdb\u884c\u8bc4\u4f30\uff0c\u5982\u679c\u8bc4\u4f30\u4e0d\u901a\u8fc7\uff0c\u5219\u4e0d\u4f1a\u5c06 @Configuration \u6dfb\u52a0\u5230\u5bb9\u5668\u4e2d\u3002<\/li>\n<li>REGISTER_BEAN\uff1a\u8fd9\u4e2a\u8868\u793a\u6dfb\u52a0\u5e38\u89c4 Bean \u7684\u65f6\u5019\u53bb\u8bc4\u4f30 Condition \u6761\u4ef6\uff08\u5e38\u89c4 Bean \u5c31\u662f\u6307\u975e\u914d\u7f6e\u7c7b\uff0c\u4f8b\u5982\u6dfb\u52a0\u642d\u914d @Bean \u6ce8\u89e3\u4f7f\u7528\u7684\u6761\u4ef6\u6ce8\u89e3\uff09\uff0c\u8fd9\u4e2a\u6761\u4ef6\u4e0d\u4f1a\u963b\u6b62\u6ce8\u518c @Configuration \u7c7b\u5230\u5bb9\u5668\u4e2d\u3002<\/li>\n<\/ul>\n<p>\u5176\u5b9e\u9053\u7406\u5f88\u597d\u61c2\uff0c\u5c31\u662f\u52a0\u8f7d\u914d\u7f6e\u7c7b\u7684\u65f6\u5019\u5c31\u6839\u636e\u6761\u4ef6\u6ce8\u89e3\u5224\u65ad\u8981\u4e0d\u8981\u52a0\u8f7d\u914d\u7f6e\u7c7b\uff0c\u8fd8\u662f\u7b49\u5230\u6ce8\u518c Bean \u7684\u65f6\u5019\u518d\u53bb\u770b\u6761\u4ef6\u6ce8\u89e3\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\u3002<\/p>\n<h2>2. \u6848\u4f8b\u5206\u6790<\/h2>\n<p>\u677e\u54e5\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u6848\u4f8b\u6765\u548c\u5c0f\u4f19\u4f34\u4eec\u6f14\u793a\u4e00\u4e0b\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u73b0\u5728\u6709\u5982\u4e0b\u6761\u4ef6\uff1a<\/p>\n<pre><code class=\"java\">public class MyCondition implements ConfigurationCondition {\n    @Override\n    public ConfigurationPhase getConfigurationPhase() {\n        return ConfigurationPhase.PARSE_CONFIGURATION;\n    }\n\n    @Override\n    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {\n        return context.getBeanFactory().containsBean(&quot;a&quot;);\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6761\u4ef6\u6211\u6ca1\u6709\u76f4\u63a5\u5b9e\u73b0 Condition \u63a5\u53e3\uff0c\u800c\u662f\u5b9e\u73b0\u7c7b ConfigurationCondition \u63a5\u53e3\uff0c\u5728\u8fd9\u4e2a\u63a5\u53e3\u4e2d\uff0cgetConfigurationPhase \u65b9\u6cd5\u8fd4\u56de\u4e86 PARSE_CONFIGURATION\uff0c\u8868\u793a\u5728\u52a0\u8f7d\u914d\u7f6e\u7c7b\u7684\u65f6\u5019\u5c31\u53bb\u8bc4\u4f30\u6761\u4ef6\u662f\u5426\u6ee1\u8db3\uff0cmatches \u65b9\u6cd5\u5219\u662f\u53bb\u5224\u65ad\u5bb9\u5668\u4e2d\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u540d\u4e3a a \u7684 Bean\u3002<\/p>\n<p>\u73b0\u5728\u6211\u6709\u4e24\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5206\u522b\u662f A \u548c B\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@Configuration\npublic class A {\n}\n@Configuration\n@Conditional(MyCondition.class)\npublic class B {\n}<\/code><\/pre>\n<p>A \u914d\u7f6e\u7c7b\u6b63\u5e38\u52a0\u8f7d\uff0cB \u914d\u7f6e\u7c7b\u6709\u4e00\u4e2a\u52a0\u8f7d\u6761\u4ef6\uff0c\u5c31\u662f\u5f97 A \u5b58\u5728\uff0cB \u624d\u4f1a\u52a0\u8f7d\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5728\u5bb9\u5668\u4e2d\u52a0\u8f7d B \u548c A \u4e24\u4e2a\u914d\u7f6e\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();\nctx.register(B.class,A.class);\nctx.refresh();\nString[] beanDefinitionNames = ctx.getBeanDefinitionNames();\nfor (String beanDefinitionName : beanDefinitionNames) {\n    System.out.println(beanDefinitionName);\n}<\/code><\/pre>\n<p>\u5927\u5bb6\u6ce8\u610f\uff0c\u52a0\u8f7d\u7684\u65f6\u5019\uff0c\u6211\u5148\u52a0\u8f7d\u4e86 B\uff0c\u540e\u52a0\u8f7d\u4e86 A\uff0c\u8fd9\u70b9\u5f88\u91cd\u8981\uff0c\u52a0\u8f7d B \u7684\u65f6\u5019\uff0c\u7531\u4e8e\u6b64\u65f6\u5bb9\u5668\u4e2d\u8fd8\u4e0d\u5b58\u5728\u4e00\u4e2a\u540d\u4e3a a \u7684 Bean\uff0c\u800c\u6211\u4eec\u7684\u8bc4\u4f30\u65f6\u673a\u662f\u5728\u5904\u7406\u914d\u7f6e\u7c7b\u7684\u65f6\u5019\uff0c\u56e0\u6b64\u5c31\u4f1a\u5bfc\u81f4 B \u914d\u7f6e\u7c7b\u4e0d\u4f1a\u88ab\u52a0\u8f7d\uff0c\u6700\u7ec8\u6253\u5370\u51fa\u6765\u7684 BeanName \u5c31\u6ca1\u6709 b\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u5c06 MyCondition \u4e2d\uff0c\u6761\u4ef6\u6ce8\u89e3\u7684\u8bc4\u4f30\u65f6\u673a\u6539\u4e3a <code>ConfigurationPhase.REGISTER_BEAN<\/code>\uff0c\u90a3\u4e48\u5c31\u8868\u793a\u5728\u7cfb\u7edf\u542f\u52a8\u7684\u65f6\u5019\uff0c\u5e76\u4e0d\u4f1a\u53bb\u8bc4\u4f30\u6761\u4ef6\u6ce8\u89e3\u662f\u5426\u6ee1\u8db3\uff0c\u800c\u662f\u4f1a\u5c06 @Configuration \u914d\u7f6e\u7c7b\u8fdb\u884c\u89e3\u6790\uff0c\u6b64\u65f6\u542f\u52a8\u7cfb\u7edf\uff0c\u5c31\u4f1a\u53d1\u73b0\u6700\u7ec8\u6253\u5370\u51fa\u6765\u7684 beanName \u91cc\u65e2\u6709 a \u53c8\u6709 b\u3002<\/p>\n<h2>3. \u6e90\u7801\u5206\u6790<\/h2>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u518d\u6765\u4ece\u6e90\u7801\u7684\u89d2\u5ea6\u6765\u5206\u6790\u4e00\u4e0b\u4e0a\u8ff0\u884c\u4e3a\u3002<\/p>\n<p>\u5728 Spring \u4e2d\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4e13\u95e8\u7684\u5185\u90e8\u7c7b ConditionEvaluator \u6765\u5904\u7406\u8981\u4e0d\u8981\u8df3\u8fc7\u6761\u4ef6\u6ce8\u89e3\uff0c\u8be5\u7c7b\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a shouldSkip \u7684\u65b9\u6cd5\uff0c\u7528\u6765\u5904\u7406\u6b64\u4e8b\uff1a<\/p>\n<pre><code class=\"java\">public boolean shouldSkip(AnnotatedTypeMetadata metadata) {\n    return shouldSkip(metadata, null);\n}\npublic boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable ConfigurationPhase phase) {\n    if (metadata == null || !metadata.isAnnotated(Conditional.class.getName())) {\n        return false;\n    }\n    if (phase == null) {\n        if (metadata instanceof AnnotationMetadata annotationMetadata &amp;&amp;\n                ConfigurationClassUtils.isConfigurationCandidate(annotationMetadata)) {\n            return shouldSkip(metadata, ConfigurationPhase.PARSE_CONFIGURATION);\n        }\n        return shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN);\n    }\n    List&lt;Condition&gt; conditions = new ArrayList&lt;&gt;();\n    for (String[] conditionClasses : getConditionClasses(metadata)) {\n        for (String conditionClass : conditionClasses) {\n            Condition condition = getCondition(conditionClass, this.context.getClassLoader());\n            conditions.add(condition);\n        }\n    }\n    AnnotationAwareOrderComparator.sort(conditions);\n    for (Condition condition : conditions) {\n        ConfigurationPhase requiredPhase = null;\n        if (condition instanceof ConfigurationCondition configurationCondition) {\n            requiredPhase = configurationCondition.getConfigurationPhase();\n        }\n        if ((requiredPhase == null || requiredPhase == phase) &amp;&amp; !condition.matches(this.context, metadata)) {\n            return true;\n        }\n    }\n    return false;\n}<\/code><\/pre>\n<p>\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u4e0d\u7528\u591a\u8bf4\uff0c\u6211\u4eec\u6765\u770b\u7b2c\u4e8c\u4e2a\u91cd\u8f7d\u65b9\u6cd5\uff0c\u91cd\u8f7d\u65b9\u6cd5\u591a\u4e86\u4e00\u4e2a\u53c2\u6570 ConfigurationPhase\uff0c\u8fd9\u4e2a\u5c31\u8868\u793a\u914d\u7f6e\u7684\u9636\u6bb5\uff0c\u4e5f\u5c31\u662f\u6761\u4ef6\u6ce8\u89e3\u751f\u6548\u7684\u9636\u6bb5\u3002<\/p>\n<p>\u9996\u5148\u4f1a\u53bb\u5224\u65ad\u5f53\u524d\u6ce8\u89e3\u662f\u5426\u662f\u4e00\u4e2a\u6761\u4ef6\u6ce8\u89e3\uff0c\u5982\u679c\u4e0d\u662f\u6761\u4ef6\u6ce8\u610f\uff0c\u90a3\u4e48\u5c31\u4e0d\u80fd\u8df3\u8fc7\uff0c\u8981\u7ee7\u7eed\u540e\u9762\u7684\u89e3\u6790\uff08\u7ee7\u7eed\u540e\u9762\u7684\u89e3\u6790\u65f6 Bean \u5c06\u4f1a\u88ab\u6ce8\u518c\uff09\uff0c\u5982\u679c\u662f\u6761\u4ef6\u6ce8\u89e3\uff0c\u5219\u7ee7\u7eed\u540e\u9762\u7684\u5224\u65ad\u3002\u7ee7\u7eed\u5224\u65ad\uff0c\u5982\u679c\u6ca1\u6709\u4f20\u9012 phase \u8fdb\u6765\uff0c\u8bf4\u660e\u6ca1\u6709\u6307\u5b9a\u5e94\u8be5\u5728\u54ea\u4e2a\u9636\u6bb5\u53bb\u8bc4\u4f30\u6761\u4ef6\u6ce8\u89e3\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\u5c31\u53bb\u5224\u65ad\uff0c\u5982\u679c\u5f53\u524d\u6ce8\u89e3\u662f\u4e00\u4e2a\u914d\u7f6e\u7c7b\u4e0a\u7684\u6ce8\u89e3\uff0c\u90a3\u4e48\u5c31\u8bbe\u7f6e phase \u4e3a PARSE_CONFIGURATION\uff0c\u7136\u540e\u7ee7\u7eed\u8c03\u7528 shouldSkip \u65b9\u6cd5\uff0c\u5426\u5219\u5c31\u8bbe\u7f6e phase \u4e3a REGISTER_BEAN \u7136\u540e\u7ee7\u7eed\u8c03\u7528 shouldSkip \u65b9\u6cd5\u3002<\/p>\n<blockquote><p>\u90a3\u4e48\u4ec0\u4e48\u6837\u7684\u60c5\u51b5\u4f1a\u88ab\u8ba4\u4e3a\u662f\u4e00\u4e2a\u914d\u7f6e\u7c7b\u4e0a\u7684\u6ce8\u89e3\u5462\uff1f\u5982\u679c\u5f53\u524d\u7c7b\u4e0a\u6dfb\u52a0\u7684\u6ce8\u89e3\u65f6 @Component\u3001@ComponentScan\u3001@Import\u3001@ImportResource \u4ee5\u53ca\u8fd9\u56db\u79cd\u6ce8\u89e3\u884d\u751f\u51fa\u6765\u7684\u6ce8\u89e3\uff0c\u4ea6\u6216\u8005\u5f53\u524d\u7c7b\u4e2d\u6709 @Bean \u6ce8\u89e3\u6807\u8bb0\u7684\u65b9\u6cd5\uff0c\u90a3\u4e48\u5f53\u524d\u7c7b\u5c31\u662f\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5c31\u4f1a\u8bbe\u7f6e phase \u4e3a PARSE_CONFIGURATION\u3002<\/p><\/blockquote>\n<p>\u7b2c\u4e8c\u6b21\u8fdb\u5165 shouldSkip \u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u5c31\u5df2\u7ecf\u6709\u660e\u786e\u7684 phase \u4e86\u3002\u8fd9\u6b21\u8fdb\u6765\u540e\uff0c\u628a\u6240\u6709\u7684\u6761\u4ef6\u6ce8\u89e3\u7684\u6761\u4ef6\u6536\u96c6\u8d77\u6765\uff0c\u5b58\u5165\u5230 conditions \u96c6\u5408\u4e2d\uff0c\u7136\u540e\u518d\u5bf9\u8be5\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3002\u7136\u540e\u904d\u5386\u8be5\u96c6\u5408\u3002\u904d\u5386\u7684\u65f6\u5019\u5c31\u53bb\u5224\u65ad\u8fd9\u4e2a\u6761\u4ef6\u6ce8\u89e3\u662f\u4e0d\u662f ConfigurationCondition \u7c7b\u578b\u7684\uff0c\u5982\u679c\u662f\uff0c\u5219\u63d0\u53d6\u51fa\u6765\u5176\u4e2d\u7684 phase \u4e3a requiredPhase\uff0c\u8fd9\u4e2a\u5c31\u8868\u793a\u8fd9\u4e2a\u6761\u4ef6\u6ce8\u610f\u5e0c\u671b\u81ea\u5df1\u88ab\u5904\u7406\u7684\u9636\u6bb5\uff0c\u63a5\u4e0b\u6765\u53bb\u5224\u65ad\uff0c\u5982\u679c requiredPhase \u4e3a\u7a7a\uff0c\u8bf4\u660e\u6761\u4ef6\u5e76\u672a\u6307\u5b9a\u81ea\u5df1\u7684\u6267\u884c\u65f6\u95f4\uff0c\u90a3\u4e48\u5c31\u6267\u884c matches \u65b9\u6cd5\u8fdb\u884c\u6761\u4ef6\u8bc4\u4f30\uff1b\u5982\u679c requiredPhase \u4e0d\u4e3a\u7a7a\uff0c\u5e76\u4e14\u548c\u4f20\u5165\u7684 phase \u76f8\u7b49\uff0c\u90a3\u4e48\u4e5f\u662f\u5f53\u524d\u8bc4\u4f30\u3002\u5176\u5b9e\u8fd9\u4e2a\u5224\u65ad\u6838\u5fc3\u903b\u8f91\u5c31\u662f\u4ee5\u53c2\u6570\u4f20\u5165\u8fdb\u6765\u7684 phase \u4e3a\u51c6\uff0c\u8981\u4e48\u6761\u4ef6\u6ca1\u6709\u8bbe\u7f6e\u8bc4\u4f30\u65f6\u673a\uff0c\u8981\u4e48\u8bbe\u7f6e\u4e86\uff0c\u4f46\u662f\u5f97\u548c\u53c2\u6570\u4f20\u8fdb\u6765\u7684 phase \u4e00\u81f4\uff0c\u53ea\u6709\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u6761\u4ef6\uff0c\u624d\u4f1a\u5f53\u573a\u8fdb\u884c\u8bc4\u4f30\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u7cfb\u7edf\u6761\u4ef6\u6ce8\u89e3\u7684\u8bc4\u4f30\u903b\u8f91\u3002<\/p>\n<p>\u5bf9\u4e8e\u914d\u7f6e\u7c7b\u6765\u8bf4\uff0c\u662f\u5728 <code>AnnotatedBeanDefinitionReader#doRegisterBean<\/code> \u65b9\u6cd5\u4e2d\u8c03\u7528\u8bc4\u4f30\u903b\u8f91\u7684\uff1a<\/p>\n<pre><code class=\"java\">private &lt;T&gt; void doRegisterBean(Class&lt;T&gt; beanClass, @Nullable String name,\n        @Nullable Class&lt;? extends Annotation&gt;[] qualifiers, @Nullable Supplier&lt;T&gt; supplier,\n        @Nullable BeanDefinitionCustomizer[] customizers) {\n    AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);\n    if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {\n        return;\n    }\n    \/\/...\n}<\/code><\/pre>\n<p>\u8c03\u7528\u7684\u65f6\u5019\u5e76\u672a\u660e\u786e\u6307\u5b9a phase\uff0c\u6240\u4ee5\u4f1a\u5728\u8fdb\u5165\u5230 shouldSkip \u65b9\u6cd5\u540e\uff0c\u81ea\u884c\u5206\u6790\u662f\u54ea\u4e2a\u9636\u6bb5\u8bc4\u4f30\u6761\u4ef6\u6ce8\u89e3\u3002<\/p>\n<p>\u5bf9\u4e8e @Bean \u6ce8\u89e3\u6807\u8bb0\u7684\u7c7b\u6765\u8bf4\uff0c\u662f\u5728 <code>ConfigurationClassBeanDefinitionReader#loadBeanDefinitionsForBeanMethod<\/code> \u65b9\u6cd5\u4e2d\u8c03\u7528\u8bc4\u4f30\u903b\u8f91\u7684\uff1a<\/p>\n<pre><code class=\"java\">private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {\n    ConfigurationClass configClass = beanMethod.getConfigurationClass();\n    MethodMetadata metadata = beanMethod.getMetadata();\n    String methodName = metadata.getMethodName();\n    if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {\n        configClass.skippedBeanMethods.add(methodName);\n        return;\n    }\n    \/\/...\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5c31\u4f20\u5165\u4e86 phase \u4e86\uff0c\u76f4\u63a5\u6307\u5b9a\u4e86\u662f\u5728 Bean \u521d\u59cb\u5316\u7684\u65f6\u5019\u8bc4\u4f30\u3002<\/p>\n<p>\u597d\u5566\uff0c\u8fd9\u5c31\u662f\u6761\u4ef6\u6ce8\u89e3\u6761\u4ef6\u8bc4\u4f30\u65f6\u673a\u7684\u4e24\u79cd\u60c5\u51b5\u3002\u5728 Spring Boot \u4e2d\u5b9a\u4e49\u7684\u6761\u4ef6\u6ce8\u89e3\u91cc\uff0c\u6709\u4e0d\u5c11\u90fd\u7528\u5230\u4e86 ConfigurationCondition\uff0c\u800c\u4e0d\u662f\u4f20\u7edf\u7684 Condition\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u81ea\u884c\u67e5\u770b\u54e6\uff5e<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044203657\">https:\/\/segmentfault.com\/a\/1190000044203657<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6761\u4ef6\u6ce8\u89e3\u76f8\u4fe1\u5404\u4f4d\u5c0f\u4f19\u4f34\u90fd\u7528\u8fc7\uff0cSpring \u4e2d\u7684\u591a\u73af\u5883\u914d\u7f6e profile \u5e95\u5c42\u5c31\u662f\u901a\u8fc7\u6761\u4ef6\u6ce8\u89e3\u6765\u5b9e\u73b0\u7684\uff0c\u677e\u54e5 [&hellip;]<\/p>\n","protected":false},"author":1,"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-10008265","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\/10008265","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008265"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008265\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}