Use spring.factories to declare each test slice's auto-config imports

Closes gh-6001
This commit is contained in:
Andy Wilkinson
2016-05-26 12:32:02 +01:00
parent 1b0bbd89a2
commit 73b01cff4f
11 changed files with 74 additions and 43 deletions

View File

@@ -26,7 +26,7 @@ import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
/**
* Import and apply the selected auto-configuration classes. Applies the same ordering
* Import and apply the specified auto-configuration classes. Applies the same ordering
* rules as {@code @EnableAutoConfiguration} but restricts the auto-configuration classes
* to the specified set, rather than consulting {@code spring.factories}.
* <p>
@@ -35,6 +35,7 @@ import org.springframework.context.annotation.Import;
* and especially when writing tests.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.3.0
*/
@Target(ElementType.TYPE)
@@ -46,9 +47,11 @@ import org.springframework.context.annotation.Import;
public @interface ImportAutoConfiguration {
/**
* The auto-configuration classes that should be imported.
* The auto-configuration classes that should be imported. When empty, the classes are
* specified using an entry in {@code META-INF/spring.factories} where the key is the
* fully-qualified name of the annotated class.
* @return the classes to import
*/
Class<?>[] value();
Class<?>[] value() default {};
}

View File

@@ -27,6 +27,7 @@ import java.util.Set;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
@@ -77,19 +78,25 @@ class ImportAutoConfigurationImportSelector
if (source != null && seen.add(source)) {
for (Annotation annotation : source.getDeclaredAnnotations()) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) {
collectCandidateConfigurations(annotation, candidates, seen);
collectCandidateConfigurations(source, annotation, candidates, seen);
}
}
collectCandidateConfigurations(source.getSuperclass(), candidates, seen);
}
}
private void collectCandidateConfigurations(Annotation annotation,
private void collectCandidateConfigurations(Class<?> source, Annotation annotation,
Set<String> candidates, Set<Class<?>> seen) {
if (ANNOTATION_NAMES.contains(annotation.annotationType().getName())) {
String[] value = (String[]) AnnotationUtils
.getAnnotationAttributes(annotation, true).get("value");
candidates.addAll(Arrays.asList(value));
if (value.length > 0) {
candidates.addAll(Arrays.asList(value));
}
else {
candidates.addAll(SpringFactoriesLoader.loadFactoryNames(source,
getClass().getClassLoader()));
}
}
collectCandidateConfigurations(annotation.annotationType(), candidates, seen);
}