在SpringBoot3中spring.factories配置的自动装配不生效
失效原因
从 Spring Boot 2.6 升级到Spring Boot 2.7后,自动配置注册有更改。 Spring Boot 2.7发行说明
废弃说明:
如果您创建了自己的自动配置,则应将注册从密钥spring.factories下移至org.springframework.boot.autoconfigure.AutoConfiguration.imports。每行包含自动配置类的完全限定名称,而不是单个逗号分隔的列表。有关示例,请参阅包含的自动配置。
为了向后兼容,spring.factories仍将保留中的条目。
- 在Spring Boot 2.7还是可以兼容使用spring.factories
- 到了SpringBoot3 spring.factories就不兼容使用了。
- tips: 在SpringBoot3中spring.factories使用org.springframework.boot.env.EnvironmentPostProcessor动态加载配置文件配置文件却还生效的。
解决办法
使用spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
代替spring.factories
中的org.springframework.boot.autoconfigure.EnableAutoConfiguration
例子
- 原spring.factories文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.Test1AutoConfiguration,\
org.springframework.boot.autoconfigure.Test2AutoConfiguration,\
- 在resource/META-INF目录下新建spring目录,并添加org.springframework.boot.autoconfigure.AutoConfiguration.imports文件
org.springframework.boot.autoconfigure.Test1AutoConfiguration
org.springframework.boot.autoconfigure.Test2AutoConfiguration
springboot2.x 和springboot3 Autoconfiguration 对比图
springboot3 读取配置文件源码
private static final String LOCATION = "META-INF/spring/%s.imports";
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader())
.getCandidates();
Assert.notEmpty(configurations,
"No auto configuration classes found 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;
}
public static ImportCandidates load(Class<?> annotation, ClassLoader classLoader) {
Assert.notNull(annotation, "'annotation' must not be null");
ClassLoader classLoaderToUse = decideClassloader(classLoader);
String location = String.format(LOCATION, annotation.getName());
Enumeration<URL> urls = findUrlsInClasspath(classLoaderToUse, location);
List<String> importCandidates = new ArrayList<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
importCandidates.addAll(readCandidateConfigurations(url));
}
return new ImportCandidates(importCandidates);
}
springboot2 读取配置文件源码
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
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;
}
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
String factoryTypeName = factoryType.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}