@Import解析
1 // Process any @Import annotations getImports(sourceClass):获取所有@Import的bean
2 processImports(configClass, sourceClass, getImports(sourceClass), filter, true);
这里只是解析@Import和ImportSelector 、ImportBeanDefinitionRegistrar,到后面通过read方法才会注册到spring容器中
1 private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass, 2 Collection<SourceClass> importCandidates, Predicate<String> exclusionFilter, 3 boolean checkForCircularImports) { 4 5 if (importCandidates.isEmpty()) { 6 return; 7 } 8 9 if (checkForCircularImports && isChainedImportOnStack(configClass)) { 10 this.problemReporter.error(new CircularImportProblem(configClass, this.importStack)); 11 } 12 else { 13 this.importStack.push(configClass); 14 try { 15 for (SourceClass candidate : importCandidates) { 16 //如果是ImportSelector实现类 17 if (candidate.isAssignable(ImportSelector.class)) { 18 // Candidate class is an ImportSelector -> delegate to it to determine imports 19 Class<?> candidateClass = candidate.loadClass(); 20 ImportSelector selector = ParserStrategyUtils.instantiateClass(candidateClass, ImportSelector.class, 21 this.environment, this.resourceLoader, this.registry); 22 Predicate<String> selectorFilter = selector.getExclusionFilter(); 23 if (selectorFilter != null) { 24 exclusionFilter = exclusionFilter.or(selectorFilter); 25 } 26 //判断是DeferredImportSelector,因为DeferredImportSelector extends ImportSelector 27 if (selector instanceof DeferredImportSelector) { 28 //将deferredImportSelectors的实现类放到deferredImportSelectors成员变量中,后续会调用processDeferredImportSelectors()来处理 29 this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector); 30 } 31 else { 32 //递归处理,因为ImportSelector实现类也可能加上@Import注解 33 String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata()); 34 Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames, exclusionFilter); 35 processImports(configClass, currentSourceClass, importSourceClasses, exclusionFilter, false); 36 } 37 } 38 //如果是ImportBeanDefinitionRegistrar实现类 39 else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) { 40 // Candidate class is an ImportBeanDefinitionRegistrar -> 41 // delegate to it to register additional bean definitions 42 Class<?> candidateClass = candidate.loadClass(); 43 ImportBeanDefinitionRegistrar registrar = 44 ParserStrategyUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class, 45 this.environment, this.resourceLoader, this.registry); 46 //将实现ImportBeanDefinitionRegistrar接口的类放入importBeanDefinitionRegistrars变量中, 47 //后续当parse()执行完后会通过ConfigurationClassBeanDefinitionReader中的方法来处理importBeanDefinitionRegistrars变量 48 configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata()); 49 } 50 //普通的带有@Import注解的类 51 else { 52 // Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar -> 53 // process it as an @Configuration class 54 this.importStack.registerImport( 55 currentSourceClass.getMetadata(), candidate.getMetadata().getClassName()); 56 //递归处理,因为@Import(xxxx.java)中的xxxx.java上面也可能带有@Import 、@Component等注解 57 //candidate.asConfigClass(configClass):标记正在解析的candidate是通过configClass导入到spring容器中的 58 processConfigurationClass(candidate.asConfigClass(configClass), exclusionFilter); 59 } 60 } 61 } 62 catch (BeanDefinitionStoreException ex) { 63 throw ex; 64 } 65 catch (Throwable ex) { 66 throw new BeanDefinitionStoreException( 67 "Failed to process import candidates for configuration class [" + 68 configClass.getMetadata().getClassName() + "]", ex); 69 } 70 finally { 71 this.importStack.pop(); 72 } 73 } 74 }