ConfigurationClassParser doProcessConfigurationClass()解析

 1 protected final SourceClass doProcessConfigurationClass(
 2             ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)
 3             throws IOException {
 4         //处理内部类,一般用不到
 5         if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
 6             // Recursively process any member (nested) classes first
 7             processMemberClasses(configClass, sourceClass, filter);
 8         }
 9 
10         // Process any @PropertySource annotations
11         //处理@PropertySource注解
12         for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
13                 sourceClass.getMetadata(), PropertySources.class,
14                 org.springframework.context.annotation.PropertySource.class)) {
15             if (this.environment instanceof ConfigurableEnvironment) {
16                 processPropertySource(propertySource);
17             }
18             else {
19                 logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
20                         "]. Reason: Environment must implement ConfigurableEnvironment");
21             }
22         }
23 
24         // Process any @ComponentScan annotations
25         // 处理@ComponentScan注解
26         Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
27                 sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
28         if (!componentScans.isEmpty() &&
29                 !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
30             for (AnnotationAttributes componentScan : componentScans) {
31                 // The config class is annotated with @ComponentScan -> perform the scan immediately
32                 Set<BeanDefinitionHolder> scannedBeanDefinitions =
33                         this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
34                 // Check the set of scanned definitions for any further config classes and parse recursively if needed
35                 for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
36                     BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
37                     if (bdCand == null) {
38                         bdCand = holder.getBeanDefinition();
39                     }
40                     if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
41                         parse(bdCand.getBeanClassName(), holder.getBeanName());
42                     }
43                 }
44             }
45         }
46 
47         // Process any @Import annotations
48         processImports(configClass, sourceClass, getImports(sourceClass), filter, true);
49 
50         // Process any @ImportResource annotations
51         AnnotationAttributes importResource =
52                 AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
53         if (importResource != null) {
54             String[] resources = importResource.getStringArray("locations");
55             Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
56             for (String resource : resources) {
57                 String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
58                 configClass.addImportedResource(resolvedResource, readerClass);
59             }
60         }
61 
62         // Process individual @Bean methods 先把@Bean的方法存入configClass,后面this.reader.loadBeanDefinitions(configClasses)会继续处理@Bean,那时候才会真正实例化@Bean;
63         Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
64         for (MethodMetadata methodMetadata : beanMethods) {
65             configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
66         }
67 
68         // Process default methods on interfaces
69         processInterfaces(configClass, sourceClass);
70 
71         // Process superclass, if any
72         if (sourceClass.getMetadata().hasSuperClass()) {
73             String superclass = sourceClass.getMetadata().getSuperClassName();
74             if (superclass != null && !superclass.startsWith("java") &&
75                     !this.knownSuperclasses.containsKey(superclass)) {
76                 this.knownSuperclasses.put(superclass, configClass);
77                 // Superclass found, return its annotation metadata and recurse
78                 return sourceClass.getSuperClass();
79             }
80         }
81 
82         // No superclass -> processing is complete
83         return null;
84     }

 

posted @ 2021-05-10 17:39  龙之谷2019  阅读(296)  评论(0)    收藏  举报