{"id":1893,"date":"2019-06-28T09:50:46","date_gmt":"2019-06-28T01:50:46","guid":{"rendered":"https:\/\/coderbee.net\/?p=1893"},"modified":"2019-06-28T09:50:46","modified_gmt":"2019-06-28T01:50:46","slug":"springboot-%e5%90%af%e5%8a%a8%e5%88%86%e6%9e%90%e5%9b%9b-%e6%b3%a8%e8%a7%a3%e9%a9%b1%e5%8a%a8%e7%9a%84-bean-%e5%ae%9a%e4%b9%89%e5%8a%a0%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20190628\/1893","title":{"rendered":"SpringBoot \u542f\u52a8\u5206\u6790(\u56db) &#8212; \u6ce8\u89e3\u9a71\u52a8\u7684 Bean \u5b9a\u4e49\u52a0\u8f7d"},"content":{"rendered":"<h1>1. \u4e00\u4e2a Spring \u52a0\u8f7d\u7c7b\u7684\u95ee\u9898<\/h1>\n<p>\u5148\u629b\u51fa\u4e2a\u95ee\u9898\uff1aSpringBoot \u5141\u8bb8\u901a\u8fc7\u6ce8\u89e3\u6839\u636e\u67d0\u4e2a\u7c7b\u662f\u5426\u5b58\u5728\u6765\u51b3\u5b9a\u914d\u7f6e\uff0c\u5982<\/p>\n<pre><code class=\"java\">@Bean\n@ConditionalOnClass(value = HikariDataSource.class)\npublic DataSource hikariDataSource() {\n    return new HikariDataSource();\n}\n\n@Bean\n@ConditionalOnClass(value = BasicDataSource.class)\npublic DataSource basicDataSource() {\n    return new BasicDataSource();\n}\n<\/code><\/pre>\n<p>\u6211\u4eec\u4e5f\u77e5\u9053 <code>ClassLoader<\/code> \u52a0\u8f7d\u4e00\u4e2a\u7c7b\u65f6\uff0c\u5982\u679c\u8fd9\u4e2a\u7c7b\u6216\u8fd9\u4e2a\u7c7b\u4f9d\u8d56\u7684\u7c7b\u627e\u4e0d\u5230\u5219\u4f1a\u629b\u51fa <code>ClassNotFoundException<\/code> \u3002<\/p>\n<p>Spring \u662f\u5982\u4f55\u5b9e\u73b0\u8fd9\u6837\u7684\u6761\u4ef6\u52a0\u8f7d\u800c\u4e0d\u4f1a\u629b\u51fa <code>ClassNotFoundException<\/code> \u5f02\u5e38\uff1f<\/p>\n<p><!--more--><\/p>\n<h1>2. Spring \u89e3\u6790\u914d\u7f6e\u7c7b\u7684\u8fc7\u7a0b<\/h1>\n<p>\u5728<a href=\"https:\/\/coderbee.net\/index.php\/framework\/20190610\/1865\">\u7b2c\u4e00\u7bc7<\/a>\u5df2\u7ecf\u4ecb\u7ecd\u8fc7 <code>ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry<\/code> \u4f1a\u6ce8\u518c\u6240\u6709\u7684 Bean \u5b9a\u4e49\u3002\u73b0\u5728\u6765\u4ed4\u7ec6\u770b\u770b\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<pre><code class=\"java\">public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {\n    int registryId = System.identityHashCode(registry);\n    if (this.registriesPostProcessed.contains(registryId)) {\n        throw new IllegalStateException(\n                \"postProcessBeanDefinitionRegistry already called on this post-processor against \" + registry);\n    }\n    if (this.factoriesPostProcessed.contains(registryId)) {\n        throw new IllegalStateException(\n                \"postProcessBeanFactory already called on this post-processor against \" + registry);\n    }\n    this.registriesPostProcessed.add(registryId);\n\n    processConfigBeanDefinitions(registry);\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4e3b\u8981\u662f\u505a\u4e86\u9632\u6b62\u91cd\u590d\u6ce8\u518c\u7684\u5904\u7406\uff0c\u5177\u4f53\u7684\u6ce8\u518c\u903b\u8f91\u5728 <code>processConfigBeanDefinitions(registry)<\/code> \u3002<\/p>\n<pre><code class=\"java\">public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {\n    List&lt;BeanDefinitionHolder&gt; configCandidates = new ArrayList&lt;BeanDefinitionHolder&gt;();\n    String[] candidateNames = registry.getBeanDefinitionNames();\n\n    \/\/ \u9996\u5148\u4ece registry \u627e\u51fa\u5df2\u7ecf\u6ce8\u518c\u7684\u672a\u5904\u7406\u7684\u914d\u7f6e\u7c7b\u7684\u5b9a\u4e49\u4f5c\u4e3a\u914d\u7f6e\u7c7b\u5019\u9009\n    for (String beanName : candidateNames) {\n        BeanDefinition beanDef = registry.getBeanDefinition(beanName);\n        if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) ||\n                ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) {\n            if (logger.isDebugEnabled()) {\n                logger.debug(\"Bean definition has already been processed as a configuration class: \" + beanDef);\n            }\n        }\n        else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {\n            configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));\n        }\n    }\n\n    \/\/ Return immediately if no @Configuration classes were found\n    if (configCandidates.isEmpty()) {\n        return;\n    }\n\n    \/\/ \u5bf9\u914d\u7f6e\u7c7b\u5019\u9009\u8fdb\u884c\u6392\u5e8f\uff0c\u4ee5\u6ee1\u8db3\u4f9d\u8d56\u914d\u7f6e\u4f9d\u8d56\u987a\u5e8f\n    \/\/ Sort by previously determined @Order value, if applicable\n    Collections.sort(configCandidates, new Comparator&lt;BeanDefinitionHolder&gt;() {\n        @Override\n        public int compare(BeanDefinitionHolder bd1, BeanDefinitionHolder bd2) {\n            int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());\n            int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());\n            return (i1 &lt; i2) ? -1 : (i1 &gt; i2) ? 1 : 0;\n        }\n    });\n\n    \/\/ \u627e\u51fa beanName \u7684\u751f\u6210\u5668\n    \/\/ Detect any custom bean name generation strategy supplied through the enclosing application context\n    SingletonBeanRegistry sbr = null;\n    if (registry instanceof SingletonBeanRegistry) {\n        sbr = (SingletonBeanRegistry) registry;\n        if (!this.localBeanNameGeneratorSet &amp;&amp; sbr.containsSingleton(CONFIGURATION_BEAN_NAME_GENERATOR)) {\n            BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);\n            this.componentScanBeanNameGenerator = generator;\n            this.importBeanNameGenerator = generator;\n        }\n    }\n\n    \/\/ Parse each @Configuration class\n    \/\/ \u521b\u5efa ConfigurationClassParser \u6765\u89e3\u6790\u5019\u9009\u914d\u7f6e\u7c7b\u5b9a\u4e49\n    ConfigurationClassParser parser = new ConfigurationClassParser(\n            this.metadataReaderFactory, this.problemReporter, this.environment,\n            this.resourceLoader, this.componentScanBeanNameGenerator, registry);\n\n    \/\/ \u5f53\u524d\u5f85\u89e3\u6790\u7684\u5019\u9009\u914d\u7f6e\u7c7b\u5b9a\u4e49\n    Set&lt;BeanDefinitionHolder&gt; candidates = new LinkedHashSet&lt;BeanDefinitionHolder&gt;(configCandidates);\n\n    \/\/ \u5f53\u524d\u5df2\u89e3\u6790\u7684\u914d\u7f6e\u7c7b\n    Set&lt;ConfigurationClass&gt; alreadyParsed = new HashSet&lt;ConfigurationClass&gt;(configCandidates.size());\n    do {\n        \/\/ \u89e3\u6790\u5f53\u524d\u5019\u9009\u96c6\u91cc\u7684\u6240\u6709\u914d\u7f6e\u7c7b\u5b9a\u4e49\n        parser.parse(candidates);\n\n        \/\/ \u9a8c\u8bc1\u89e3\u6790\u7684\u7ed3\u679c\n        parser.validate();\n\n        \/\/ \u4ece\u89e3\u6790\u83b7\u53d6\u89e3\u6790\u5f97\u5230\u7684\u6240\u6709\u914d\u7f6e\u7c7b\n        Set&lt;ConfigurationClass&gt; configClasses = new LinkedHashSet&lt;ConfigurationClass&gt;(parser.getConfigurationClasses());\n\n        \/\/ \u79fb\u9664\u5df2\u7ecf\u5904\u7406\u8fc7\u7684\u914d\u7f6e\u7c7b\uff0c\u56e0\u4e3a\u4e0b\u9762\u8981\u8fdb\u884c\u6ce8\u518c\uff0c\u9632\u6b62\u91cd\u590d\u6ce8\u518c\n        configClasses.removeAll(alreadyParsed);\n\n        \/\/ \u521b\u5efa ConfigurationClassBeanDefinitionReader\uff0c\u7528\u4e8e\u6ce8\u518c\u914d\u7f6e\u7c7b\n        \/\/ reader \u7684\u521b\u5efa\u5e94\u8be5\u662f\u53ef\u4ee5\u653e\u5230\u5faa\u73af\u5916\u7684\n        \/\/ Read the model and create bean definitions based on its content\n        if (this.reader == null) {\n            this.reader = new ConfigurationClassBeanDefinitionReader(\n                    registry, this.sourceExtractor, this.resourceLoader, this.environment,\n                    this.importBeanNameGenerator, parser.getImportRegistry());\n        }\n\n        \/\/ \u6ce8\u518c\u914d\u7f6e\u7c7b\n        this.reader.loadBeanDefinitions(configClasses);\n\n        \/\/ \u628a\u672c\u8f6e\u5904\u7406\u5b8c\u7684\u914d\u7f6e\u7c7b\u52a0\u5230\u5df2\u5904\u7406\u96c6\u5408\n        alreadyParsed.addAll(configClasses);\n\n        \/\/ \u6e05\u9664\u5df2\u5904\u7406\u7684\u5019\u9009\u96c6\uff0c\u590d\u7528\u8fd9\u4e2a\u96c6\u5408\n        candidates.clear();\n\n        \/\/ \u4ece registry \u627e\u51fa\u65b0\u52a0\u5165\u3001\u672a\u5904\u7406\u8fc7\u7684\u914d\u7f6e\u7c7b\u5b9a\u4e49\uff0c\u52a0\u5165\u5f53\u524d\u5019\u9009\u96c6\n        if (registry.getBeanDefinitionCount() &gt; candidateNames.length) {\n            String[] newCandidateNames = registry.getBeanDefinitionNames();\n            Set&lt;String&gt; oldCandidateNames = new HashSet&lt;String&gt;(Arrays.asList(candidateNames));\n            Set&lt;String&gt; alreadyParsedClasses = new HashSet&lt;String&gt;();\n            for (ConfigurationClass configurationClass : alreadyParsed) {\n                alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());\n            }\n            for (String candidateName : newCandidateNames) {\n                if (!oldCandidateNames.contains(candidateName)) { \/\/ \u8fc7\u6ee4\u51fa\u65b0\u52a0\u5165\u7684\n                    BeanDefinition bd = registry.getBeanDefinition(candidateName);\n                    if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &amp;&amp;\n                            !alreadyParsedClasses.contains(bd.getBeanClassName())) { \/\/ \u627e\u51fa\u672a\u5904\u7406\u8fc7\u7684\n                        candidates.add(new BeanDefinitionHolder(bd, candidateName));\n                    }\n                }\n            }\n            candidateNames = newCandidateNames;\n        }\n    } while (!candidates.isEmpty()); \/\/ \u5982\u679c\u5f53\u524d\u5019\u9009\u96c6\u4e0d\u4e3a\u7a7a\u5219\u7ee7\u7eed\u4e0b\u4e00\u8f6e\u5904\u7406\n\n    \/\/ \u81f3\u6b64\uff0c\u901a\u8fc7 Java \u7c7b\u914d\u7f6e\u7684 bean \u5b9a\u4e49\u90fd\u52a0\u8f7d\u5e76\u6ce8\u518c\u5230\u5bb9\u5668\n\n    \/\/ Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes\n    if (sbr != null) {\n        if (!sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {\n            sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());\n        }\n    }\n\n    if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {\n        ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u65b9\u6cd5\u5e76\u672a\u6d89\u53ca\u67d0\u4e2a\u914d\u7f6e\u7c7b\u5b9a\u4e49\u5982\u4f55\u89e3\u6790\u5f97\u5230\u914d\u7f6e\u7c7b\u3001\u6ce8\u518c\u914d\u7f6e\u7c7b\u5b9a\u4e49\u7684\u7ec6\u8282\u8fd9\u4e00\u5c42\u7684\u903b\u8f91\u5982\u4e0b\uff1a<br \/>\n1. \u4ece <code>registry<\/code> \u6536\u96c6\u914d\u7f6e\u7c7b\u5b9a\u4e49\u653e\u5230 <code>Set&lt;BeanDefinitionHolder&gt; candidates<\/code> \u4f5c\u4e3a\u5904\u7406\u7684\u5f00\u59cb\u6e90\uff1b<br \/>\n2. \u521b\u5efa <code>ConfigurationClassParser parser<\/code> \u7528\u4e8e\u89e3\u6790\u65b0\u7684\u914d\u7f6e\u7c7b\u5b9a\u4e49\u548c <code>ConfigurationClassBeanDefinitionReader reader<\/code> \u7528\u4e8e\u6ce8\u518c\u65b0\u7684\u914d\u7f6e\u7c7b\u5b9a\u4e49\uff1b<br \/>\n3. \u521d\u59cb\u5316\u5df2\u89e3\u6790\u96c6\u5408 <code>alreadyParsed<\/code>\uff1b<br \/>\n4. \u7528 <code>parser<\/code> \u89e3\u6790\u5019\u9009\u96c6 <code>candidates<\/code>\uff0c\u8fdb\u884c\u9a8c\u8bc1\uff1b<br \/>\n5. \u5f97\u5230\u89e3\u6790\u7ed3\u679c <code>Set&lt;ConfigurationClass&gt; configClasses<\/code>\uff0c\u79fb\u9664\u5df2\u7ecf\u5904\u7406\u8fc7\u7684 <code>Set&lt;ConfigurationClass&gt; alreadyParsed<\/code> \uff1b<br \/>\n6. \u7528 <code>reader<\/code> \u628a\u6536\u96c6\u5230\u7684\u914d\u7f6e\u7c7b\u96c6\u5408 <code>configClasses<\/code> \u6ce8\u518c\u5230 <code>registry<\/code>\uff1b<br \/>\n7. \u628a <code>configClasses<\/code> \u52a0\u5165\u5df2\u89e3\u6790\u96c6\u5408 <code>alreadyParsed<\/code>;<br \/>\n8. \u6e05\u7a7a\u5019\u9009\u96c6 <code>candidates<\/code>\uff0c\u91cd\u65b0\u6536\u96c6\u5019\u9009\u96c6 <code>candidates<\/code>\uff1b<br \/>\n9. \u5982\u679c\u6536\u96c6\u5230\u7684\u5019\u9009\u96c6 <code>candidates<\/code> \u4e0d\u4e3a\u7a7a\uff0c\u7ee7\u7eed\u8dcc\u4ee3\u6b65\u9aa4 4 \u3002<\/p>\n<p>\u73b0\u5728\u6765\u770b\u5982\u4f55\u89e3\u6790\u5019\u9009\u96c6\uff0c<code>ConfigurationClassParser.parse(Set&lt;BeanDefinitionHolder&gt; configCandidates)<\/code> \uff1a<\/p>\n<pre><code class=\"java\">public void parse(Set&lt;BeanDefinitionHolder&gt; configCandidates) {\n    this.deferredImportSelectors = new LinkedList&lt;DeferredImportSelectorHolder&gt;();\n\n    for (BeanDefinitionHolder holder : configCandidates) {\n        BeanDefinition bd = holder.getBeanDefinition();\n        try {\n            if (bd instanceof AnnotatedBeanDefinition) {\/\/ \u57fa\u4e8e Java \u7c7b\u914d\u7f6e\u7684\u90fd\u662f\u8d70\u8fd9\u91cc\n                parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());\n            }\n            else if (bd instanceof AbstractBeanDefinition &amp;&amp; ((AbstractBeanDefinition) bd).hasBeanClass()) {\n                parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());\n            }\n            else {\n                parse(bd.getBeanClassName(), holder.getBeanName());\n            }\n        }\n        catch (BeanDefinitionStoreException ex) {\n            throw ex;\n        }\n        catch (Throwable ex) {\n            throw new BeanDefinitionStoreException(\n                    \"Failed to parse configuration class [\" + bd.getBeanClassName() + \"]\", ex);\n        }\n    }\n\n    processDeferredImportSelectors();\n}\n\n\nprotected void processConfigurationClass(ConfigurationClass configClass) throws IOException {\n    \/\/ ....\u5220\u6389\u4e86\u9632\u6b62\u91cd\u590d\u5904\u7406\u7684\u903b\u8f91\n\n    \/\/ SourceClass \u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5305\u88c5\uff0c\u5141\u8bb8\u88ab\u6ce8\u89e3\u7684\u6e90\u7c7b\u4ee5\u7edf\u4e00\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\uff0c\u800c\u4e0d\u7ba1\u5b83\u4eec\u662f\u5982\u4f55\u52a0\u8f7d\u7684\n\n    \/\/ \u9012\u5f52\u5904\u7406\u914d\u7f6e\u7c7b\u548c\u5b83\u7684\u8d85\u7c7b\n    SourceClass sourceClass = asSourceClass(configClass);\n    do {\n        \/\/ \u5904\u7406\u7ed9\u5b9a\u7684\u914d\u7f6e\u7c7b\uff0c\u5982\u679c\u5b83\u6709\u975e Object \u7684\u7236\u7c7b\uff0c\u5219\u8fd4\u56de\u5176\u7236\u7c7b\uff0c\u9012\u5f52\u5904\u7406\n        sourceClass = doProcessConfigurationClass(configClass, sourceClass);\n    }\n    while (sourceClass != null);\n\n    this.configurationClasses.put(configClass, configClass);\n}\n\nprotected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {\n\n    \/\/ \u9996\u5148\u9012\u5f52\u5904\u7406\u5185\u90e8\u5d4c\u5957\u7c7b\n    processMemberClasses(configClass, sourceClass);\n\n    \/\/ \u5904\u7406 @PropertySource \u6ce8\u89e3\uff0c\u628a\u8d44\u6e90\u6587\u4ef6\u52a0\u8f7d\u5230 environment \u91cc\n    for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(\n            sourceClass.getMetadata(), PropertySources.class,\n            org.springframework.context.annotation.PropertySource.class)) {\n        if (this.environment instanceof ConfigurableEnvironment) {\n            processPropertySource(propertySource);\n        } else {\n            logger.warn(\"Ignoring @PropertySource annotation on [\" + sourceClass.getMetadata().getClassName() +\n                    \"]. Reason: Environment must implement ConfigurableEnvironment\");\n        }\n    }\n\n    \/\/ \u5904\u7406 @ComponentScan \u6ce8\u89e3\uff0c\u5982\u679c\u6709\uff0c\u7acb\u5373\u6267\u884c\u626b\u63cf\n    Set&lt;AnnotationAttributes&gt; componentScans = AnnotationConfigUtils.attributesForRepeatable(\n            sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);\n    if (!componentScans.isEmpty() &amp;&amp;\n            !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {\n        for (AnnotationAttributes componentScan : componentScans) {\n            \/\/ \u5982\u679c\u914d\u7f6e\u7c7b\u6709 @ComponentScan \u6ce8\u89e3\uff0c\u7acb\u5373\u6267\u884c\u626b\u63cf\n            Set&lt;BeanDefinitionHolder&gt; scannedBeanDefinitions =\n                    this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());\n            \/\/ \u68c0\u67e5\u626b\u63cf\u5230\u7684\u5b9a\u4e49\u91cc\u662f\u5426\u6709\u66f4\u591a\u7684\u914d\u7f6e\u7c7b\uff0c\u6709\u5219\u9012\u5f52 parse\n            for (BeanDefinitionHolder holder : scannedBeanDefinitions) {\n                BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();\n                if (bdCand == null) {\n                    bdCand = holder.getBeanDefinition();\n                }\n                if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {\n                    parse(bdCand.getBeanClassName(), holder.getBeanName());\n                }\n            }\n        }\n    }\n\n    \/\/ \u5904\u7406 @Import \u6ce8\u89e3\n    processImports(configClass, sourceClass, getImports(sourceClass), true);\n\n    \/\/ \u5904\u7406 @ImportResource \u6ce8\u89e3\n    if (sourceClass.getMetadata().isAnnotated(ImportResource.class.getName())) {\n        AnnotationAttributes importResource =\n                AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);\n        String[] resources = importResource.getStringArray(\"locations\");\n        Class&lt;? extends BeanDefinitionReader&gt; readerClass = importResource.getClass(\"reader\");\n        for (String resource : resources) {\n            String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);\n            configClass.addImportedResource(resolvedResource, readerClass);\n        }\n    }\n\n    \/\/ \u5904\u7406 @Bean \u6ce8\u89e3\u7684\u65b9\u6cd5\n    Set&lt;MethodMetadata&gt; beanMethods = retrieveBeanMethodMetadata(sourceClass);\n    for (MethodMetadata methodMetadata : beanMethods) {\n        configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));\n    }\n\n    \/\/ \u5904\u7406\u63a5\u53e3\u7684\u9ed8\u8ba4\u65b9\u6cd5\n    processInterfaces(configClass, sourceClass);\n\n    \/\/ \u5982\u679c\u6709\uff0c\u5904\u7406\u7236\u7c7b\n    if (sourceClass.getMetadata().hasSuperClass()) {\n        String superclass = sourceClass.getMetadata().getSuperClassName();\n        if (!superclass.startsWith(\"java\") &amp;&amp; !this.knownSuperclasses.containsKey(superclass)) {\n            this.knownSuperclasses.put(superclass, configClass);\n            \/\/ Superclass found, return its annotation metadata and recurse\n            return sourceClass.getSuperClass();\n        }\n    }\n\n    \/\/ \u6ca1\u6709\u7236\u7c7b\uff0c\u5904\u7406\u5b8c\u6210\n    return null;\n}\n<\/code><\/pre>\n<p>\u89e3\u6790\u4e00\u4e2a\u914d\u7f6e\u7c7b\u7684\u5927\u81f4\u8fc7\u7a0b\uff1a<\/p>\n<ol>\n<li>\u9012\u5f52\u5904\u7406\u6240\u6709\u5d4c\u5957\u7c7b\uff1b<\/li>\n<li>\u5904\u7406\u6e90\u7c7b\u4e0a <code>@PropertySource<\/code> \u6ce8\u89e3\uff1b<\/li>\n<li>\u5904\u7406 <code>@ComponentScan<\/code> \u6ce8\u89e3\uff1b<\/li>\n<li>\u5904\u7406 <code>@Import<\/code> \u6ce8\u89e3\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u7684 <code>ImportSelector\/ImportBeanDefinitionRegistrar<\/code> \u63a5\u53e3\u5b9e\u73b0\u6765\u52a0\u8f7dbean\u5b9a\u4e49\uff1b<\/li>\n<li>\u5904\u7406 <code>@ImportResource<\/code> \u6ce8\u89e3\uff1b<\/li>\n<li>\u5904\u7406\u6240\u6709\u6709 <code>@Bean<\/code> \u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u65b9\u6cd5\u7684\u4fe1\u606f\u5c01\u88c5\u5728 <code>MethodMetadata<\/code>\uff0c\u4e0e\u6240\u5728\u7684\u914d\u7f6e\u7c7b\u4e00\u8d77\u5c01\u88c5\u6210 <code>BeanMethod<\/code> \u6dfb\u52a0\u5230\u914d\u7f6e\u7c7b\u7684 <code>beanMethods<\/code> \u96c6\u5408\u91cc\uff1b<\/li>\n<li>\u9012\u5f52\u5904\u7406\u6240\u5b9e\u73b0\u7684 interfaces \u4e0a\u7684\u9ed8\u8ba4\u65b9\u6cd5\uff1b<\/li>\n<li>\u5982\u679c\u975e Object\u3001\u975e java \u81ea\u5e26\u7c7b\u7684\u7236\u7c7b\uff0c\u5219\u8fd4\u56de\u8fdb\u884c\u9012\u5f52\u5904\u7406\uff1b<\/li>\n<\/ol>\n<p>\u4e0a\u9762\u7684\u89e3\u6790\u8fc7\u7a0b\u4e2d\u6709\u4e2a\u5f88\u91cd\u8981\u7684\u89d2\u8272\u662f <code>SourceClass<\/code>\uff0c\u8868\u793a SpringBoot \u81ea\u5df1\u89e3\u6790\u5230\u7684\u6e90\u6587\u4ef6\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"java\">\/\/ SourceClass \u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5305\u88c5\uff0c\u5141\u8bb8\u88ab\u6ce8\u89e3\u7684\u6e90\u7c7b\u4ee5\u7edf\u4e00\u7684\u65b9\u5f0f\u8fdb\u884c\u5904\u7406\uff0c\u800c\u4e0d\u7ba1\u5b83\u4eec\u662f\u5982\u4f55\u52a0\u8f7d\u7684\n\n\/\/ \u9012\u5f52\u5904\u7406\u914d\u7f6e\u7c7b\u548c\u5b83\u7684\u8d85\u7c7b\nSourceClass sourceClass = asSourceClass(configClass);\ndo {\n    \/\/ \u5904\u7406\u7ed9\u5b9a\u7684\u914d\u7f6e\u7c7b\uff0c\u5982\u679c\u5b83\u6709\u975e Object \u7684\u7236\u7c7b\uff0c\u5219\u8fd4\u56de\u5176\u7236\u7c7b\uff0c\u9012\u5f52\u5904\u7406\n    sourceClass = doProcessConfigurationClass(configClass, sourceClass);\n}\nwhile (sourceClass != null);\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u7684\u9012\u5f52\u5904\u7406\u8fc7\u7a0b\u4e2d\uff0csourceClass \u4e00\u5f00\u59cb\u662f\u914d\u7f6e\u7c7b\u672c\u8eab\u5bf9\u5e94\u7684\u6e90\u7c7b\u7684\u4fe1\u606f\uff0c\u540e\u7eed\u662f\u5176\u7236\u7c7b\u7684\u6e90\u7c7b\u4fe1\u606f\u3002<\/p>\n<h2>SourceClass<\/h2>\n<p>SourceClass \u4e0e Class \u5565\u533a\u522b\uff1f\u4e3a\u4ec0\u4e48\u9700\u8981\u5b83\uff1f<\/p>\n<p>\u4ece\u4e0a\u9762\u4ee3\u7801\u7684 <code>SourceClass sourceClass = asSourceClass(configClass);<\/code> \u5f00\u59cb\u770b\uff1a<\/p>\n<pre><code class=\"java\">SourceClass asSourceClass(String className) throws IOException {\n    if (className.startsWith(\"java\")) {\n        \/\/ \u5bf9\u4e8e\u6838\u5fc3Java\u7c7b\u4e0d\u4f7f\u7528ASM\uff0c\u56e0\u4e3a\u80af\u5b9a\u5b58\u5728\u7684\uff0c\u76f4\u63a5\u52a0\u8f7d\n        try {\n            return new SourceClass(this.resourceLoader.getClassLoader().loadClass(className));\n        } catch (ClassNotFoundException ex) {\n            throw new NestedIOException(\"Failed to load class [\" + className + \"]\", ex);\n        }\n    }\n    return new SourceClass(this.metadataReaderFactory.getMetadataReader(className));\n}\n<\/code><\/pre>\n<p>metadataReaderFactory \u7528\u5b8c\u6574\u7c7b\u540d\u6784\u9020\u4e00\u4e2a Resource\uff0c\u7136\u540e\u8fd9\u4e2a Resource \u548c ClassLoader \u6765\u6784\u9020\u4e00\u4e2a SimpleMetadataReader\u3002SimpleMetadataReader \u901a\u8fc7 ClassReader \u6765\u8bfb\u53d6\u8fd9\u4e2a\u7c7b\u7684\u5b57\u8282\u7801\u6587\u4ef6\uff0c\u518d\u901a\u8fc7 ASM \u5de5\u5177 AnnotationMetadataReadingVisitor \u8fdb\u884c\u89e3\u6790\uff0c\u5f97\u5230\u5173\u4e8e\u8fd9\u4e2a\u7c7b\u7684\u5143\u6570\u636e\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u7ed5\u8fc7\u4e86JDK \u7684\u7c7b\u52a0\u8f7d\u673a\u5236\uff0c\u4e5f\u5c31\u907f\u514d\u4e86\u4e00\u5f00\u59cb\u90a3\u4e2a\u53ef\u80fd\u51fa\u73b0\u7c7b\u627e\u4e0d\u5230\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u7ee7\u7eed\u770b\u4e0b SimpleMetadataReader\uff0c<code>MetadataReader<\/code> \u7684\u552f\u4e00\u5b9e\u73b0\u7c7b\uff1a<\/p>\n<pre><code class=\"java\">final class SimpleMetadataReader implements MetadataReader {\n    private final Resource resource;\n    private final ClassMetadata classMetadata;\n    private final AnnotationMetadata annotationMetadata;\n\n    SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {\n        InputStream is = new BufferedInputStream(resource.getInputStream());\n        ClassReader classReader;\n        try {\n            classReader = new ClassReader(is);\n        } catch (IllegalArgumentException ex) {\n            throw new NestedIOException(\"ASM ClassReader failed to parse class file - \" +\n                    \"probably due to a new Java class file version that isn't supported yet: \" + resource, ex);\n        } finally {\n            is.close();\n        }\n\n        AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);\n        classReader.accept(visitor, ClassReader.SKIP_DEBUG);\n\n        this.annotationMetadata = visitor;\n        \/\/ (since AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor)\n        this.classMetadata = visitor;\n        this.resource = resource;\n    }\n    \/\/ \u7701\u7565\u5176\u4ed6\u4ee3\u7801\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e00\u4e2a Spring \u52a0\u8f7d\u7c7b\u7684\u95ee\u9898 \u5148\u629b\u51fa\u4e2a\u95ee\u9898\uff1aSpringBoot \u5141\u8bb8 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20190628\/1893\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[308],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1893"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1893"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1893\/revisions"}],"predecessor-version":[{"id":1895,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1893\/revisions\/1895"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}