Skip to content

Commit 5c4b698

Browse files
committed
Support string names @AutoConfigureBefore/After
Update @AutoConfigureBefore and @AutoConfigureAfter annotations to support String classnames in addition direct Class references. Fixes gh-2529
1 parent affa584 commit 5c4b698

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationSorter.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -18,12 +18,10 @@
1818

1919
import java.io.IOException;
2020
import java.util.ArrayList;
21-
import java.util.Arrays;
2221
import java.util.Collection;
2322
import java.util.Collections;
2423
import java.util.Comparator;
2524
import java.util.HashMap;
26-
import java.util.HashSet;
2725
import java.util.LinkedHashSet;
2826
import java.util.List;
2927
import java.util.Map;
@@ -56,15 +54,11 @@ public AutoConfigurationSorter(ResourceLoader resourceLoader) {
5654

5755
public List<String> getInPriorityOrder(Collection<String> classNames)
5856
throws IOException {
59-
6057
final AutoConfigurationClasses classes = new AutoConfigurationClasses(
6158
this.metadataReaderFactory, classNames);
62-
6359
List<String> orderedClassNames = new ArrayList<String>(classNames);
64-
6560
// Initially sort alphabetically
6661
Collections.sort(orderedClassNames);
67-
6862
// Then sort by order
6963
Collections.sort(orderedClassNames, new Comparator<String>() {
7064
@Override
@@ -74,12 +68,9 @@ public int compare(String o1, String o2) {
7468
return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0;
7569
}
7670
});
77-
7871
// Then respect @AutoConfigureBefore @AutoConfigureAfter
7972
orderedClassNames = sortByAnnotation(classes, orderedClassNames);
80-
8173
return orderedClassNames;
82-
8374
}
8475

8576
private List<String> sortByAnnotation(AutoConfigurationClasses classes,
@@ -170,7 +161,10 @@ private Set<String> getAnnotationValue(Class<?> annotation) {
170161
if (attributes == null) {
171162
return Collections.emptySet();
172163
}
173-
return new HashSet<String>(Arrays.asList((String[]) attributes.get("value")));
164+
Set<String> value = new LinkedHashSet<String>();
165+
Collections.addAll(value, (String[]) attributes.get("value"));
166+
Collections.addAll(value, (String[]) attributes.get("name"));
167+
return value;
174168
}
175169

176170
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
* The auto-configure classes that should have already been applied.
3636
* @return the classes
3737
*/
38-
Class<?>[] value();
38+
Class<?>[] value() default {};
39+
40+
/**
41+
* The names of the auto-configure classes that should have already been applied.
42+
* @return the class names
43+
* @since 1.2.2
44+
*/
45+
String[] name() default {};
3946

4047
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
* The auto-configure classes that should have not yet been applied.
3636
* @return the classes
3737
*/
38-
Class<?>[] value();
38+
Class<?>[] value() default {};
39+
40+
/**
41+
* The names of the auto-configure classes that should have already been applied.
42+
* @return the class names
43+
* @since 1.2.2
44+
*/
45+
String[] name() default {};
3946

4047
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2013 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -48,4 +48,5 @@
4848
* @return the class names that must be present.
4949
*/
5050
public String[] name() default {};
51+
5152
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 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.
@@ -51,6 +51,8 @@ public class AutoConfigurationSorterTests {
5151
private static final String X = AutoConfigureX.class.getName();
5252
private static final String Y = AutoConfigureY.class.getName();
5353
private static final String Z = AutoConfigureZ.class.getName();
54+
private static final String A2 = AutoConfigureA2.class.getName();
55+
private static final String W2 = AutoConfigureW2.class.getName();
5456

5557
@Rule
5658
public ExpectedException thrown = ExpectedException.none();
@@ -94,6 +96,13 @@ public void byAutoConfigureMixedBeforeAndAfter() throws Exception {
9496
assertThat(actual, nameMatcher(C, W, B, A, X));
9597
}
9698

99+
@Test
100+
public void byAutoConfigureMixedBeforeAndAfterWithClassNames() throws Exception {
101+
List<String> actual = this.sorter.getInPriorityOrder(Arrays.asList(A2, B, C, W2,
102+
X));
103+
assertThat(actual, nameMatcher(C, W2, B, A2, X));
104+
}
105+
97106
@Test
98107
public void byAutoConfigureMixedBeforeAndAfterWithDifferentInputOrder()
99108
throws Exception {
@@ -160,6 +169,10 @@ public static class OrderHighest {
160169
public static class AutoConfigureA {
161170
}
162171

172+
@AutoConfigureAfter(name = "org.springframework.boot.autoconfigure.AutoConfigurationSorterTests$AutoConfigureB")
173+
public static class AutoConfigureA2 {
174+
}
175+
163176
@AutoConfigureAfter({ AutoConfigureC.class, AutoConfigureD.class,
164177
AutoConfigureE.class })
165178
public static class AutoConfigureB {
@@ -179,6 +192,10 @@ public static class AutoConfigureE {
179192
public static class AutoConfigureW {
180193
}
181194

195+
@AutoConfigureBefore(name = "org.springframework.boot.autoconfigure.AutoConfigurationSorterTests$AutoConfigureB")
196+
public static class AutoConfigureW2 {
197+
}
198+
182199
public static class AutoConfigureX {
183200
}
184201

0 commit comments

Comments
 (0)