从 @Import 注解到自动配置
@Import 注解用于把实例加入 Spring IOC 容器中。
打开 @SpringBootApplication 注解(Spring Boot 2.6.13 为例),会发现其被 @EnableAutoConfiguration 标注:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}
打开 @EnableAutoConfiguration 注解,发现其被 @Import 注解:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}
提供给 @Import 注解的参数为 AutoConfigurationImportSelector.class,它实现了 DeferredImportSelector 接口,DeferredImportSelector 接口继承自 ImportSelector 接口,其核心为 selectImports 方法,用来返回需要由容器管理的类的全类名。下面是 AutoConfigurationImportSelector 的方法实现:
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
其中的 getAutoConfigurationEntry 方法,返回找到的自动配置类。方法如下:
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
}
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
configurations = removeDuplicates(configurations);
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = getConfigurationClassFilter().filter(configurations);
fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationEntry(configurations, exclusions);
}
重点是 getCandidateConfigurations 方法,该方法会扫描到所有自动配置类。返回值经过去重、过滤等操作后,封装到 AutoConfigurationEntry 对象中返回。进入方法内部:
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
getSpringFactoriesLoaderFactoryClass 方法返回 EnableAutoConfiguration.class,后面用来找自动配置类:
protected Class<?> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
SpringFactoriesLoader.loadFactoryNames 方法会返回一个 List,这个 List 中包含的是所有自动配置类的全类名。进入方法:
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
ClassLoader classLoaderToUse = classLoader;
if (classLoaderToUse == null) {
classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();
}
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());
}
loadSpringFactories 方法会返回一个 Map,其 key 为全类名,value 为全类名对应的类名集合。进入方法:
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {
Map<String, List<String>> result = cache.get(classLoader);
if (result != null) {
return result;
}
result = new HashMap<>();
try {
Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
String factoryTypeName = ((String) entry.getKey()).trim();
String[] factoryImplementationNames =
StringUtils.commaDelimitedListToStringArray((String) entry.getValue());
for (String factoryImplementationName : factoryImplementationNames) {
result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())
.add(factoryImplementationName.trim());
}
}
}
// Replace all lists with unmodifiable lists containing unique elements
result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));
cache.put(classLoader, result);
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
return result;
}
重点关注Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION)
,其中FACTORIES_RESOURCE_LOCATION
的值为META-INF/spring.factories
:
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
打开spring-boot-autoconfigure-2.6.13.jar
的 META-INF/spring.factories 文件,可以看到包含如下内容:
# Auto Configuration Import Filters
org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\
org.springframework.boot.autoconfigure.condition.OnBeanCondition,\
org.springframework.boot.autoconfigure.condition.OnClassCondition,\
org.springframework.boot.autoconfigure.condition.OnWebApplicationCondition
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
...
loadSpringFactories 方法的作用就是,扫描 META-INF/spring.factories 文件,将文件中的配置信息加载为一个 Map,其中 key 为全类名,value 为全类名对应的类名集合。
最终 loadFactoryNames 中的loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList())
的返回值就是所有自动配置类的全类名。
注意 loadSpringFactories 方法会扫描到类路径下所有 jar 包中的 META-INF/spring.factories 文件,包括但不限于spring-boot-autoconfigure-2.6.13.jar
。
这样,Spring 容器就知道要加载哪些自动配置类了。
Spring Boot 2.7 之后,改为通过 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 来声明自动配置类。其 getCandidateConfigurations 方法如下(以 Spring Boot 2.7.18 为例):
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = new ArrayList<>(
SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));
ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);
Assert.notEmpty(configurations,
"No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you "
+ "are using a custom packaging, make sure that file is correct.");
return configurations;
}
可以看到它除了调用 SpringFactoriesLoader.loadFactoryNames 方法外,还调用了 ImportCandidates.load 方法,也就是说此时可以通过 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 和 META-INF/spring.factories 两种方法来声明自动配置类。