{"id":10008188,"date":"2023-08-29T15:01:41","date_gmt":"2023-08-29T07:01:41","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008188"},"modified":"2023-08-29T15:01:41","modified_gmt":"2023-08-29T07:01:41","slug":"%e4%b8%80%e9%81%93%e7%bb%8f%e5%85%b8%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9aconfiguration-%e5%92%8c-component-%e6%9c%89%e4%bd%95%e5%8c%ba%e5%88%ab%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008188","title":{"rendered":"\u4e00\u9053\u7ecf\u5178\u9762\u8bd5\u9898\uff1a@Configuration \u548c @Component \u6709\u4f55\u533a\u522b\uff1f"},"content":{"rendered":"<p>\u5173\u4e8e @Configuration \u6ce8\u89e3\u6709\u4e00\u4e2a\u7279\u522b\u7ecf\u5178\u7684\u9762\u8bd5\u9898\uff1a<\/p>\n<ul>\n<li>@Configuration \u548c @Component \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/li>\n<\/ul>\n<p>\u65e0\u8bba\u5c0f\u4f19\u4f34\u4eec\u4e4b\u524d\u662f\u5426\u80cc\u8fc7\u76f8\u5173\u7684\u9762\u8bd5\u9898\uff0c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u5b66\u5b8c\u4e4b\u540e\u76f8\u4fe1\u5927\u5bb6\u5bf9\u8fd9\u4e2a\u95ee\u9898\u90fd\u4f1a\u6709\u66f4\u6df1\u4e00\u5c42\u7684\u7406\u89e3\uff0c\u5e9f\u8bdd\u4e0d\u591a\u5c11\uff0c\u54b1\u4eec\u5f00\u59cb\u5206\u6790\u3002<\/p>\n<h2>1. \u60c5\u666f\u5c55\u73b0<\/h2>\n<p>@Configuration \u548c @Component \u5230\u5e95\u6709\u4f55\u533a\u522b\u5462\uff1f\u6211\u5148\u901a\u8fc7\u5982\u4e0b\u4e00\u4e2a\u6848\u4f8b\uff0c\u5728\u4e0d\u5206\u6790\u6e90\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u5c0f\u4f19\u4f34\u4eec\u5148\u6765\u76f4\u89c2\u611f\u53d7\u4e00\u4e0b\u8fd9\u4e24\u4e2a\u4e4b\u95f4\u7684\u533a\u522b\u3002<\/p>\n<pre><code class=\"java\">@Configuration\npublic class JavaConfig01 {\n}\n@Component\npublic class JavaConfig02 {\n}<\/code><\/pre>\n<p>\u9996\u5148\uff0c\u5206\u522b\u5411 Spring \u5bb9\u5668\u4e2d\u6ce8\u5165\u4e24\u4e2a Bean\uff0cJavaConfig01 \u548c JavaConfig02\uff0c\u5176\u4e2d\uff0cJavaConfig01 \u4e0a\u6dfb\u52a0\u7684\u662f @Configuration \u6ce8\u89e3\u800c JavaConfig02 \u4e0a\u6dfb\u52a0\u7684\u5219\u662f @Component \u6ce8\u89e3\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5728 XML \u6587\u4ef6\u4e2d\u914d\u7f6e\u5305\u626b\u63cf\uff1a<\/p>\n<pre><code class=\"xml\">&lt;context:component-scan \nbase-package=&quot;org.javaboy.demo.p6&quot;\/&gt;<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u52a0\u8f7d XML \u914d\u7f6e\u6587\u4ef6\uff0c\u521d\u59cb\u5316\u5bb9\u5668\uff1a<\/p>\n<pre><code class=\"java\">public class Demo {\n    public static void main(String[] args) {\n        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;beans_demo.xml&quot;);\n        JavaConfig01 config01 = ctx.getBean(JavaConfig01.class);\n        JavaConfig02 config02 = ctx.getBean(JavaConfig02.class);\n        System.out.println(&quot;config01.getClass() = &quot; + config01.getClass());\n        System.out.println(&quot;config02.getClass() = &quot; + config02.getClass());\n    }\n}<\/code><\/pre>\n<p>\u6700\u7ec8\u6253\u5370\u51fa\u6765\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044160450\" alt=\"\" title=\"\"><\/p>\n<p>\u4ece\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u6765\u4e24\u4e2a\u7ed3\u8bba\uff1a<\/p>\n<ol>\n<li>@Configuration \u6ce8\u89e3\u4e5f\u662f Spring \u7ec4\u4ef6\u6ce8\u89e3\u7684\u4e00\u79cd\uff0c\u901a\u8fc7\u666e\u901a\u7684 Bean \u626b\u63cf\u4e5f\u53ef\u4ee5\u626b\u63cf\u5230 @Configuration\u3002<\/li>\n<li>@Configuration \u6ce8\u89e3\u6ce8\u518c\u5230 Spring \u4e2d\u7684 Bean \u662f\u4e00\u4e2a CGLIB \u4ee3\u7406\u7684 Bean\uff0c\u800c\u4e0d\u662f\u539f\u59cb Bean\uff0c\u8fd9\u4e00\u70b9\u548c @Component \u4e0d\u4e00\u6837\uff0c@Component \u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e2d\u7684\u8fd8\u662f\u539f\u59cb Bean\u3002<\/li>\n<\/ol>\n<p>\u4e00\u4e2a\u95ee\u9898\u6765\u4e86\uff0c@Configuration \u6807\u8bb0\u7684\u7c7b\u4e3a\u4ec0\u4e48\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u4e4b\u540e\u5c31\u53d8\u6210\u4e86\u4ee3\u7406\u5bf9\u8c61\u4e86\u5462\uff1f\u95ed\u7740\u773c\u775b\u5927\u5bb6\u4e5f\u80fd\u731c\u5230\uff0c\u80af\u5b9a\u662f\u4e3a\u4e86\u901a\u8fc7\u4ee3\u7406\u6765\u589e\u5f3a\u5176\u529f\u80fd\uff0c\u90a3\u4e48\u7a76\u7adf\u589e\u5f3a\u4ec0\u4e48\u529f\u80fd\u5462\uff1f\u63a5\u4e0b\u6765\u6211\u4eec\u901a\u8fc7\u6e90\u7801\u5206\u6790\u6765\u548c\u5c0f\u4f19\u4f34\u4eec\u68b3\u7406\u4e00\u4e0b\u8fd9\u91cc\u7684\u6761\u6761\u6846\u6846\u3002<\/p>\n<h2>2. \u6e90\u7801\u5206\u6790<\/h2>\n<p>\u8981\u7406\u89e3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9996\u5148\u5f97\u7ed3\u5408\u6211\u4eec\u524d\u9762\u7684\u6587\u7ae0<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=0Uwr1S4%2BVnnsa1p4JRsBng%3D%3D.TjDIX1MOkKPK35r7h33OtFJOmCky2clv4KTP4PlOOPRyihi17gtDPjkQcMIS204VJsQfqUpXiBWpHNl%2BXTUtFQ%3D%3D\" rel=\"noopener noreferrer\">@Configuration \u6ce8\u89e3\u7684 Full \u6a21\u5f0f\u548c Lite \u6a21\u5f0f\uff01<\/a>\uff0c\u5728\u8be5\u6587\u4e2d\uff0c\u677e\u54e5\u63d0\u5230\u4e86 @Configuration \u6a21\u5f0f\u5206\u4e3a\u4e86 Full \u6a21\u5f0f\u548c Lite \u6a21\u5f0f\uff0c\u6240\u4ee5\uff0c\u5bf9\u4e8e @Configuration \u6ce8\u89e3\u7684\u5904\u7406\uff0c\u5728\u52a0\u8f7d\u7684\u65f6\u5019\uff0c\u5c31\u9700\u8981\u9996\u5148\u533a\u5206\u51fa\u6765\u662f Full \u6a21\u5f0f\u8fd8\u662f Lite \u6a21\u5f0f\u3002<\/p>\n<p>\u8d1f\u8d23 @Configuration \u6ce8\u89e3\u7684\u662f ConfigurationClassPostProcessor\uff0c\u8fd9\u4e2a\u5904\u7406\u5668\u662f\u4e00\u4e2a BeanFactoryPostProcessor\uff0cBeanFactoryPostProcessor \u7684\u4f5c\u7528\u5c31\u662f\u5728 Bean \u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u901a\u8fc7\u4fee\u6539 BeanDefinition \u6765\u91cd\u65b0\u5b9a\u4e49 Bean \u7684\u884c\u4e3a\uff0c\u8fd9\u4e2a\u677e\u54e5\u4e4b\u524d\u6709\u8fc7\u4e13\u95e8\u7684\u6587\u7ae0\u4ecb\u7ecd\uff0c\u4e0d\u719f\u6089\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u5148\u770b\u770b\u8fd9\u91cc\uff1a<\/p>\n<ul>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=VRk5gxFSFdylyodduoLQ%2Fg%3D%3D.DuCqDdu2jWyFQX6G1uh3HFp%2BloQHYn8vKS6ygAYsDdl1dMNz%2F%2FpUIYIouVpZweQJ9rR%2BGjz7rOIqrhfp%2FYsl4g%3D%3D\" rel=\"noopener noreferrer\">Spring \u4e2d BeanFactory \u548c FactoryBean \u6709\u4f55\u533a\u522b\uff1f<\/a><\/li>\n<\/ul>\n<p>\u540c\u65f6\uff0cConfigurationClassPostProcessor \u4e5f\u662f BeanDefinitionRegistryPostProcessor \u7684\u5b9e\u4f8b\uff0cBeanDefinitionRegistryPostProcessor \u662f\u5e72\u561b\u7684\u5462\uff1f<\/p>\n<p>BeanDefinitionRegistryPostProcessor \u662f Spring \u6846\u67b6\u4e2d\u7684\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5728\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u542f\u52a8\u65f6\uff0c\u5bf9 BeanDefinitionRegistry \u8fdb\u884c\u540e\u7f6e\u5904\u7406\u3002\u5177\u4f53\u6765\u8bf4\uff0cBeanDefinitionRegistryPostProcessor \u53ef\u4ee5\u7528\u4e8e\u4fee\u6539\u6216\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u4e2d\u7684 BeanDefinition\uff0c\u5373\u5728 Bean \u5b9e\u4f8b\u5316\u4e4b\u524d\u5bf9 BeanDefinition \u8fdb\u884c\u4fee\u6539\u3002\u5b83\u53ef\u4ee5\u6dfb\u52a0\u3001\u5220\u9664\u6216\u4fee\u6539 BeanDefinition \u7684\u5c5e\u6027\uff0c\u751a\u81f3\u53ef\u4ee5\u52a8\u6001\u5730\u6ce8\u518c\u65b0\u7684 BeanDefinition\u3002\u901a\u8fc7\u5b9e\u73b0 BeanDefinitionRegistryPostProcessor \u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 Spring \u5bb9\u5668\u542f\u52a8\u8fc7\u7a0b\u4e2d\u5e72\u9884 Bean \u7684\u5b9a\u4e49\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u52a0\u8f7d\u4e4b\u524d\u5bf9 Bean \u8fdb\u884c\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u52a8\u6001\u6ce8\u518c Bean \u6216\u8005\u4fee\u6539 Bean \u7684\u5c5e\u6027\u3002<strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cBeanDefinitionRegistryPostProcessor \u5728 BeanFactoryPostProcessor \u4e4b\u524d\u88ab\u8c03\u7528\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u5f71\u54cd\u5230 BeanFactoryPostProcessor \u7684\u884c\u4e3a\u3002<\/strong><\/p>\n<p>BeanFactoryPostProcessor \u4e2d\u7684\u65b9\u6cd5\u662f postProcessBeanFactory\uff0c\u800c BeanDefinitionRegistryPostProcessor \u4e2d\u7684\u65b9\u6cd5\u662f postProcessBeanDefinitionRegistry\uff0c\u6839\u636e\u524d\u9762\u7684\u4ecb\u7ecd\uff0cpostProcessBeanDefinitionRegistry \u65b9\u6cd5\u5c06\u5728 postProcessBeanFactory \u65b9\u6cd5\u4e4b\u524d\u6267\u884c\u3002<\/p>\n<p>\u6240\u4ee5\uff0c\u6211\u4eec\u5c31\u4ece postProcessBeanDefinitionRegistry \u65b9\u6cd5\u5f00\u59cb\u770b\u8d77\u5427\uff5e<\/p>\n<h3>2.1 postProcessBeanDefinitionRegistry<\/h3>\n<pre><code class=\"java\">@Override\npublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {\n    int registryId = System.identityHashCode(registry);\n    if (this.registriesPostProcessed.contains(registryId)) {\n        throw new IllegalStateException(\n                &quot;postProcessBeanDefinitionRegistry already called on this post-processor against &quot; + registry);\n    }\n    if (this.factoriesPostProcessed.contains(registryId)) {\n        throw new IllegalStateException(\n                &quot;postProcessBeanFactory already called on this post-processor against &quot; + registry);\n    }\n    this.registriesPostProcessed.add(registryId);\n    processConfigBeanDefinitions(registry);\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u9762\u524d\u9762\u7684\u4ee3\u7801\u4e3b\u8981\u662f\u4e3a\u4e86\u786e\u4fdd\u8be5\u65b9\u6cd5\u6267\u884c\u4e00\u6b21\uff0c\u6211\u4eec\u5c31\u4e0d\u591a\u8bf4\u4e86\u3002\u5173\u952e\u5728\u4e8e\u6700\u540e\u7684 processConfigBeanDefinitions \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u7528\u6765\u51b3\u7b56\u914d\u7f6e\u7c7b\u662f Full \u6a21\u5f0f\u8fd8\u662f Lite \u6a21\u5f0f\u7684\u3002<\/p>\n<pre><code class=\"java\">public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {\n    List&lt;BeanDefinitionHolder&gt; configCandidates = new ArrayList&lt;&gt;();\n    String[] candidateNames = registry.getBeanDefinitionNames();\n    for (String beanName : candidateNames) {\n        BeanDefinition beanDef = registry.getBeanDefinition(beanName);\n        if (beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE) != null) {\n            if (logger.isDebugEnabled()) {\n                logger.debug(&quot;Bean definition has already been processed as a configuration class: &quot; + beanDef);\n            }\n        }\n        else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {\n            configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));\n        }\n    }\n    \/\/\u7701\u7565\u3002\u3002\u3002\n}<\/code><\/pre>\n<p>\u6211\u7701\u7565\u4e86\u5176\u4ed6\u4ee3\u7801\uff0c\u5927\u5bb6\u770b\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u4f1a\u9996\u5148\u6839\u636e beanName \u53d6\u51fa\u6765 BeanDefinition\uff0c\u7136\u540e\u5224\u65ad BeanDefinition \u4e2d\u662f\u5426\u5305\u542b <code>ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE<\/code> \u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u4e0a\u8bb0\u5f55\u4e86\u5f53\u524d\u914d\u7f6e\u7c7b\u662f Full \u6a21\u5f0f\u8fd8\u662f Lite \u6a21\u5f0f\uff0c\u4e0d\u540c\u6a21\u5f0f\u5c06\u6765\u7684\u5904\u7406\u65b9\u6848\u80af\u5b9a\u4e5f\u662f\u4e0d\u540c\u7684\u3002\u5982\u679c\u662f\u7b2c\u4e00\u6b21\u5904\u7406\uff0c\u663e\u7136 BeanDefinition \u4e2d\u5e76\u4e0d\u5305\u542b\u8be5\u5c5e\u6027\uff0c\u56e0\u6b64\u5c31\u4f1a\u8fdb\u5165\u5230 ConfigurationClassUtils.checkConfigurationClassCandidate \u65b9\u6cd5\u4e2d\uff0c\u6b63\u662f\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0c\u5224\u65ad\u5f53\u524d\u914d\u7f6e\u7c7b\u662f Full \u6a21\u5f0f\u8fd8\u662f Lite \u6a21\u5f0f\uff0c\u5e76\u8fdb\u884c\u6807\u8bb0\uff0ccheckConfigurationClassCandidate \u65b9\u6cd5\u7684\u903b\u8f91\u4e5f\u633a\u957f\u7684\uff0c\u6211\u8fd9\u91cc\u6311\u51fa\u6765\u8ddf\u6211\u4eec\u611f\u5174\u8da3\u7684\u90e8\u5206\uff1a<\/p>\n<pre><code class=\"java\">static boolean checkConfigurationClassCandidate(\n        BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) {\n        \/\/\u7701\u7565\u3002\u3002\u3002\n    Map&lt;String, Object&gt; config = metadata.getAnnotationAttributes(Configuration.class.getName());\n    if (config != null &amp;&amp; !Boolean.FALSE.equals(config.get(&quot;proxyBeanMethods&quot;))) {\n        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);\n    }\n    else if (config != null || isConfigurationCandidate(metadata)) {\n        beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);\n    }\n    else {\n        return false;\n    }\n    \/\/\u7701\u7565\n}<\/code><\/pre>\n<p>Full \u6a21\u5f0f\u60c5\u51b5\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5982\u679c\u914d\u7f6e\u7c7b\u4e0a\u5b58\u5728 @Configuration \u6ce8\u89e3\uff0c\u5e76\u4e14\u8be5\u6ce8\u89e3\u7684 proxyBeanMethods \u5c5e\u6027\u503c\u4e0d\u4e3a false\uff0c\u90a3\u4e48\u5c31\u662f Full \u6a21\u5f0f\uff0c\u8fd9\u4e2a\u8ddf\u677e\u54e5\u5728 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=02w%2B1GRqKpeTY3P%2BZMiEjw%3D%3D.J5%2FNORIfPV6FmX61fEAw9Cfq9mEif3%2BJhuu5nchcb6Guu6b4usZgor0rA9s1MgGofKzHEK1R490%2FS7I1HIcJOg%3D%3D\" rel=\"noopener noreferrer\">@Configuration \u6ce8\u89e3\u7684 Full \u6a21\u5f0f\u548c Lite \u6a21\u5f0f\uff01<\/a>\u4e00\u6587\u4e2d\u7684\u4ecb\u7ecd\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<p>Lite \u6a21\u5f0f\u5c31\u60c5\u51b5\u591a\u4e00\u4e9b\uff0c\u9996\u5148 <code>config!=null<\/code> \u5c31\u662f\u8bf4\u73b0\u5728\u4e5f\u5b58\u5728 @Configuration \u6ce8\u89e3\uff0c\u4f46\u662f proxyBeanMethods \u5c5e\u6027\u503c\u6b64\u65f6\u4e3a false\uff0c\u90a3\u4e48\u5c31\u662f Lite \u6a21\u5f0f\uff08proxyBeanMethods \u5c5e\u6027\u503c\u4e3a true \u7684\u8bdd\u5c31\u8fdb\u5165\u5230 if \u5206\u652f\u4e2d\u4e86\uff09\u3002<\/p>\n<p>\u53e6\u5916\u5c31\u662f\u5728 isConfigurationCandidate \u65b9\u6cd5\u4e2d\u6709\u4e00\u4e9b\u5224\u65ad\u903b\u8f91\u53bb\u9501\u5b9a\u662f\u5426\u4e3a Lite \u6a21\u5f0f\uff1a<\/p>\n<pre><code class=\"java\">static boolean isConfigurationCandidate(AnnotationMetadata metadata) {\n    \/\/ Do not consider an interface or an annotation...\n    if (metadata.isInterface()) {\n        return false;\n    }\n    \/\/ Any of the typical annotations found?\n    for (String indicator : candidateIndicators) {\n        if (metadata.isAnnotated(indicator)) {\n            return true;\n        }\n    }\n    \/\/ Finally, let's look for @Bean methods...\n    return hasBeanMethods(metadata);\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u5224\u65ad\u903b\u8f91\u662f\u8fd9\u6837\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\u6ce8\u89e3\u8981\u662f\u6807\u8bb0\u7684\u662f\u63a5\u53e3\uff0c\u90a3\u5c31\u4e0d\u80fd\u7b97\u662f Lite \u6a21\u5f0f\u3002<\/li>\n<li>\u904d\u5386 candidateIndicators\uff0c\u5224\u65ad\u5f53\u524d\u7c7b\u4e0a\u662f\u5426\u5305\u542b\u8fd9\u4e2a Set \u96c6\u5408\u4e2d\u7684\u6ce8\u89e3\uff0c\u8fd9\u4e2a Set \u96c6\u5408\u4e2d\u7684\u6ce8\u89e3\u6709\u56db\u4e2a\uff0c\u5206\u522b\u662f @Component\u3001@ComponentScan\u3001@Import\u3001@ImportResource \u56db\u4e2a\uff0c\u4e5f\u5c31\u662f\uff0c\u5982\u679c\u7c7b\u4e0a\u6807\u8bb0\u7684\u662f\u8fd9\u56db\u4e2a\u6ce8\u89e3\u7684\u8bdd\uff0c\u90a3\u4e48\u4e5f\u6309\u7167 Lite \u6a21\u5f0f\u5904\u7406\u3002<\/li>\n<li>\u5224\u65ad\u5f53\u524d\u7c7b\u4e2d\u662f\u5426\u6709 @Bean \u6807\u8bb0\u7684\u65b9\u6cd5\uff0c\u5982\u679c\u6709\u5219\u6309\u7167 Lite \u6a21\u5f0f\u5904\u7406\uff0c\u5426\u5219\u5c31\u4e0d\u662f Lite \u6a21\u5f0f\u3002<\/li>\n<\/ol>\n<p>\u5982\u679c\u5c0f\u4f19\u4f34\u4eec\u770b\u8fc7\u677e\u54e5\u4e4b\u524d\u7684 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=fk3ppMv2OQuprU7gey3%2Btw%3D%3D.BlX1GnKc4QMRGImREECxXynutxCbwFMDRE0WmgdilAbsXiaazyTJZ1TNNi5csaDLWBoQ3OJi9wj%2FXERTTVFbSw%3D%3D\" rel=\"noopener noreferrer\">@Configuration \u6ce8\u89e3\u7684 Full \u6a21\u5f0f\u548c Lite \u6a21\u5f0f\uff01<\/a>\u4e00\u6587\uff0c\u90a3\u4e48\u4e0a\u9762\u8fd9\u4e9b\u4ee3\u7801\u5e94\u8be5\u90fd\u5f88\u597d\u7406\u89e3\uff0c\u8ddf\u677e\u54e5\u5728\u8be5\u6587\u7ae0\u4e2d\u7684\u4ecb\u7ecd\u90fd\u662f\u4e00\u81f4\u7684\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u7ecf\u8fc7\u4e0a\u9762\u7684\u5904\u7406\uff0c\u73b0\u5728\u5c31\u5df2\u7ecf\u6807 BeanDefinition \u4e2d\u6807\u8bb0\u4e86\u8fd9\u4e2a\u914d\u7f6e\u7c7b\u5230\u5e95\u662f Full \u6a21\u5f0f\u8fd8\u662f Lite \u6a21\u5f0f\u4e86\u3002<\/p>\n<h3>2.2 postProcessBeanFactory<\/h3>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u6765\u770b postProcessBeanFactory \u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"java\">\/**\n * Prepare the Configuration classes for servicing bean requests at runtime\n * by replacing them with CGLIB-enhanced subclasses.\n *\/\n@Override\npublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {\n    int factoryId = System.identityHashCode(beanFactory);\n    if (this.factoriesPostProcessed.contains(factoryId)) {\n        throw new IllegalStateException(\n                &quot;postProcessBeanFactory already called on this post-processor against &quot; + beanFactory);\n    }\n    this.factoriesPostProcessed.add(factoryId);\n    if (!this.registriesPostProcessed.contains(factoryId)) {\n        \/\/ BeanDefinitionRegistryPostProcessor hook apparently not supported...\n        \/\/ Simply call processConfigurationClasses lazily at this point then.\n        processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);\n    }\n    enhanceConfigurationClasses(beanFactory);\n    beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));\n}<\/code><\/pre>\n<p>\u9996\u5148\u5927\u5bb6\u770b\u4e00\u4e0b\u8fd9\u4e2a\u65b9\u6cd5\u7684\u6ce8\u91ca\uff0c\u6ce8\u91ca\u8bf4\u7684\u5f88\u660e\u786e\u4e86\uff0c\u5c06 Configuration \u7c7b\u901a\u8fc7 CGLIB \u8fdb\u884c\u589e\u5f3a\uff0c\u4ee5\u4fbf\u5728\u8fd0\u884c\u65f6\u8f83\u597d\u7684\u5904\u7406 Bean \u8bf7\u6c42\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u8fd8\u4f1a\u518d\u6b21\u786e\u8ba4\u4e00\u4e0b postProcessBeanDefinitionRegistry \u65b9\u6cd5\u5df2\u7ecf\u5904\u7406\u8fc7\u4e86\uff0c\u5982\u679c\u6ca1\u6709\u5904\u7406\u7684\u8bdd\uff0c\u5219\u4f1a\u5728\u8be5\u65b9\u6cd5\u4e2d\u8c03\u7528 processConfigBeanDefinitions \u53bb\u786e\u8ba4 Bean \u4f7f\u7528\u7684\u662f\u54ea\u79cd\u6a21\u5f0f\u3002<\/p>\n<p>\u8be5\u65b9\u6cd5\u7684\u5173\u952e\u5728\u4e8e enhanceConfigurationClasses\uff0c\u8fd9\u4e2a\u5c31\u662f\u7528\u6765\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u589e\u5f3a\u914d\u7f6e\u7c7b\u7684\uff0c\u5f53\u7136\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u662f\u6bd4\u8f83\u957f\u7684\uff0c\u6211\u8fd9\u91cc\u5217\u51fa\u6765\u4e00\u4e9b\u5173\u952e\u7684\u903b\u8f91\uff1a<\/p>\n<pre><code class=\"java\">public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory) {\n    StartupStep enhanceConfigClasses = this.applicationStartup.start(&quot;spring.context.config-classes.enhance&quot;);\n    Map&lt;String, AbstractBeanDefinition&gt; configBeanDefs = new LinkedHashMap&lt;&gt;();\n    for (String beanName : beanFactory.getBeanDefinitionNames()) {\n        BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);\n        Object configClassAttr = beanDef.getAttribute(ConfigurationClassUtils.CONFIGURATION_CLASS_ATTRIBUTE);\n        if ((configClassAttr != null || methodMetadata != null) &amp;&amp;\n                (beanDef instanceof AbstractBeanDefinition abd) &amp;&amp; !abd.hasBeanClass()) {\n            \/\/ Configuration class (full or lite) or a configuration-derived @Bean method\n            \/\/ -&gt; eagerly resolve bean class at this point, unless it's a 'lite' configuration\n            \/\/ or component class without @Bean methods.\n            boolean liteConfigurationCandidateWithoutBeanMethods =\n                    (ConfigurationClassUtils.CONFIGURATION_CLASS_LITE.equals(configClassAttr) &amp;&amp;\n                        annotationMetadata != null &amp;&amp; !ConfigurationClassUtils.hasBeanMethods(annotationMetadata));\n            if (!liteConfigurationCandidateWithoutBeanMethods) {\n                try {\n                    abd.resolveBeanClass(this.beanClassLoader);\n                }\n            }\n        }\n        if (ConfigurationClassUtils.CONFIGURATION_CLASS_FULL.equals(configClassAttr)) {\n            configBeanDefs.put(beanName, abd);\n        }\n    }\n    \n    ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer();\n    for (Map.Entry&lt;String, AbstractBeanDefinition&gt; entry : configBeanDefs.entrySet()) {\n        AbstractBeanDefinition beanDef = entry.getValue();\n        \/\/ If a @Configuration class gets proxied, always proxy the target class\n        beanDef.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);\n        \/\/ Set enhanced subclass of the user-specified bean class\n        Class&lt;?&gt; configClass = beanDef.getBeanClass();\n        Class&lt;?&gt; enhancedClass = enhancer.enhance(configClass, this.beanClassLoader);\n        if (configClass != enhancedClass) {\n            beanDef.setBeanClass(enhancedClass);\n        }\n    }\n    enhanceConfigClasses.tag(&quot;classCount&quot;, () -&gt; String.valueOf(configBeanDefs.keySet().size())).end();\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u7684\u903b\u8f91\uff0c\u6211\u6574\u4f53\u4e0a\u5c06\u4e4b\u5206\u4e3a\u4e24\u90e8\u5206\uff1a<\/p>\n<p>\u7b2c\u4e00\u90e8\u5206\u5c31\u662f\u5148\u627e\u5230 Full \u6a21\u5f0f\u7684\u914d\u7f6e\u7c7b\u7684\u540d\u79f0\uff0c\u5b58\u5165\u5230 configBeanDefs \u96c6\u5408\u4e2d\u3002<\/p>\n<p>\u5177\u4f53\u5bfb\u627e\u7684\u903b\u8f91\u5c31\u662f\u6839\u636e\u914d\u7f6e\u7c7b\u7684\u6a21\u5f0f\u53bb\u5bfb\u627e\uff0c\u5982\u679c\u914d\u7f6e\u7c7b\u662f Full \u6a21\u5f0f\uff0c\u5c31\u5c06\u4e4b\u5b58\u5165\u5230 configBeanDefs \u4e2d\u3002\u5982\u679c\u914d\u7f6e\u7c7b\u662f Lite \u6a21\u5f0f\uff0c\u4e14\u91cc\u8fb9\u6ca1\u6709 @Bean \u6807\u8bb0\u7684\u65b9\u6cd5\uff0c\u90a3\u5c31\u8bf4\u660e\u8fd9\u53ef\u80fd\u5e76\u4e0d\u662f\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u5c31\u662f\u4e00\u4e2a\u666e\u901a Bean\uff0c\u90a3\u4e48\u5c31\u5728\u8fd9\u91cc\u52a0\u8f7d\u7c7b\u5c31\u884c\u4e86\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b65\u5219\u662f\u904d\u5386 configBeanDefs \u96c6\u5408\uff0c\u589e\u5f3a\u914d\u7f6e\u7c7b\u3002<\/p>\n<p>\u8fd9\u4e2a\u5982\u679c\u5927\u5bb6\u4e86\u89e3 CGLIB \u52a8\u6001\u4ee3\u7406\u7684\u8bdd\uff0c\u8fd9\u4e2a\u5c31\u5f88\u597d\u61c2\u4e86\uff0c\u5173\u4e8e CGLIB \u52a8\u6001\u4ee3\u7406\u677e\u54e5\u8fd9\u91cc\u4e0d\u5570\u55e6\uff0c\u6700\u8fd1\u66f4\u65b0\u7684 Spring \u6e90\u7801\u89c6\u9891\u4e2d\u90fd\u6709\u8be6\u7ec6\u8bb2\u5230\u3002\u90a3\u4e48\u8fd9\u91cc\u4e3b\u8981\u662f\u901a\u8fc7 enhancer.enhance \u65b9\u6cd5\u6765\u751f\u6210\u4ee3\u7406\u7c7b\u7684\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">public Class&lt;?&gt; enhance(Class&lt;?&gt; configClass, @Nullable ClassLoader classLoader) {\n    if (EnhancedConfiguration.class.isAssignableFrom(configClass)) {\n        return configClass;\n    }\n    Class&lt;?&gt; enhancedClass = createClass(newEnhancer(configClass, classLoader));\n    return enhancedClass;\n}\nprivate Enhancer newEnhancer(Class&lt;?&gt; configSuperClass, @Nullable ClassLoader classLoader) {\n    Enhancer enhancer = new Enhancer();\n    enhancer.setSuperclass(configSuperClass);\n    enhancer.setInterfaces(new Class&lt;?&gt;[] {EnhancedConfiguration.class});\n    enhancer.setUseFactory(false);\n    enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);\n    enhancer.setAttemptLoad(true);\n    enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));\n    enhancer.setCallbackFilter(CALLBACK_FILTER);\n    enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());\n    return enhancer;\n}<\/code><\/pre>\n<p>\u5c0f\u4f19\u4f34\u4eec\u770b\u5230\uff0c\u589e\u5f3a\u7c7b\u4e2d\u7684 setCallbackFilter \u662f CALLBACK_FILTER\uff0c\u8fd9\u4e2a\u91cc\u8fb9\u5305\u542b\u4e86\u51e0\u4e2a\u65b9\u6cd5\u62e6\u622a\u5668\uff0c\u8ddf\u6211\u4eec\u76f8\u5173\u7684\u662f BeanMethodInterceptor\uff0c\u6211\u4eec\u6765\u770b\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">private static class BeanMethodInterceptor implements MethodInterceptor, ConditionalCallback {\n    \n    @Override\n    @Nullable\n    public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object[] beanMethodArgs,\n                MethodProxy cglibMethodProxy) throws Throwable {\n        ConfigurableBeanFactory beanFactory = getBeanFactory(enhancedConfigInstance);\n        String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod);\n        \/\/ Determine whether this bean is a scoped-proxy\n        if (BeanAnnotationHelper.isScopedProxy(beanMethod)) {\n            String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName);\n            if (beanFactory.isCurrentlyInCreation(scopedBeanName)) {\n                beanName = scopedBeanName;\n            }\n        }\n        \/\/ To handle the case of an inter-bean method reference, we must explicitly check the\n        \/\/ container for already cached instances.\n        \/\/ First, check to see if the requested bean is a FactoryBean. If so, create a subclass\n        \/\/ proxy that intercepts calls to getObject() and returns any cached bean instance.\n        \/\/ This ensures that the semantics of calling a FactoryBean from within @Bean methods\n        \/\/ is the same as that of referring to a FactoryBean within XML. See SPR-6602.\n        if (factoryContainsBean(beanFactory, BeanFactory.FACTORY_BEAN_PREFIX + beanName) &amp;&amp;\n                factoryContainsBean(beanFactory, beanName)) {\n            Object factoryBean = beanFactory.getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);\n            if (factoryBean instanceof ScopedProxyFactoryBean) {\n                \/\/ Scoped proxy factory beans are a special case and should not be further proxied\n            }\n            else {\n                \/\/ It is a candidate FactoryBean - go ahead with enhancement\n                return enhanceFactoryBean(factoryBean, beanMethod.getReturnType(), beanFactory, beanName);\n            }\n        }\n        if (isCurrentlyInvokedFactoryMethod(beanMethod)) {\n            return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs);\n        }\n        return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName);\n    }\n}<\/code><\/pre>\n<p>\u81ea\u5df1\u5199\u8fc7 CGLIB \u52a8\u6001\u4ee3\u7406\u7684\u5c0f\u4f19\u4f34\u90fd\u77e5\u9053\u8fd9\u91cc intercept \u65b9\u6cd5\u7684\u542b\u4e49\uff0c\u8fd9\u5c31\u662f\u771f\u6b63\u7684\u62e6\u622a\u65b9\u6cd5\u4e86\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u7684\u914d\u7f6e\u7c7b\u662f Full \u6a21\u5f0f\u7684\u8bdd\uff0c\u90a3\u4e48\u5c06\u6765\u8c03\u7528 @Bean \u6ce8\u89e3\u6807\u8bb0\u7684\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u8c03\u7528\u7684\u5176\u5b9e\u662f\u8fd9\u91cc\u7684 intercept \u65b9\u6cd5\u3002<\/p>\n<p>\u4e0a\u9762\u65b9\u6cd5\uff0c\u9996\u5148\u4f1a\u5224\u65ad\u5f53\u524d\u4ee3\u7406\u662f\u5426\u4e3a\u4f5c\u7528\u57df\u4ee3\u7406\uff0c\u6211\u4eec\u8fd9\u91cc\u5f53\u7136\u4e0d\u662f\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u5224\u65ad\u8bf7\u6c42\u7684 Bean \u662f\u5426\u662f\u4e00\u4e2a FactoryBean\uff0c\u5982\u679c\u662f\uff0c\u5219\u9700\u8981\u53bb\u4ee3\u7406\u5176 getObject \u65b9\u6cd5\uff0c\u5f53\u6267\u884c\u5230 getObject \u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u5c31\u53bb Spring \u5bb9\u5668\u4e2d\u67e5\u627e\u9700\u8981\u7684 Bean\uff0c\u5f53\u7136\uff0c\u6211\u4eec\u8fd9\u91cc\u4e5f\u4e0d\u5c5e\u4e8e\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u5224\u65ad\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u662f\u5426\u4e3a\u5bb9\u5668\u4e2d\u6b63\u5728\u8c03\u7528\u7684\u5de5\u5382\u65b9\u6cd5\u3002<\/p>\n<p>\u4f8b\u5982\u6211\u6709\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">@Configuration\npublic class JavaConfig {\n\n    @Bean\n    User user() {\n        User user = new User();\n        user.setDog(dog());\n        return user;\n    }\n\n    @Bean\n    Dog dog() {\n        return new Dog();\n    }\n}<\/code><\/pre>\n<p>\u90a3\u4e48\u5982\u679c\u662f\u76f4\u63a5\u8c03\u7528 dog() \u65b9\u6cd5\uff0c\u5219 isCurrentlyInvokedFactoryMethod \u8fd4\u56de true\uff0c\u5982\u679c\u662f\u5728 user() \u65b9\u6cd5\u4e2d\u8c03\u7528\u7684 dog() \u65b9\u6cd5\uff0c\u5219 isCurrentlyInvokedFactoryMethod \u8fd4\u56de false\u3002<\/p>\n<p>\u5f53 isCurrentlyInvokedFactoryMethod \u8fd4\u56de true \u7684\u65f6\u5019\uff0c\u5c31\u6267\u884c invokeSuper \u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u771f\u6b63\u7684\u89e6\u53d1 dog() \u65b9\u6cd5\u7684\u6267\u884c\u3002<\/p>\n<p>\u5f53 isCurrentlyInvokedFactoryMethod \u8fd4\u56de false \u7684\u65f6\u5019\uff0c\u5219\u6267\u884c\u4e0b\u9762\u7684 resolveBeanReference \u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5148\u53bb Spring \u5bb9\u5668\u4e2d\u67e5\u627e\u76f8\u5e94\u7684 Bean\uff0c\u5982\u679c Spring \u5bb9\u5668\u4e2d\u4e0d\u5b58\u5728\u8be5 Bean\uff0c\u5219\u4f1a\u89e6\u53d1 Bean \u7684\u521b\u5efa\u6d41\u7a0b\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5c0f\u4f19\u4f34\u4eec\u5e94\u8be5\u660e\u767d\u4e86\u4e3a\u4ec0\u4e48 Full \u6a21\u5f0f\u4e0b\uff0c\u8c03\u7528 @Bean \u6ce8\u89e3\u6807\u8bb0\u7684\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u5bfc\u81f4 Bean \u7684\u91cd\u590d\u521b\u5efa\u4e86\u5427\uff5e<\/p>\n<p>\u597d\u5566\uff0c\u672c\u6587\u7ed3\u5408\u4e0a\u6587 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=nV42dmuVnBggCzzEMWDW3w%3D%3D.GBevAUKMDHjAVmyfr1czZqtz098A%2BnMP3uiOhAx1JvGwjzi%2BbctGFcy6Mw4SL7uQllhxk9aBDaA7MYvZtORGVg%3D%3D\" rel=\"noopener noreferrer\">@Configuration \u6ce8\u89e3\u7684 Full \u6a21\u5f0f\u548c Lite \u6a21\u5f0f\uff01<\/a> \u4e00\u8d77\u98df\u7528\u6548\u679c\u66f4\u4f73\u54e6\uff5e<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044160448\">https:\/\/segmentfault.com\/a\/1190000044160448<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e @Configuration \u6ce8\u89e3\u6709\u4e00\u4e2a\u7279\u522b\u7ecf\u5178\u7684\u9762\u8bd5\u9898\uff1a @Configuration \u548c @Comp [&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-10008188","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\/10008188","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=10008188"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008188\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}