這句話能夠做爲理解springboot自動注入的原理的鑰匙:ImportSelector接口的selectImports返回的數組(類的全類名)都會被歸入到spring容器中.java
至於spring怎麼根據類名建立bean的就不用管了。博文地址https://www.jianshu.com/p/464d04c36fb1spring
public class AutoConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware, ResourceLoaderAware, BeanFactoryAware, EnvironmentAware, Ordered //看這個類實現的接口多複雜
@Override public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return NO_IMPORTS; } AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader .loadMetadata(this.beanClassLoader); AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry( autoConfigurationMetadata, annotationMetadata); return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations()); }
這個方法就是導入了自動配置的文件的字符串。數組