diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfiguration.java index 3fee75c5618..a8ad538d823 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfiguration.java @@ -25,8 +25,11 @@ import java.lang.annotation.Target; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.annotation.ImportCandidates; +import org.springframework.context.annotation.AnnotationBeanNameGenerator; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.AliasFor; import org.springframework.core.io.support.SpringFactoriesLoader; /** @@ -55,6 +58,52 @@ import org.springframework.core.io.support.SpringFactoriesLoader; @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration(proxyBeanMethods = false) +@AutoConfigureBefore +@AutoConfigureAfter public @interface AutoConfiguration { + /** + * Explicitly specify the name of the Spring bean definition associated with the + * {@code @AutoConfiguration} class. If left unspecified (the common case), a bean + * name will be automatically generated. + *
+ * The custom name applies only if the {@code @AutoConfiguration} class is picked up
+ * via component scanning or supplied directly to an
+ * {@link AnnotationConfigApplicationContext}. If the {@code @AutoConfiguration} class
+ * is registered as a traditional XML bean definition, the name/id of the bean element
+ * will take precedence.
+ * @return the explicit component name, if any (or empty String otherwise)
+ * @see AnnotationBeanNameGenerator
+ */
+ @AliasFor(annotation = Configuration.class)
+ String value() default "";
+
+ /**
+ * The auto-configure classes that should have not yet been applied.
+ * @return the classes
+ */
+ @AliasFor(annotation = AutoConfigureBefore.class, attribute = "value")
+ Class>[] before() default {};
+
+ /**
+ * The names of the auto-configure classes that should have not yet been applied.
+ * @return the class names
+ */
+ @AliasFor(annotation = AutoConfigureBefore.class, attribute = "name")
+ String[] beforeName() default {};
+
+ /**
+ * The auto-configure classes that should have already been applied.
+ * @return the classes
+ */
+ @AliasFor(annotation = AutoConfigureAfter.class, attribute = "value")
+ Class>[] after() default {};
+
+ /**
+ * The names of the auto-configure classes that should have already been applied.
+ * @return the class names
+ */
+ @AliasFor(annotation = AutoConfigureAfter.class, attribute = "name")
+ String[] afterName() default {};
+
}
diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java
index e3bb47f88cf..f1e99e1f9a3 100644
--- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java
+++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2020 the original author or authors.
+ * Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java
index 5da0b3a3f95..eef105ff0ce 100644
--- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java
+++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2020 the original author or authors.
+ * Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java
index 6d25109e970..93191028ed6 100644
--- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java
+++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2019 the original author or authors.
+ * Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
import java.util.Properties;
import java.util.Set;
@@ -28,6 +29,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.Ordered;
+import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
@@ -43,6 +45,7 @@ import static org.mockito.Mockito.mock;
*
* @author Phillip Webb
* @author Andy Wilkinson
+ * @author Moritz Halbritter
*/
class AutoConfigurationSorterTests {
@@ -54,8 +57,14 @@ class AutoConfigurationSorterTests {
private static final String A = AutoConfigureA.class.getName();
+ private static final String A2 = AutoConfigureA2.class.getName();
+
+ private static final String A3 = AutoConfigureA3.class.getName();
+
private static final String B = AutoConfigureB.class.getName();
+ private static final String B2 = AutoConfigureB2.class.getName();
+
private static final String C = AutoConfigureC.class.getName();
private static final String D = AutoConfigureD.class.getName();
@@ -64,15 +73,17 @@ class AutoConfigurationSorterTests {
private static final String W = AutoConfigureW.class.getName();
+ private static final String W2 = AutoConfigureW2.class.getName();
+
private static final String X = AutoConfigureX.class.getName();
private static final String Y = AutoConfigureY.class.getName();
+ private static final String Y2 = AutoConfigureY2.class.getName();
+
private static final String Z = AutoConfigureZ.class.getName();
- private static final String A2 = AutoConfigureA2.class.getName();
-
- private static final String W2 = AutoConfigureW2.class.getName();
+ private static final String Z2 = AutoConfigureZ2.class.getName();
private AutoConfigurationSorter sorter;
@@ -95,12 +106,24 @@ class AutoConfigurationSorterTests {
assertThat(actual).containsExactly(C, B, A);
}
+ @Test
+ void byAutoConfigureAfterAliasFor() {
+ List