Skip to content

Commit d7b229d

Browse files
committed
Rename AutoConfigurationLoader to ImportCandidates
Move the class to a more suitable package, and load the files from META-INF/spring/<fqn>.imports See gh-29872
1 parent 14c9147 commit d7b229d

File tree

48 files changed

+84
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+84
-68
lines changed

buildSrc/src/main/java/org/springframework/boot/build/autoconfigure/AutoConfigurationMetadata.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public AutoConfigurationMetadata() {
6666
.withPathSensitivity(PathSensitivity.RELATIVE).withPropertyName("spring.factories");
6767
getInputs()
6868
.file((Callable<File>) () -> new File(this.sourceSet.getOutput().getResourcesDir(),
69-
"META-INF/spring-boot/org.springframework.boot.autoconfigure.AutoConfiguration"))
69+
"META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports"))
7070
.withPathSensitivity(PathSensitivity.RELATIVE)
7171
.withPropertyName("org.springframework.boot.autoconfigure.AutoConfiguration");
7272

@@ -137,17 +137,17 @@ private Set<String> readSpringFactories() throws IOException {
137137

138138
/**
139139
* Reads auto-configurations from
140-
* META-INF/spring-boot/org.springframework.boot.autoconfigure.AutoConfiguration.
140+
* META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.
141141
* @return auto-configurations
142142
*/
143143
private List<String> readAutoConfigurationsFile() throws IOException {
144144
File file = new File(this.sourceSet.getOutput().getResourcesDir(),
145-
"META-INF/spring-boot/org.springframework.boot.autoconfigure.AutoConfiguration");
145+
"META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports");
146146
if (!file.exists()) {
147147
return Collections.emptyList();
148148
}
149149
// Nearly identical copy of
150-
// org.springframework.boot.autoconfigure.AutoConfigurationLoader.readAutoConfigurations
150+
// org.springframework.boot.context.annotation.ImportCandidates.load
151151
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
152152
List<String> autoConfigurations = new ArrayList<>();
153153
String line;

buildSrc/src/main/java/org/springframework/boot/build/test/autoconfigure/TestSliceMetadata.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private Properties readTestSlices() throws IOException {
106106
Properties springFactories = readSpringFactories(
107107
new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring.factories"));
108108
readTestSlicesDirectory(springFactories,
109-
new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring-boot/"));
109+
new File(this.sourceSet.getOutput().getResourcesDir(), "META-INF/spring/"));
110110
for (File classesDir : this.sourceSet.getOutput().getClassesDirs()) {
111111
addTestSlices(testSlices, classesDir, metadataReaderFactory, springFactories);
112112
}
@@ -123,14 +123,17 @@ private Properties readTestSlices() throws IOException {
123123
* @param directory directory to scan
124124
*/
125125
private void readTestSlicesDirectory(Properties springFactories, File directory) {
126-
File[] files = directory.listFiles();
126+
File[] files = directory.listFiles((dir, name) -> name.endsWith(".imports"));
127127
if (files == null) {
128128
return;
129129
}
130130
for (File file : files) {
131131
try {
132132
List<String> lines = removeComments(Files.readAllLines(file.toPath()));
133-
springFactories.setProperty(file.getName(), StringUtils.collectionToCommaDelimitedString(lines));
133+
String fileNameWithoutExtension = file.getName().substring(0,
134+
file.getName().length() - ".imports".length());
135+
springFactories.setProperty(fileNameWithoutExtension,
136+
StringUtils.collectionToCommaDelimitedString(lines));
134137
}
135138
catch (IOException ex) {
136139
throw new UncheckedIOException("Failed to read file " + file, ex);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.boot.context.annotation.ImportCandidates;
2728
import org.springframework.context.annotation.Conditional;
2829
import org.springframework.context.annotation.Configuration;
2930
import org.springframework.core.io.support.SpringFactoriesLoader;
@@ -34,9 +35,8 @@
3435
* {@link Configuration @Configuration} with the exception that
3536
* {@literal Configuration#proxyBeanMethods() proxyBeanMethods} is always {@code false}.
3637
* <p>
37-
* They are located using the {@link AutoConfigurationLoader} and the
38-
* {@link SpringFactoriesLoader} mechanism (keyed against
39-
* {@link EnableAutoConfiguration}).
38+
* They are located using {@link ImportCandidates} and the {@link SpringFactoriesLoader}
39+
* mechanism (keyed against {@link EnableAutoConfiguration}).
4040
* <p>
4141
* Generally auto-configuration classes are marked as {@link Conditional @Conditional}
4242
* (most often using {@link ConditionalOnClass @ConditionalOnClass} and

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationExcludeFilter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.springframework.beans.factory.BeanClassLoaderAware;
24+
import org.springframework.boot.context.annotation.ImportCandidates;
2425
import org.springframework.context.annotation.Configuration;
2526
import org.springframework.core.io.support.SpringFactoriesLoader;
2627
import org.springframework.core.type.classreading.MetadataReader;
@@ -63,11 +64,9 @@ private boolean isAutoConfiguration(MetadataReader metadataReader) {
6364

6465
protected List<String> getAutoConfigurations() {
6566
if (this.autoConfigurations == null) {
66-
List<String> autoConfigurations = new ArrayList<>();
67-
autoConfigurations.addAll(
67+
List<String> autoConfigurations = new ArrayList<>(
6868
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.beanClassLoader));
69-
autoConfigurations
70-
.addAll(new AutoConfigurationLoader().loadNames(AutoConfiguration.class, this.beanClassLoader));
69+
ImportCandidates.load(AutoConfiguration.class, this.beanClassLoader).forEach(autoConfigurations::add);
7170
this.autoConfigurations = autoConfigurations;
7271
}
7372
return this.autoConfigurations;

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationImportSelector.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.beans.factory.BeanFactoryAware;
4141
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4242
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
43+
import org.springframework.boot.context.annotation.ImportCandidates;
4344
import org.springframework.boot.context.properties.bind.Binder;
4445
import org.springframework.context.EnvironmentAware;
4546
import org.springframework.context.ResourceLoaderAware;
@@ -168,7 +169,7 @@ protected Class<?> getAnnotationClass() {
168169

169170
/**
170171
* Return the auto-configuration class names that should be considered. By default
171-
* this method will load candidates using {@link AutoConfigurationLoader} with
172+
* this method will load candidates using {@link ImportCandidates} with
172173
* {@link #getSpringFactoriesLoaderFactoryClass()}. For backward compatible reasons it
173174
* will also consider {@link SpringFactoriesLoader} with
174175
* {@link #getSpringFactoriesLoaderFactoryClass()}.
@@ -178,12 +179,11 @@ protected Class<?> getAnnotationClass() {
178179
* @return a list of candidate configurations
179180
*/
180181
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
181-
List<String> configurations = new ArrayList<>();
182-
configurations.addAll(
182+
List<String> configurations = new ArrayList<>(
183183
SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));
184-
configurations.addAll(new AutoConfigurationLoader().loadNames(AutoConfiguration.class, getBeanClassLoader()));
184+
ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);
185185
Assert.notEmpty(configurations,
186-
"No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring-boot/org.springframework.boot.autoconfigure.AutoConfiguration. If you "
186+
"No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you "
187187
+ "are using a custom packaging, make sure that file is correct.");
188188
return configurations;
189189
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/EnableAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
29+
import org.springframework.boot.context.annotation.ImportCandidates;
2930
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
3031
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
3132
import org.springframework.context.annotation.Conditional;
@@ -60,7 +61,7 @@
6061
* and classes can be searched.
6162
* <p>
6263
* Auto-configuration classes are regular Spring {@link Configuration @Configuration}
63-
* beans. They are located using the {@link AutoConfigurationLoader} and the
64+
* beans. They are located using {@link ImportCandidates} and the
6465
* {@link SpringFactoriesLoader} mechanism (keyed against this class). Generally
6566
* auto-configuration beans are {@link Conditional @Conditional} beans (most often using
6667
* {@link ConditionalOnClass @ConditionalOnClass} and

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959

6060
/**
6161
* The auto-configuration classes that should be imported. When empty, the classes are
62-
* specified using a file in {@code META-INF/spring-boot} where the file name is the
63-
* fully-qualified name of the annotated class.
62+
* specified using a file in {@code META-INF/spring} where the file name is the
63+
* fully-qualified name of the annotated class, suffixed with '.imports'.
6464
* @return the classes to import
6565
*/
6666
@AliasFor("value")

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfigurationImportSelector.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import java.util.Set;
2929

3030
import org.springframework.boot.context.annotation.DeterminableImports;
31+
import org.springframework.boot.context.annotation.ImportCandidates;
3132
import org.springframework.core.annotation.AnnotatedElementUtils;
3233
import org.springframework.core.annotation.AnnotationAttributes;
3334
import org.springframework.core.annotation.AnnotationUtils;
@@ -95,9 +96,9 @@ private Collection<String> getConfigurationsForAnnotation(Class<?> source, Annot
9596
}
9697

9798
protected Collection<String> loadFactoryNames(Class<?> source) {
98-
List<String> factoryNames = new ArrayList<>();
99-
factoryNames.addAll(SpringFactoriesLoader.loadFactoryNames(source, getBeanClassLoader()));
100-
factoryNames.addAll(new AutoConfigurationLoader().loadNames(source, getBeanClassLoader()));
99+
List<String> factoryNames = new ArrayList<>(
100+
SpringFactoriesLoader.loadFactoryNames(source, getBeanClassLoader()));
101+
ImportCandidates.load(source, getBeanClassLoader()).forEach(factoryNames::add);
101102
return factoryNames;
102103
}
103104

0 commit comments

Comments
 (0)